drumhrd Posted April 15, 2009 Share Posted April 15, 2009 Ok so I am a noob. I have attempted to use multiple tutorials and they all end up have script errors. Which I cannot figure out. It's hard to learn something when every tutorial out there is not accurate, and has script errors. I am getting to errors from the following code. Notice: Query: MySQL Error: No database selected in /var/www/links2.php on line 16 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/links2.php on line 17 Sorry, no records were found! According to this code I would have assumed I have already selected the DB by using mysql_select_db("mydb",$db); It seems as if the selected database does not pass from the if logic to the else logic. Thanks in advance for your help. <?php $db = mysql_connect("localhost", "root". "<ommited>"); mysql_select_db("mydb",$db); // display individual record if ($id) { $result = mysql_query("SELECT * FROM employees WHERE id=$id",$db) or trigger_error("Query: \n<br />MySQL Error: " . mysql_error()); $myrow = mysql_fetch_array($result); printf("First name: %s\n<br>", $myrow["first"]); printf("Last name: %s\n<br>", $myrow["last"]); printf("Address: %s\n<br>", $myrow["address"]); printf("Position: %s\n<br>", $myrow["position"]); } else { // show employee list $result = mysql_query("SELECT * FROM employees",$db) or trigger_error("Query: \n<br />MySQL Error: " . mysql_error()); if ($myrow = mysql_fetch_array($result)) { // display list if there are records to display do { printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]); } while ($myrow = mysql_fetch_array($result)); } else { // no records to display echo "Sorry, no records were found!"; } } ?> Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/ Share on other sites More sharing options...
MasterACE14 Posted April 15, 2009 Share Posted April 15, 2009 try this... <?php $db = mysql_connect("localhost", "root". "<ommited>"); $db = mysql_select_db("mydb",$db); // display individual record if ($id) { $result = mysql_query("SELECT * FROM employees WHERE id=$id") or die("Query: \n<br />MySQL Error: " . mysql_error()); $myrow = mysql_fetch_array($result); printf("First name: %s\n<br>", $myrow["first"]); printf("Last name: %s\n<br>", $myrow["last"]); printf("Address: %s\n<br>", $myrow["address"]); printf("Position: %s\n<br>", $myrow["position"]); } else { // show employee list $result = mysql_query("SELECT * FROM employees") or die("Query: \n<br />MySQL Error: " . mysql_error()); if (mysql_num_rows($result) > 0) { // display list if there are records to display $myrow = mysql_fetch_array($result); printf("<a href=\"%s?id=%s\">%s%s</a><br>\n", $_SERVER['PHP_SELF'], $myrow["id"], $myrow["first"], $myrow["last"]); } else { // no records to display echo "Sorry, no records were found!"; } } ?> Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/#findComment-810268 Share on other sites More sharing options...
drumhrd Posted April 15, 2009 Author Share Posted April 15, 2009 nope..still getting the same errors Notice: Query: 2 MySQL Error: No database selected in /var/www/links2.php on line 16 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/links2.php on line 17 Sorry, no records were found! Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/#findComment-810275 Share on other sites More sharing options...
MasterACE14 Posted April 15, 2009 Share Posted April 15, 2009 you sure the database name is right? and the table `employees` exists? Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/#findComment-810277 Share on other sites More sharing options...
drumhrd Posted April 15, 2009 Author Share Posted April 15, 2009 yes the DB name is correct..this code..the previous piece in the tutorial is working fine <?php $db = mysql_connect("localhost", "root", "ommited"); mysql_select_db("mydb",$db); $result = mysql_query("SELECT * FROM employees",$db); if ($myrow = mysql_fetch_array($result)) { do { printf("<a href=\"%s?id=%s \">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]); } while ($myrow = mysql_fetch_array($result)); } else { echo "Sorry, no records were found!"; } ?> Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/#findComment-810279 Share on other sites More sharing options...
drumhrd Posted April 15, 2009 Author Share Posted April 15, 2009 and here is the table format CREATE TABLE employees ( id tinyint(4) NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), address varchar(255), position varchar(50), PRIMARY KEY (id), UNIQUE id (id)); INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager'); INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist'); INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman'); Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/#findComment-810281 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2009 Share Posted April 15, 2009 So, find out why the database was not selected. Change your mysql_select_db code to the following - mysql_select_db("mydb",$db) or die("Could not select database: " . mysql_error()); Edit: There is also a dot . in the following that should be a comma - $db = mysql_connect("localhost", "root". "<ommited>"); Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/#findComment-810284 Share on other sites More sharing options...
drumhrd Posted April 15, 2009 Author Share Posted April 15, 2009 awesome..the "." was the problem...I am such a noob!!! thanks for you help Link to comment https://forums.phpfreaks.com/topic/154140-solved-newbie-questionno-database-selected/#findComment-810324 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.