Jump to content

Warning: mysql_error() expects parameter 1 to be resource, string given in F:\ProductsQueries.php on line 43


ziad

Recommended Posts

Hi guys, I am trying to run the code below and I end up getting the error above, as am a newby in php, i just can't figure out where am going wrong, can anyone help me please

 

<?php
 require "db3.inc";

// 1.open the database connection - always use localhost
  $connection = mysql_connect($hostname, $username, $password);

 //2. Open the database connection and use the winestore database
  mysql_select_db($dbname, $connection);
 
  $query = "SELECT * FROM product";
  $ProductType = $_GET["ProductType"] ;
 
 
 //Create the Query
  $query = "SELECT  * FROM  product ";

            
  // ... then, if the user has specified a type ,
   // add the regionName as an AND clause ...
   

   if ($ProductType != 1)
 // Note the literals around $windType
   $query .= " WHERE Make = '$ProductType'";
   // ... and then complete the query.
   $query .= " ORDER BY Make ";
   

 //Get the recordset
 $result = mysql_query($query,$connection) OR die(mysql_error($query));
 
 

  $num_rows = mysql_num_rows($result);

  //Check some records have been found
   if ($num_rows < 1 )
   {
     echo "<strong> No Products found - make sure you entered the Product name correctly</strong>";
   }
   else
   //Format the information into a table using table/cell tags      
    while ($row = @ mysql_fetch_row($result))
    {
    // ... start a Table row...
    echo "\n<tr>";
    
// ... and print out each of the attributes in that row as
    // seperate TD (table Date)

    foreach($row as $data)
        print "\n\t<td> {$data} </td>";
        
    // finish the row
    print "\n<tr>";
    }
 // then finish the table
 print "\n</table>\n";


// (5) Close the connection
mysql_close($connection);

?>
</table>
</body>
</html>

 

 

Link to comment
Share on other sites

$query is a string, $connection is a resource 
 
From the manual

string mysql_error ([ resource $link_identifier = NULL ] )


Try this instead

$result = mysql_query($query,$connection) OR die(mysql_error($connection));

I do strongly advice you to stop using the mysql extension and look at mysqli and/or PDO instead.. See my signature for the reason

Link to comment
Share on other sites

Please use CODE tags when posting code:


 

That error would indicate that you have not selected a database. Back at the beginning of the code you have:

 //2. Open the database connection and use the winestore database
  mysql_select_db($dbname, $connection);
add the or die(mysql_error()); to that statement to see why the database selection is failing. Either the database does not exist, or the user does not have access to it, or the variable is not defined, or something.
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.