Sunday, January 31, 2016

How to Connect to a MySQL DB and Get Data in PHP using mysqli Syntax

<?php

$dburl = "localhost";
$dbuname = "USERNAME";
$dbpwd = "PASSWORD";
$dbname = "databasename";

$mysqli = new mysqli($dburl, $dbuname, $dbpwd, $dbname);

$query = 'SELECT name, price FROM Products ORDER BY name';

if ($mysqli->errno)
{
printf("Unable to connect to database:<br/> %s", $mysqli->error);
exit();
}
else
{
$result = $mysqli->query($query, MYSQLI_STORE_RESULT);
while(list($name, $price)= $result->fetch_row())
{
printf("Price of %s is: \$%s <br/>", $name, $price);
}
printf("<br>Task Completed.");
}
?>

No comments:

Post a Comment