Jump to content

Recommended Posts

This script is trying to access a MySQL database

 

but it keeps failing at line 82, and I can't find the problem with the code.

 

This is the error I get:

Warning: mysql_select_db() expects parameter 2 to be resource, null given in /Library/WebServer/Documents/example.6-14.php on line 82
Error 0 : 

 

and this is the line that keeps producing the error:

 

    if (!mysql_select_db($databaseName, $connection))
        showerror();

 

 

<!DOCTYPE HTML PUBLIC
               "-//W3C// DTD HTML  4.01  Transitional//EN"
               "http://www.w3.org/TR/html401/loose.dtd">
               
               
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Exploring Wines in a Region</title>
</head>


<body bgcolor="white">
<?php
      require 'db.inc';
      
      
      // Show all wines in a region in a <table>
      function displayWinesList($connection,
                                $query,
                                $regionName)
                                
      
      {
      
           // Run the query on the server
           if (!($result = @ mysql_query ($query, $connection)))
                showerror();
                
           // Find out how many rows are available
           $rowsFound = @ mysql_num_rows($result);
           
           
          // If the query has results
          if ($rowsFound > 0)
          {
          
               // ... print out a header
               print "Wines of $regionName<br>";
               
               
               // and start a <table>
               print "\n<table>\n<tr>" .
                     "\n\t<th>Wine ID</th>" .
                     "\n\t<th>Wine Name</th>" .
                     "\n\t<th>Year</th>"  .
                     "\n\t<th>Winery</th>" .
                     "\n\t<th>Description</th>\n</tr>" ;
                     
               // Fetch each of the query rows
               while ($row = @ mysql_fetch_array($result))
               { 
               
                  // Print one row of results 
                  print "\n<tr>\n\t<td>{$row["wine_id"]}</td>" .
                        "\n\t<td>{$row["wine_name"]}</td>" .
                        "\n\t<td>{$row["year"]}</td>" .
                        "\n\t<td>{$row["winery_name"]}</td>" .
                        "\n\t<td>{$row["description"]}<td>\n</tr>";
                        
               } // end while loop body
               
               
               // Finish the table
               print "\n</table>";
       }  // end if $rowsFound in body
       
       // Report how many rows were found
       
      print "{$rowsFound} records found matching your criteria</br>";
      
   } // end of function
   
   // Connect to the MySQL server
   if (!($connetion = @ mysql_connect($hostName, $username, $password)))
        die("Could not connect");
        
    // Secure the user parameter $regionName
    $regionName = mysqlclean($_GET, "regionName", 30, $connection);
    
    
    if (!mysql_select_db($databaseName, $connection))
        showerror();
    
   
   
   // Start a query ...
   $query = "SELECT wine_id, wine_name, description, year, winery_name
             FROM   winery, region, wine
             WHERE  winery.region_id = region.region_id
             AND    wine.winery_id = winery.winery_id";
             
    // ... then, if the user has specified a region, add the regionName
    // as an AND clause
    if (isset($regionName) && $regionName !="All")
       $query .= " AND region_name = \"{regionName}\"";
       
       
    // ... and then complete the query
    $query .= " ORDER BY wine_name ";
    
    // run the query and show the results
    displayWinesList($connection, $query, $regionName);
    
    
    ?>
    </body>
    </html>

 

and this is the db.inc script, there's not much to it

 

<?php
   // This file is the same as example 6-7, but includes mysqlclean() and shellclean() 

   $hostName = "localhost";
   $databaseName = "winestore";
   $username = "like-i'd-tell-you";
   $password = "shhhh";

   function showerror()
   {
      die("Error " . mysql_errno() . " : " . mysql_error());
   }

   function mysqlclean($array, $index, $maxlength, $connection)
   {
     if (isset($array["{$index}"]))
     {
        $input = substr($array["{$index}"], 0, $maxlength);
        $input = mysql_real_escape_string($input, $connection);
        return ($input);
     }
     return NULL;
   }

   function shellclean($array, $index, $maxlength)
   {
     if (isset($array["{$index}"]))
     {
       $input = substr($array["{$index}"], 0, $maxlength);
       $input = EscapeShellArg($input);
       return ($input);
     }
     return NULL;
   }
?>

 

Thanks for your help

 

Link to comment
https://forums.phpfreaks.com/topic/180482-php-script-not-accessing-mysql/
Share on other sites

$connetion ain't spelled right in the line where you are assigning a value to it. You need to be developing and debugging php code in a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini to get php to help you. There would have been an undefined error at the first occurrence of $connection (spelled correctly) that would have alerted you to the fact that you had not assigned a value to a variable by that name and you could have backtracked to the line that is supposed to be assigning that value to discover the spelling error.

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.