mysqli_query()期望参数1为mysqli,给定null

时间:2012-05-11 16:21:20

标签: php mysql

我正在尝试按照YouTube教程来构建CMS。我开始对它正在创建的错误消息感到困惑。我收到以下错误 '警告:mysqli_query()期望参数1为mysqli,在第7行的/home/a8241081/public_html/index.php中给出null;

我的代码是对的吗?应该有mysqli_select语句吗?

<?php
$db_host = "********";
$db_username = "******"; 
$db_pass = "*******"; 
$db_name = "********";
mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");  
?>

<?php
session_start();
require_once "scripts/connect_to_mysql.php";

//Build Main Navigation menu and gather page data here
$sqlCommand = "SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query = mysqli_query($myConnection,$sqlCommand) or die (mysqli_error($myConnection));

$menuDisplay='';
while ($row=mysqli_fetch_array($query)){
    $pid=$row["id"];
    $linklabel=$row["linklabel"];

    $menuDisplay .='<a href="index.php?pid=' . $pid . '">' . $linklabel . '</a><br/>';

  }
  mysqli_free_result($query);
  //mysqli_close($myConnection); 
?>

由于

1 个答案:

答案 0 :(得分:8)

你应该

$myConnection = mysqli_connect($db_host, $db_username, $db_pass, $db_name) or die ("could not connect to mysql");  

在第一个区块的末尾。

在您当前的代码中,$ myConnection未在此调用中定义:

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error($myConnection));