0

Possible Duplicate:
PHP: Warning: sort() expects parameter 1 to be array, resource given

here is my code: I see nothing that should be causing this...ideas?

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

 <?php

 include_once "mysql_connect.php";

 if ($_POST['parse_var'] == "contactform"){

 if(is_array($categories)) $whereCond = "in '".implode(",",$categories); else 
 $wherecond  = "= ".$categories;

       $dropdownValue = $_POST['dropdown'];

$dropdownValue = mysql_real_escape_string($dropdownValue);
$dropdownValue = eregi_replace("`", "", $dropdownValue);


$searchField= $_POST['searchinput'];
$searchField = mysql_real_escape_string($searchField);
$searchField = eregi_replace("`", "", $searchField);


if ($dropdownValue == "phone"){
$sql = mysql_query("SELECT * FROM pcparts WHERE phone='$searchField'");


    while($row1 = mysql_fetch_array($sql)){


        $arrayuserinfo[] = array(

            'phone' => $row1["phone"],
        'name' => $row1["name"],
        'city' => $row1["city"],
        'state' => $row1["state"],
        'address' => $row1["address"],
        'zip' => $row1["zip"],
    );
    };
                    for($i=0;$i < count($arrayuserinfo);$i++){

    $phone = $arrayuserinfo[$i]["phone"];
    $name = $arrayuserinfo[$i]["name"];
    $city = $arrayuserinfo[$i]["city"];
    $state = $arrayuserinfo[$i]["state"];
    $address = $arrayuserinfo[$i]["address"];
    $zip = $arrayuserinfo[$i]["zip"];
            echo"<table width='400' border='1' cellpadding='3'>
            <tr>
            <td>phone</td>
            <td>name</td>
            <td>address</td>
            <td>city</td>
            <td>state</td>
            <td>zip</td>
            </tr>
             <tr>
            <td>$phone</td>
            <td>$name</td>
             <td>$address</td>
             <td>$city</td>
            <td>$state</td>
           <td>$zip</td>
       </tr>
        </table><br />
        ";
        }
}

else if ($dropdownValue == "name"){
$sql = mysql_query("SELECT * FROM pcparts WHERE name='$searchField'");


    while($row1 = mysql_fetch_array($sql)){

        $arrayuserinfo[] = array(

        'phone' => $row1["phone"],
        'name' => $row1["name"],
        'city' => $row1["city"],
        'state' => $row1["state"],
        'address' => $row1["address"],
        'zip' => $row1["zip"],
    );
    };

        for($i=0;$i < count($arrayuserinfo);$i++){

    $phone = $arrayuserinfo[$i]["phone"];
    $name = $arrayuserinfo[$i]["name"];
    $city = $arrayuserinfo[$i]["city"];
    $state = $arrayuserinfo[$i]["state"];
    $address = $arrayuserinfo[$i]["address"];
    $zip = $arrayuserinfo[$i]["zip"];

    echo"<table width='400' border='1' cellpadding='3'>
        <tr>
        <td>phone</td>
        <td>name</td>
        <td>address</td>
         <td>city</td>
        <td>state</td>
        <td>zip</td>
        </tr>
         <tr>
        <td>$phone</td>
        <td>$name</td>
       <td>$address</td>
        <td>$city</td>
        <td>$state</td>
        <td>$zip</td>
       </tr>
       </table><br />
        ";
        }
}

else if ($dropdownValue == "city"){
    $sql = mysql_query("SELECT * FROM pcparts WHERE city='$searchField'");


    while($row1 = mysql_fetch_array($sql)){

        $arrayuserinfo[] = array(

        'phone' => $row1["phone"],
        'name' => $row1["name"],
        'city' => $row1["city"],
        'state' => $row1["state"],
        'address' => $row1["address"],
        'zip' => $row1["zip"],
    );
    };

        for($i=0;$i < count($arrayuserinfo);$i++){

    $phone = $arrayuserinfo[$i]["phone"];
    $name = $arrayuserinfo[$i]["name"];
    $city = $arrayuserinfo[$i]["city"];
    $state = $arrayuserinfo[$i]["state"];
    $address = $arrayuserinfo[$i]["address"];
    $zip = $arrayuserinfo[$i]["zip"];

    echo"<table width='400' border='1' cellpadding='3'>
     <tr>
     <td>phone</td>
     <td>name</td>
     <td>address</td>
     <td>city</td>
     <td>state</td>
     <td>zip</td>
     </tr>
       <tr>
    <td>$phone</td>
    <td>$name</td>
    <td>$address</td>
    <td>$city</td>
   <td>$state</td>
    <td>$zip</td>
    </tr>
    </table><br />
     ";
        }
}

      }
      ?>




     <html>
     <body>
      <h2>Customers Database Search</h2>

    <form action="file1.php" method="POST">
    <input type='hidden' name='parse_var' value='contactform'>
    Search fields:
 <select name="dropdown" id="dropdown" >
 <option value="<?php print "$dropdownValue"; ?>"><?php print "$dropdownValue"; ?></option>
  <option value="phone">phone</option>
  <option value="name">name</option>
  <option value="city">city</option>
</select>
 Search item:<input type="text" name='searchinput' id="searchinput" value="
 <?php print "$searchField"; ?>" size='20'><br><br>
 <input type="submit" name="button" id="button" value="Search" />
 </form>
 <br />
  <br />
 </body>
  </html>

mysql_connect:

   <?php

    $db_host = "localhost";
    $db_username = "user";

     $db_pass = "test1234";

    $db_name = "pcparts";

    @mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL");
    @mysql_select_db("$db_name") or die ("No database");

    ?>

if you find something i need to add, can you please tell me exactly where to put it?

Community
  • 1
  • 1
gregg
  • 1
  • 4
  • 2
    Why are you ignoring the potential errors raised by `mysql_connect` and others? – Mat Jul 17 '11 at 12:47
  • Your query failed and therefore it did not return anything that function which gives error can work with. That easy it is. You need to deal with the failures that might happen when code relies on functions which might fail. – hakre Jul 17 '11 at 12:49

4 Answers4

6

Your query failed, and you forgot to use mysql_error() to find out why.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
2

mysql_query returns FALSE when an error occurs. Make sure you deal with that situation before you try to use the $sql variable.

Mat
  • 195,986
  • 40
  • 382
  • 396
2

One of your queries is failing, or you never had a valid connection to begin with.

Remove the @ from the mysql_connect() and mysql_select_db() statements. Then after each call to mysql_query(), you need to check if it succeeded:

$sql = mysql_query("SELECT * FROM pcparts WHERE city='$searchField'");
if ($sql) {
  // query was valid -- fetch results.
}
else {
  // something went wrong.
  echo mysql_error();
}

Just a tip - you have piles and piles of unnecessary code copy/pasted. This whole block:

while($row1 = mysql_fetch_array($sql)){


        $arrayuserinfo[] = array(

            'phone' => $row1["phone"],
        'name' => $row1["name"],
        'city' => $row1["city"],
        'state' => $row1["state"],
        'address' => $row1["address"],
        'zip' => $row1["zip"],
    );
    };
                    for($i=0;$i < count($arrayuserinfo);$i++){

    $phone = $arrayuserinfo[$i]["phone"];
    $name = $arrayuserinfo[$i]["name"];
    $city = $arrayuserinfo[$i]["city"];
    $state = $arrayuserinfo[$i]["state"];
    $address = $arrayuserinfo[$i]["address"];
    $zip = $arrayuserinfo[$i]["zip"];
            echo"<table width='400' border='1' cellpadding='3'>
            <tr>
            <td>phone</td>
            <td>name</td>
            <td>address</td>
            <td>city</td>
            <td>state</td>
            <td>zip</td>
            </tr>
             <tr>
            <td>$phone</td>
            <td>$name</td>
             <td>$address</td>
             <td>$city</td>
            <td>$state</td>
           <td>$zip</td>
       </tr>
        </table><br />
        ";
        }

Can be simplified down to just this:

while($row1 = mysql_fetch_array($sql)) {
  echo"<table width='400' border='1' cellpadding='3'>
      <tr>
            <td>phone</td>
            <td>name</td>
            <td>address</td>
            <td>city</td>
            <td>state</td>
            <td>zip</td>
            </tr>
             <tr>
            <td>{$row1['phone']}</td>
            <td>{$row1['name']}</td>
             <td>{$row1['address']}</td>
             <td>{$row1['city']}</td>
            <td>{$row1['state']}</td>
           <td>{$row1['zip']}</td>
      </tr>
    </table><br />";
}

Another tip: Every time you enclose a variable in quotes like these:

mysql_select_db("$db_name")
<?php print "$searchField"; ?>

It's redundant and unnecessary. You just need

mysql_select_db($db_name)
<?php print $searchField; ?>
Michael Berkowski
  • 260,803
  • 45
  • 432
  • 377
0

Knowing which of the 3 mysql_fetch_array() calls is causing the warning would be helpful, as would knowing what value of $searchField is being passed, but looking at your code it suggests the SQL query you're forming is invalid. A failing query will return false instead of a mysql resource.

deefour
  • 34,168
  • 7
  • 95
  • 90