简体   繁体   中英

What's wrong in this MySQL - PHP code?

$mysqli = new mysqli("localhost", "user", "pass", "db");

$sql = "SELECT OrgNo, CompanyName 
        FROM ematch 
        WHERE CompanyName LIKE '%A%'";

$result = mysqli_query($mysqli, $sql);

while ($row = mysql_fetch_array($result)) 
{
     print $row['OrgNo'] .'<br />';
     print $row['CompanyName'] .'<br />';
}

Error produced is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

I think you can't feed a mysqli resource to a mysql function. Try turning mysql_fetch_array(...) into mysqli_fetch_array(...) .

You don't need to pass the mysqli as a resource. Use it as an object and invoke it's query method like this:

$mysqli = new mysqli("localhost", "user", "pass", "db");

$sql = "SELECT OrgNo, CompanyName 
    FROM ematch 
    WHERE CompanyName LIKE '%A%'";

$result = $mysqli->query($sql);

while (($row=$result->fetch_assoc())!=false) 
{
    print $row['OrgNo'] .'<br />';
    print $row['CompanyName'] .'<br />';
}

$result in this example is a mysqli_resultset object. There are many functions to give you these results in various formats like objects or numeric/associative arrays. check the PHP manual for all the choices. If you need to use this routine often, you can derive a class from the mysqli_result base and make a member function out of your code like:

class MyResult extends mysqli_result
{
   public function company_info($name=null)
   {
      if ($name==null)
      {
         while (($row=$result->fetch_assoc())!=false) 
         {
            print $row['OrgNo'] .'<br />';
            print $row['CompanyName'] .'<br />';
         }
      }
      else
      {
          while (($row=$result->fetch_assoc())!=false) 
         {
            $result->field_seek(1);
            if($result->fetch_field==$name)
            {
               print $row['OrgNo'] .'<br />';
               print $row['CompanyName'] .'<br />';
            }
         }
      }
   }
}

I threw in some extra code to give you a better feel for the mysqli functions. My little routine moves the cursor to the second field in the set and compares it to the function's name parameter.The mysqli class has many different functions just like the mysqli_resultset. Don't think of the result as a resource. It is member data in an object and is accessed through it's methods. Remember, load in your object data then manipulate it through it's member functions. If you need a function that isn't predefined, just derive and add it to the class. Just Dont use mysql at all. it is really obsolete and probably should be deprecated in favor of the true OO style in the "improved" version. The i in mysqli stands for improved. It makes use of the newer PHP 5 object model. When you get used to the OO syntax you'll not want to revert to the old procedural style. Access the derived class like this:

 $i_am_an_object = new MyResult; //Make The result a MyResult instead of just a mysqli_result
 $i_am_an_object = $mysqli->query($sql); //populate the MyResult object with a base class function
 $i_am_an_object->company_info(); //call your function to print fields from each record in the result\
 $i_am_an_object->company_info("xyz Company"); //checks the second field in each record for the $name parameter and prints fields for any matching records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM