PHP - mysql_fetch_assoc() issue
I'm new to PHP and I'm trying to learn how to display data I select from a
mysql database (phpmyadmin). My current php code consists of 2 files
connect.inc.php and connecting.php:
conncet.inc.php is the file used to establish connection with the database:
<?php
//Varialbles
//MYSQL details
$mysql_server='localhost';
$mysql_user='root';
$mysql_user_pass='12345678';
$mysql_db='test';
//Messages
$db_conn_error='Could not connect to database';
if(mysql_connect($mysql_server, $mysql_user, $mysql_user_pass)
and mysql_select_db($mysql_db)){
echo 'Connection is ok'.'<br>';
}
else
{
echo 'Connection is not ok';
}
?>
while the other file (connecting.php) should display any records in the
particular table called food :
<?php
require 'connect.inc.php';
$query = "SELECT 'food', 'calories' FROM food ORDER BY 'id'";
if ($query_run = mysql_query($query)){
while($query_row = mysql_fetch_assoc($query_run)){
$food = $query_row['food'];
$calories = $query_row['calories'];
echo $food . ' has '.$calories.' calories.<br>';
}
}
else
{
echo 'Query Failed';
}
?>
Additional details:
server name : localhost
user : root
password : 12345678
database name : test
table name : food
field names : id, food, calories, healthy_unhealthy
Ths issue is that whenever i execute code in the file connecting.php i
always get the following:
Connection is ok
food has calories calories.
food has calories calories.
When it should say
Connection is ok
Pizza has 1000 calories
Salad has 200 calories
Help is highly appreciated :)
Thanks in advance,
Joe :)
No comments:
Post a Comment