I have just started learning php and i am trying to set up connection to local mysql db I have pasted the code that i am trying to run.
I am getting $result = false and i reach else part --" No return" In the image attached , there is a table called "table" under db test, and i have 3-4 entries as well
Can someone take time to help me understand whats going wrong here ?
thank you !!
<!doctype html>
<html>
<head>
</head>
<body>
<?php
$link = mysqli_connect('localhost','root','','test');
$query = "select * from 'table' " ;
$outputDisplay = "";
$myrowcount = 0;
if (!$link)
{
die('Could not connect to MySQL: ' . mysqli_error());
} else
{
echo 'Connection OK';
print ($query);
$result = mysqli_query($link, $query);
if($result)
{
$numresults = mysqli_num_rows($result);
printf("Select returned %d rows.
", $numresults);
}
else
{
echo "No return";
}
mysqli_close($link);
}
?>
</body>
</html>
mysqli_query()
returns false because something wrong has happened in your query..Here your problem is with the single quotes..Instead of single quotes you should use back-ticks like
$query = "select * from `table` " ;
$query = "select * from 'table' " ;
You can't put table names in single-quotes. Single-quotes are for string literals and date literals.
You may put table names in back-ticks:
$query = "select * from `table` " ;