Maracles Posted May 25, 2009 Share Posted May 25, 2009 I am receiving the following error when trying to process the code found below. I havedone substantial amounts of searching on Google and found mutiple confliciting reasons for what causes this error, the solutions to which have not yet worked. I am pretty certain it is not a problem with connecting to the database, and I have checked several times for typos and cannot find any. Can anyone suggest a solution. If proposing a solution can you bare in mind that I use a hosted solution therefore I do not have easy access to changing config files etc (some solutions I have read so far propose this), I am also not familiar with how these files work. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /homepages/36/d271495301/htdocs/Websites/ChimEnt/phpdev/chap11/results.php on line 34 Number of books found: Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /homepages/36/d271495301/htdocs/Websites/ChimEnt/phpdev/chap11/results.php on line 53 <html> <head> <title>Book-O-Rama Search Results</title> </head> <body> <h1>Book-O-Rama Search Results</h1> <?php error_reporting(E_ALL); // create short variable names $searchtype=$_POST['searchtype']; $searchterm=trim($_POST['searchterm']); if (!$searchtype || !$searchterm) { echo 'You have not entered search details. Please go back and try again.'; exit; } if (!get_magic_quotes_gpc()){ $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); } @ $db = mysql_connect('db1920.oneandone.co.uk', 'dbo285208419', 'xxxxxxx'); if (mysql_errno ()) { echo 'Error: Could not connect to database. Please try again later.'; exit; } $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = mysql_query($db, $query); $num_results = $result->num_rows; echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0; $i <$num_results; $i++) { $row = mysql_fetch_row ($result); echo "<p><strong>".($i+1).". Title: "; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong><br />Author: "; echo stripslashes($row['author']); echo "<br />ISBN: "; echo stripslashes($row['isbn']); echo "<br />Price: "; echo stripslashes($row['price']); echo "</p>"; } mysql_free_result ($result); mysql_close ($db); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/ Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Share Posted May 25, 2009 It's because the query isn't validating or returning any results. Try to add or die(mysql_error()) to your mysql queries. Like so: $result = mysql_query($db, $query) or die (mysql_error()); Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841867 Share on other sites More sharing options...
PFMaBiSmAd Posted May 25, 2009 Share Posted May 25, 2009 Description resource mysql_query ( string $query [, resource $link_identifier ] ) The first parameter is the query string, the second is the link identifier. To effectively use any of the php functions, you must make use of the documentation at some point in time before, during, and especially after they don't work the way you are attempting to use them. Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841874 Share on other sites More sharing options...
garethhall Posted May 25, 2009 Share Posted May 25, 2009 I think the problem is in your query Please update and try this I could be completely wrong lol but worth a try <? $query = "select * from books where $searchtype like % $searchterm %"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841887 Share on other sites More sharing options...
Maracles Posted May 25, 2009 Author Share Posted May 25, 2009 Thanks for that anti-moronic. That helped clear the second error. After doing that all I got was: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /homepages/36/d271495301/htdocs/Websites/ChimEnt/phpdev/chap11/results.php on line 34 Thank you also PFM. I have to admit to struggling to understand the php syntax's sometimes, I have been working from a book as such I have not had to use them as often as maybe I should. I have now looked at the function page but am still slightly confused. As I understand it the string should be $query, and the link identifier is the variable I used for the mysql_connect earlier in the code i.e. $db. Using this understanding I have rearranged the code as below: $query = "SELECT * FROM books WHERE ".$searchtype." LIKE '%".$searchterm."%'"; $result = mysql_query($query, $db) or die (mysql_error()); Now when I run this I get a different error message, this is: No database selected I have also tried without the link identifier but that also produced the same message. Can you provide further assistence? Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841892 Share on other sites More sharing options...
Maracles Posted May 25, 2009 Author Share Posted May 25, 2009 gareth thanks for the input, I have tried you way however and i'm getting the same 'No database selected' message as in my above post. At least I'm getting somehwere - I think?! Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841895 Share on other sites More sharing options...
garethhall Posted May 25, 2009 Share Posted May 25, 2009 Try to do a fetch and see if you get the same change $num_results = $result->num_rows; to $num_results = mysql_fetch_assoc($result ); Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841913 Share on other sites More sharing options...
Maracles Posted May 25, 2009 Author Share Posted May 25, 2009 Thanks Gareth, I have done that but no luck. I have continued to read about people having similar problems and am thinking it may be something to do with my privileges on the database. I use shared hosting for my website (1and1) and although I created the database myself is it possible that I do not have enough privileges for the query to run? How do I check my own privileges? Would not having privileges cause the same error? I have tried calling my hosting company but unfortuantely they are not very helpful and had to give up after 15 min of trying to explain! Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841921 Share on other sites More sharing options...
garethhall Posted May 25, 2009 Share Posted May 25, 2009 Please verify that the query is working Do this <? $result = mysql_query($db, $query); if(!$result){ echo "mysql query not working"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841929 Share on other sites More sharing options...
Maracles Posted May 25, 2009 Author Share Posted May 25, 2009 I added your code as instructed and got the following errors; Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /homepages/36/d271495301/htdocs/Websites/ChimEnt/phpdev/chap11/results.php on line 35 mysql query not working Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /homepages/36/d271495301/htdocs/Websites/ChimEnt/phpdev/chap11/results.php on line 40 Number of books found: Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /homepages/36/d271495301/htdocs/Websites/ChimEnt/phpdev/chap11/results.php on line 57 Then, after removing the $db from infront of the $query in the code you provided I receive the same errors minus the warning about line 35. Either way your code indicates the the query is not working. This obviously means that a connection is being made to the database (if not the connection error would be displayed), but that the query cannot be run correctly. Does this point even more to a privileges issue? Any idea why removing the $db removes the warning on line 35? Once again I appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841932 Share on other sites More sharing options...
premiso Posted May 25, 2009 Share Posted May 25, 2009 After you connect to a database you need to use mysql_select_db to select the database you want to use. If you do not do that, MySQL does not know which database it needs to pull the results from. Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841957 Share on other sites More sharing options...
Maracles Posted May 25, 2009 Author Share Posted May 25, 2009 Ah Ha!! Thank you very much, that (kinda) worked. All the other errors have disappeared and now all I have is the message below Can't use dbo285208419: Access denied for user 'dbo285208419'@'%' to database 'dbo285208419' This suggests there must also be an issue with permissions/privileges. Can you shed any light on that? I pay for my hosting, I created the database from within the hosting control panel and I set all user names and passwords, why would I not have access to my own database? I have gone into the database itself and tried to grant privilege using the GRANT command but this lists the same error as above. Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-841963 Share on other sites More sharing options...
Maracles Posted May 26, 2009 Author Share Posted May 26, 2009 OK, now I feel stupid. Found the reason for the access error (after finally getting some sleep!). I was using the database user name for the database name instead of obviously using the actual database name, With 1and1 hosting they do not allow you to rename the database yourself or the user name as such the only difference the user name and database name is an additional 'o'. i.e. dbo285208419 and db285208419. At least that will teach me to check more carefully. Thanks for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/159617-solved-mysql_query-supplied-argument-is-not-a-valid-mysql-link-resource-in-error/#findComment-842098 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.