简体   繁体   English

这个MySQL-PHP代码有什么问题?

[英]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 产生的错误是:警告:mysql_fetch_array():提供的参数不是有效的MySQL结果资源

I think you can't feed a mysqli resource to a mysql function. 我认为您无法将mysqli资源提供给mysql函数。 Try turning mysql_fetch_array(...) into mysqli_fetch_array(...) . 尝试将mysql_fetch_array(...)转换为mysqli_fetch_array(...)

You don't need to pass the mysqli as a resource. 您不需要将mysqli作为资源传递。 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. 在此示例中,$ result是一个mysqli_resultset对象。 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. 有关所有选择,请查看PHP手册。 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: 如果您需要经常使用此例程,则可以从mysqli_result基类派生一个类,并从代码中创建一个成员函数,例如:

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. 我添加了一些额外的代码,以使您对mysqli函数有更好的了解。 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. 我的小例程将光标移动到集合的第二个字段,并将其与函数的name参数进行比较。mysqli类具有许多不同的函数,就像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. 只是根本不使用mysql。 it is really obsolete and probably should be deprecated in favor of the true OO style in the "improved" version. 它确实已经过时了,应该在“改进”版本中不赞成使用真正的OO样式。 The i in mysqli stands for improved. mysqli中的i代表改进。 It makes use of the newer PHP 5 object model. 它利用了更新的PHP 5对象模型。 When you get used to the OO syntax you'll not want to revert to the old procedural style. 当您习惯了OO语法时,您将不希望恢复到旧的过程样式。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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