将mysql转换为mysqli

时间:2015-09-24 11:00:33

标签: php mysql mysqli

我有一个在mysql中的代码,我想将其更改为mysqli,我试过但行$result = mysqli_query($query) or die(mysqli_error()."[".$query."]");中有错误错误是在期待参数,代码中可能会有更多错误,因为我mysqli新手。 Mysql代码工作正常。

mysql

   <form action="" method="post">
   <label>Category:</label>
   <?php
   $query = "SELECT * FROM categories";
   $result = mysql_query($query) or die(mysql_error()."[".$query."]");
   ?>
   <select name="category">
   <?php 
   while ($row = mysql_fetch_array($result))
   {
   echo "<option value='".$row['category_id']."'>".$row['category']." </option>";
    }
   ?>        
   </select>

mysqli

  <form action="" method="post">
  <label>Category:</label>
  <?php
  $query = "SELECT * FROM categories";
  $result = mysqli_query($query) or die(mysqli_error()."[".$query."]");
  ?>
  <select name="category">
  <?php 
  $row = mysqli_fetch_array($result);
  {
  echo "<option value='".$row['category_id']."'>".$row['category']."</option>";
  }
  ?>        
  </select>

2 个答案:

答案 0 :(得分:1)

mysqli_error()需要一个链接。 ... mysqli_connect()或mysqli_init()

返回的链接标识符

答案 1 :(得分:0)

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

$sql="SELECT * FROM categories";
$result=mysqli_query($con,$sql);
?> 
<select name="category">
<?php 
 $row = mysqli_fetch_array($result);
 {
    echo "<option value='".$row['category_id']."'>".$row['category']."</option>";
 }
 ?>        
 </select>

mysqli_*函数需要两个参数。