Pulling data from MySQL using PHP

Netshock on PHP

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.

Tagged under: