php error warning

Problems with the Windows version of XAMPP, questions, comments, and anything related.

php error warning

Postby turner66 » 06. June 2012 16:43

I get these errors Warning: mysql_fetch_field() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php1\htdocs\search.php on line 44

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php1\htdocs\search.php on line 49
when I run my code
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/xml; charset=utf-8" />
    <title>search.php</title>
    <style type = "text/css">
      table, th, td {
        border: 1px solid black;
      }
    </style>
  </head>

  <body>
    <h1>My Contacts</h1>
  <?php
 
  $sql = processInput();
  printResults($sql);
 
  function processInput(){
   
    $srchVal = $_POST["srchVal"];
    $srchField = $_POST["srchField"];

    $conn = mysql_connect("localhost", "root", "apollo66");
   
   
    $sql = "SELECT * FROM contact WHERE $srchField LIKE '%$srchVal%'";
    return $sql;
     
  }
 
  function printResults($sql){
    $conn = mysql_connect("localhost", "root", "apollo66");
    mysql_select_db("sb");

    $result = mysql_query($sql, $conn);

    print "  <table> \n";

   
    print "      <tr> \n";
    while ($field = mysql_fetch_field($result)){
      print "        <th>$field->name</th> \n";
    }
    print "      </tr> \n";

    while ($row = mysql_fetch_assoc($result)){
      print "      <tr> \n";
      foreach ($row as $name => $value){
        print "        <td>$value</td> \n";   
      }
     
      print "      </tr> \n";

    }

    print "    </table> \n";
  }   
  ?>
  </body>
</html>


Any Idea what the problem is and how I can fix it?
turner66
 
Posts: 1
Joined: 06. June 2012 16:28
Operating System: Windows XP

Re: php error warning

Postby Altrea » 06. June 2012 16:56

Hi turner66,

1st:
Wrong forum. I have moved your thread to the correct place. Next time take some minutes to orientate yourself at this board first.

2nd:
Your headline is far away from being self speaking. There are hundreds of php error warnings you can get.

3rd
There is always time for an introducing welcome line if you start a new thread, especially if you want to get help at a community board. It's a matter of politeness.

back to your problem.

Thats your problem lines:
Code: Select all
//...
while ($field = mysql_fetch_field($result)){
//...
while ($row = mysql_fetch_assoc($result)){


Ask yourself:
- How can parameter1 ($result) can be an boolean? (var_dump() can help you to find out which boolean it is)
- where does $return come from and what does that function it comes from return in which situations? (on php.net you can find all core php functions and their return values)

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8286
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 45 guests