I see few errors there...
Try this...
PHP Code:
<?php
$link = mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("testdb", $link) or die(mysql_error());
$sql = "SELECT * FROM tbltestdb";
$result = mysql_query($sql, $link);
while ($row = mysql_fetch_array($result))
{
$id = $row['id'];
$name = $row['name'];
echo "ID: " . $id . "<br />";
echo "Name: " . $name . "<br />";
}
mysql_close($link);
?>
Now the errors I'm suspecting are...
1.
PHP Code:
# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "hello");
define("MYSQL_DB", "test");
$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
So the string that's getting genrated is...
PHP Code:
$conn = mysql_connect("""localhost""", """root""", """hello""") or die(mysql_error());
Because...you wrote
mysql_connect("".MYSQL_HOST.""
so the variable MYSQL_HOST = "localhost" that you defined avobe
so "localhost" gets concatinated as ""."localhost"."" which becomes """localhost"""
Right..??
So you should have write it as...
PHP Code:
$conn = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die(mysql_error());
Better use...
PHP Code:
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$link = mysql_connect($dbhost, $dbuser, $dbpass);
Another one is...
2.
PHP Code:
$res = mysql_query($sql);
PHP Code:
mysql_query($sql);
should actually be
PHP Code:
mysql_query($sql, $link);
Though I've seen many people to write it as like you did. I don't know whether it's right or wrong, but $link should be provided as the function prototype itself says that...
Correct me if I'm wrong anywhere..!!
And lastly one suggestion...always perform mysql_close();
Happy Coding