I am new to mysql but have recently tried doing some tutorials to advance my knowledge. Anyways I installed mysql on my ubuntu server as well as phpmyadmin. They both seem to work just fine, I created a database in phpmyadmin called “test” as well as a table called “teddy”.
I put data into the table and I can clearly see it through phpmyadmin.
Now when I try accessing this database via a php script on my ubuntu server, nothing happens. Literally nothing. It reacts the same way as if I had a syntax error (a quick load with nothing displayed).
This is my code… If anyone knows where my mistake is it would be much appreciated.
Thank you
<?php
$username='root';
$password='***';
$database='test';
@$db= mysqli_connect('localhost', $username, $password, $database);
if (mysqli_connect_errno())
{
echo 'Error: Could not connect';
exit;
}
$query= "select * from test";
$result= $db->query($query);
$num_results= $result->num_rows;
echo $num_results;
?>
EDITED (based on drew 010)
Problem was the table was not called test but teddy so putting in this code worked.
“SELECT * FROM ‘teddy'”;
$db= new mysqli('localhost', $username, $password, $database);
if (mysqli_connect_errno())
{
echo 'Error: Could not connect';
exit;
}
$query = "SELECT * FROM 'teddy'";
$result = $db->query($query);
if ($result) {
$num_results = $result->num_rows;
echo $num_results;
} else {
echo "Query failed: {$db->error}\n";
}
?>
$result= $db->query($query);is probably returning false for some reason.Try:
See http://www.php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues