Select a database with mysql_select_db
before querying it
mysql_connect("localhost","root","");
mysql_select_db("chatterr");
or specify the database name in the query
$query = "SELECT * FROM chatterr.students ORDER BY id DESC LIMIT 4";
UPDATE: Besides that your is connection probably failing. Change
mysql_connect("localhost","root","");
to
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
to see if that's the case.
UPDATE3: Put it all together. Although that code has a LOT room for improvement it works perfectly fine on my machine.
<?php
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("chatterr", $db);
if (!$db_selected) {
die ('Could not select db: ' . mysql_error());
}
//query databae
$query = "SELECT * FROM test.students ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows)
{
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result,$row,"first_name");
$last_name=mysql_result($result,$row,"last_name"); ?>
<?php echo $id; ?>
<?php
$row++;
}
?>
And please, don't use mysql_* functions for new code. They are
deprecated. Use prepared
statements with either PDO
or MySQLi.