michalchojno Posted July 20, 2009 Share Posted July 20, 2009 I seem to struggle with establishing a simple connection to MySQL database using PHP and fetching data from SQL and displaying them on the screen. Here is a code I wrote to carefully check if everything works: (yet it doesn't) <?php echo 'Start.<p>'; $db = mysql_connect('localhost', 'root', 'password', 'db'); // replace 'password' and 'db' with the right ones. echo 'Check1: Connection to db.<p>'; $q = "SELECT id, owner from table where id = 69"; //when I execute this Select throu PhpMyAdmin I get 2 records as a result. echo 'Check2: q defined as Select statement.<p>'; $r = mysql_query ($db, $q); echo 'Check3: r defined as a query.<p>'; if ($r) { echo ' <table> <tr> <td align="left"><b>ref</b></td> <td align="left"><b>owner</b></td> </tr> '; echo 'Check4: if r ok, create table's header.<p>'; while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) { echo ' <tr> <td align="left"'.$row['id'].'</td> <td align="left"'.$row['owner'].'</td> </tr> '; } echo '</table>'; mysql_free_result ($r); } else { echo 'System error.'; echo 'Error: '.mysql_error($db).' Query: '.$q.'.'; } mysql_close ($db); echo 'End.<p>'; ?> As a result of this query, this is what I get: Start. Check1: Connection to db. Check2: q defined as Select statement. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:...file.php on line 18 Check3: r defined as a query. System error.Error: Query: SELECT id, owner from equipment where id = 69. End. I don't understand why it doesn't work. I execture this Select statement directly in the database and it's OK. Yet the script here fails to generate the right results. Any tips why? How can I fix this? Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/ Share on other sites More sharing options...
jamesxg1 Posted July 20, 2009 Share Posted July 20, 2009 Hiya, Firstly this line should be, $r = mysql_query ($q, $db); and try this, $q = "SELECT `id`, `owner` FROM `table` WHERE id = '69'" or die(mysql_error()); James. Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878793 Share on other sites More sharing options...
PFMaBiSmAd Posted July 20, 2009 Share Posted July 20, 2009 @jamesxg1, putting an or die() on the end of an assignment statement ($q = ....) does not tell you anything. It should be on the mysql_query() statement. And in fact the code is already using mysql_error() in its' if() {} else {} logic. Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878800 Share on other sites More sharing options...
michalchojno Posted July 20, 2009 Author Share Posted July 20, 2009 I changed the syntax of mysql_query to $r = mysql_query ($q, $db); It still refuses to generate the right results. This is what I get: Start. Check1: Connection to db. Check2: q defined as Select statement. Check3: r defined as a query. System error.Error: No database was selected. Query: SELECT id, owner from equipment where id = 69. End.[/query] Yes, the die() statement is not crucial. I'd first like to make the connection work and generate some results. Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878817 Share on other sites More sharing options...
PFMaBiSmAd Posted July 20, 2009 Share Posted July 20, 2009 So, the error message is telling you what the problem is - No database was selected You must select a database before executing a query - mysql_select_db Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878820 Share on other sites More sharing options...
michalchojno Posted July 20, 2009 Author Share Posted July 20, 2009 Argh, still not it.... Thank you so much for all this help and patience. This helps a lot, although this is frustrating. I still have an error. OK, here is my modified code: <?php echo 'Start.<p>'; $link = mysql_connect('localhost', 'root', 'password'); // replace 'password' with the right one. echo 'Check1: Connection to db.<p>'; $db = mysql_select_db('db', $link); // replace 'db' with the right one. echo 'Check1a: DB defined.<p>'; $q = "SELECT id, owner from table where id = 69"; //when I execute this Select throu PhpMyAdmin I get 2 records as a result. echo 'Check2: q defined as Select statement.<p>'; $r = mysql_query ($q, $db); echo 'Check3: r defined as a query.<p>'; if ($r) { echo ' <table> <tr> <td align="left"><b>ref</b></td> <td align="left"><b>owner</b></td> </tr> '; echo 'Check4: if r ok, create table's header.<p>'; while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) { echo ' <tr> <td align="left"'.$row['id'].'</td> <td align="left"'.$row['owner'].'</td> </tr> '; } echo '</table>'; mysql_free_result ($r); } else { echo 'System error.'; echo 'Error: '.mysql_error($link).' Query: '.$q.'.'; } mysql_close ($link); echo 'End.<p>'; ?> And the results: Start. Check1: Connection to db. Check1a: DB defined. Check2: q defined as Select statement. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:...file.php on line 25 Check3: r defined as a query. System error.Error: Query: SELECT id, owner from equipment where id = 69. End. Any ideas what might be wrong? Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878831 Share on other sites More sharing options...
jamesxg1 Posted July 20, 2009 Share Posted July 20, 2009 @jamesxg1, putting an or die() on the end of an assignment statement ($q = ....) does not tell you anything. It should be on the mysql_query() statement. And in fact the code is already using mysql_error() in its' if() {} else {} logic. oh christ! lol sorry my mind is in wonder land lol sorry guy's James. Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878837 Share on other sites More sharing options...
michalchojno Posted July 20, 2009 Author Share Posted July 20, 2009 Wait wait... I think I know. mysql_query ($q, $link) and NOT mysql_query ($q, $db). Right? Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878838 Share on other sites More sharing options...
jamesxg1 Posted July 20, 2009 Share Posted July 20, 2009 Yea!, how didnt we see this shame on me lol yes that may just work lol Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878839 Share on other sites More sharing options...
michalchojno Posted July 20, 2009 Author Share Posted July 20, 2009 Let me check... Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878858 Share on other sites More sharing options...
michalchojno Posted July 20, 2009 Author Share Posted July 20, 2009 Wheeeew...... IT WORKS!!! I really owe you guys. Thank you so much for your patient help. This forum is awesome! How do I mark this thread as SOLVED? Final question: Do I always need to use both mysql_connect () and mysql_select_db () ? Link to comment https://forums.phpfreaks.com/topic/166658-solved-phpmysql-connection-to-database/#findComment-878870 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.