mysqli_query()需要至少2个参数,给定1个

时间:2015-04-19 06:16:54

标签: php mysql mysqli

我尝试将下面的原始代码转换为mysql,但我收到此错误: mysqli_query()预计至少有2个参数,1给定

<?php 

$connection = mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("database1", $connection) or die(mysql_error());

function loop_array($array = array(), $parent_id = 0)

{
  if (!empty($array[$parent_id])) {
      echo '<ul>';
      foreach ($array[$parent_id] as $items) {
          echo '<li>';
          echo '<a href="?page='.$items['id'].'">'.$items['title'].'</a>';
          loop_array($array, $items['id']);
          echo '</li>';
      }
          echo '</ul>';
  }
}

function displays_menus_revised()

{
  $sql = "SELECT * FROM pages";
  $query = mysql_query($sql) or die(mysql_error());
  $array = array();
  if (mysql_num_rows($query)) {
    while ($rows = mysql_fetch_array($query)) {
      $array[$rows['parent_id']][] = $rows;
    }
    loop_array($array);
  }
}

 ?>

以下是我转换为 mysqli 。我尝试将$connection放入mysqli_query($connection, $sql),但仍然出错。

<?php 

$connection = mysqli_connect("localhost", "root", "password", "database1") or die(mysqli_connect_error());
mysqli_select_db($connection, "database1") or die(mysqli_connect_error());

function loop_array($array = array(), $parent_id = 0)

{
  if (!empty($array[$parent_id])) {
      echo '<ul>';
      foreach ($array[$parent_id] as $items) {
          echo '<li>';
          echo '<a href="?page='.$items['id'].'">'.$items['title'].'</a>';
          loop_array($array, $items['id']);
          echo '</li>';
      }
      echo '</ul>';
  }
}

function displays_menus_revised()

{
  $sql = "SELECT * FROM pages";
  $query = mysqli_query($sql) or die(mysqli_connect_error());
  $array = array();
  if (mysqli_num_rows($query)) {
    while ($rows = mysqli_fetch_array($query)) {
      $array[$rows['parent_id']][] = $rows;
    }
    loop_array($array);
  }

}

 ?>

4 个答案:

答案 0 :(得分:0)

更改您的mysqli连接:

$connection = mysqli_connect("localhost", "root", "password", "database1") or die(mysqli_connect_error());
mysqli_select_db($connection, "database1") or die(mysqli_connect_error());

为:

$connection = mysqli_connect("localhost", "root", "password", "database1") or die(mysqli_connect_error($connection));

比以下更改查询:

$query = mysqli_query($sql) or die(mysqli_connect_error());

为:

$query = mysqli_query($sql, $connection) or die(mysqli_connect_error($connection));

答案 1 :(得分:0)

mysqli_query需要两个参数,其中1是链接标识符,它是您的数据库连接,第二个是将在提供的数据库中执行的查询。

$connection = mysqli_connect("localhost", "root", "password", "database1") or die(mysqli_connect_error());

function loop_array($array = array(), $parent_id = 0)
{
  if (!empty($array[$parent_id])) {
      echo '<ul>';
      foreach ($array[$parent_id] as $items) {
          echo '<li>';
          echo '<a href="?page='.$items['id'].'">'.$items['title'].'</a>';
          loop_array($array, $items['id']);
          echo '</li>';
      }
      echo '</ul>';
  }
}

function displays_menus_revised()
{
  $sql = "SELECT * FROM pages";
  $query = mysqli_query($connection,$sql) or die(mysqli_connect_error());
  $array = array();
  if (mysqli_num_rows($query)) {
    while ($rows = mysqli_fetch_array($query)) {
      $array[$rows['parent_id']][] = $rows;
    }
    loop_array($array);
  }

}

答案 2 :(得分:0)

尝试使用$ result = $ connection-&gt;查询($ sql)而不是mysqli_query。然后你可以使用其他方法来获取结果,例如$ results-&gt; fetch_assoc()。不确定它是否会对你有所帮助,但我认为它可能会有所帮助。

答案 3 :(得分:0)

感谢您的建议,我修改了我的代码,这就是解决方案:

我在第二个函数变量$ connection中声明为global,

global $connection;

然后我在$ query = mysqli_query($ sql)

中放置了连接
$query = mysqli_query($connection, $sql) OR die(mysqli_connect_error());

完整的代码现在正在运行:

<?php 


    $connection = mysqli_connect('localhost', 'admin', 'nimica', 'ipip') OR die('Could not connect because: '.mysqli_connect_error());

    function loop_array($array = array(), $parent_id = 0)

    {
      if (!empty($array[$parent_id])) {
          echo '<ul>';
          foreach ($array[$parent_id] as $items) {
              echo '<li>';
              echo '<a href="?page='.$items['id'].'">'.$items['title'].'</a>';
              loop_array($array, $items['id']);
              echo '</li>';
          }
          echo '</ul>';
      }
    }

    function displays_menus_revised()

    { 
      global $connection;
      $sql = "SELECT * FROM pages";
      $query = mysqli_query($connection, $sql) OR die(mysqli_connect_error());
      $array = array();
      if (mysqli_num_rows($query)) {
        while ($rows = mysqli_fetch_array($query)) {
          $array[$rows['parent_id']][] = $rows;
        }
        loop_array($array);
      }
    }

     ?>