2

我正在尝试按照 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);

$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); 
?>
4

1 回答 1

9

您应该将结果分配mysqli_connect()给一个变量:

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

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

$query = mysqli_query($myConnection, $sqlCommand);
于 2012-05-11T16:22:58.513 回答