Jump to content

[SOLVED] mysql_query(): supplied argument is not a valid MySQL-Link resource in.. error


Maracles

Recommended Posts

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>

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.