Daishi Posted June 10, 2007 Share Posted June 10, 2007 Hello. I am trying to open a connection to my MYSQL database with php, and I am using the following code: <? $server = "localhost"; $username = "infotddg_Jonah"; $password = "********"; $connection = mysql_connect ($server, $username, $password); $query = "SELECT * FROM table1 WHERE username like '%3%'"; $results = mysql_query ($query , $connection); $final = mysql_fetch_array ($results); echo "the results are " . $final . "."; ?> When I run the script, it gives me the following error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/infotddg/public_html/MYSQL/test.php on line 17 Is there something wrong with my code? Link to comment https://forums.phpfreaks.com/topic/54992-mysql_query-error/ Share on other sites More sharing options...
Hypnos Posted June 10, 2007 Share Posted June 10, 2007 It means it's an empty result set. Either there was an error in your SQL/connection, or your query just didn't return any results. Echo your query and test it directly (phpmyadmin or mysql client). If it's right, then try echoing mysql_error(). Link to comment https://forums.phpfreaks.com/topic/54992-mysql_query-error/#findComment-271886 Share on other sites More sharing options...
soycharliente Posted June 10, 2007 Share Posted June 10, 2007 You should put a DIE statement with some type of message after you try to connect to your db. That way, if connecting is the problem, the page will tell you. Link to comment https://forums.phpfreaks.com/topic/54992-mysql_query-error/#findComment-271902 Share on other sites More sharing options...
Psycho Posted June 10, 2007 Share Posted June 10, 2007 It means it's an empty result set. Either there was an error in your SQL/connection, or your query just didn't return any results. Echo your query and test it directly (phpmyadmin or mysql client). If it's right, then try echoing mysql_error(). Um, no. An empty result set will NOT return that error. I don't see where you select the database to use. Also, mysql_fetch_array will return, well, an array. So, you simply can't print the array tot he page. Try this (adding the correct database name): <? $server = "localhost"; $username = "infotddg_Jonah"; $password = "********"; $connection = mysql_connect ($server, $username, $password) or die (mysql_error()); mysql_select_db("databasename", $connection) or die (mysql_error()); $query = "SELECT * FROM table1 WHERE username like '%3%'"; $results = mysql_query ($query , $connection) or die (mysql_error()); $final = mysql_fetch_array ($results); echo "The results are:<br><pre>"; print_r($final); echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/54992-mysql_query-error/#findComment-271935 Share on other sites More sharing options...
Hypnos Posted June 10, 2007 Share Posted June 10, 2007 You're right. An empty result set won't return that error. My apologies. Your SQL query has an error. mysql_select_db and mysql_connect will throw different warnings. It's mysql_query. Debug your query, like I suggested. If you use mysql_error(), make sure you comment them out, or remove them when you're done. Link to comment https://forums.phpfreaks.com/topic/54992-mysql_query-error/#findComment-271947 Share on other sites More sharing options...
Psycho Posted June 11, 2007 Share Posted June 11, 2007 Your SQL query has an error. mysql_select_db and mysql_connect will throw different warnings. Well, if you never select a database, or if there is an error when selecting the database and there is no error handling, then you wll only see the error the OP stated. So, it may or may not be the query. I think the most likely cause is that he has not selected a database. Just a guess, but it's kind of hard to run a query when the code doen't know what database to run it against. Link to comment https://forums.phpfreaks.com/topic/54992-mysql_query-error/#findComment-272059 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.