Building my own tech
Now Reading
Pulling data from MySQL using PHP

Pulling data from MySQL using PHP

by KieranMay 9, 2015

I’ve recently been learning some PHP. I’ve been pulling data from a MySQL database to display on a PHP / HTML web page. I spent quite a while trying to get all the connections working properly.

I’ve highlighted in red, the items that you need to change. Once you’ve changed them, this script should work for you too!

<?php

$con = mysql_connect(“SERVER“,”USERNAME“,”PASSWORD“);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“DATABASE“, $con);

$query = “SELECT *
FROM `TABLE` “;

$TABLENAME= mysql_query($query);

while($row = mysql_fetch_array($TABLENAME, MYSQL_ASSOC))
{
$FIELDNAME= $row[‘FIELDNAME‘];
$FIELDNAME= $row[‘FIELDNAME‘];
$FIELDNAME= $row[‘FIELDNAME‘];
$FIELDNAME= $row[‘FIELDNAME‘];

$FIELDNAME= htmlspecialchars($row[‘FIELDNAME‘],ENT_QUOTES);
echo ” <div >

$FIELD NAME
</div>
“;
}

mysql_close($con);
?>

Image used under creative commons

This article was brought to you by Netshock. Netshock aim to provide technology guides and insight to our readers.

What's your reaction?
Love It
0%
Interested
0%
Meh...
0%
What?
0%
Hate It
0%
Sad
0%
About The Author
Kieran

My name is Kieran, I love to see how technology can drive business growth. I started the Netshock technology blog as a place to share my thoughts and experiences with a wider audience. I cover all sorts of topics, from marketing to development.