php和mysql运行得很好,但我收到了一个警告


The php and mysql work perfectly, but I am getting a WARNING

我一直在努力解决这个问题,并在Stackoverflow和其他网站上搜索与我的问题有关的任何内容。

我有一个php脚本,当人们打开相应的页面时,它正在更新mysql表。整件事都很好(因为我一直让它在验证数据是否存在的每一步都打印文本。更新是在从未访问过页面的情况下将url和其他数据放入表中,或者每次访问都更新total_votes和current_vote。total_vates用于不同页面上使用的统计报告。current_votes的值每周都会减少。

出于某种原因,我收到了警告。我认为,尽管整件事都在起作用,但最好还是问一下,因为我讨厌警告和我无法解决的错误(即使最终结果没有问题)。

数据库设置为utf8_general_ci,因此url信息在这方面不应该是一个问题。

以下是我为本节准备的代码块(警告位于while($row=mysqli_fetch_array($query)){

<?php
function curPageURL() {
$pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$cururl = curPageURL();
echo $cururl;

require_once "scripts/connect.php";
$sqlCommand = "SELECT Count(*) as `Counter` FROM `hot_topics` WHERE `article_url` = '". $cururl ."'";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());

while ($row = mysqli_fetch_array($query)){
    $Count = $row["Counter"];
echo $Count;

if ($Count == 1) {
echo '1';
require_once "scripts/connect.php";
$sqlCommand = "UPDATE `hot_topics` SET `total_votes` = `total_votes`+1, `current_vote` = `current_vote`+1 WHERE `article_url` = '". $cururl ."'";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
} elseif ($Count == '0') {
echo '2';
$yearHead=$_GET["yearrange"];
$weekHead=$_GET["weekrange"];
if ($weekHead == 'Current') {
$type=$_GET["type"];
$yearHead = date("Y");
$weekHead = date("W");
$Startdate = new DateTime();
$Startdate->setISODate($yearHead, $weekHead);
$Enddate = new DateTime();
$Enddate->setISODate($yearHead, $weekHead, 7);
require_once "scripts/connect.php";
$sqlCommand = "INSERT INTO `hot_topics` (`total_votes`, `current_vote`, `article_url`, `article_title`) VALUES ('1', '1', '". $cururl ."', '". $type ." Chart: ". $Startdate->format('d/F/Y') ." - ". $Enddate->format('d/F/Y') ."')";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
} elseif ($weekHead == 'All') {
$type=$_GET["type"];
require_once "scripts/connect.php";
$sqlCommand = "INSERT INTO `hot_topics` (`total_votes`, `current_vote`, `article_url`, `article_title`) VALUES ('1', '1', '". $cururl ."', '". $type ." Chart: All Weeks')";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
} else {
$type=$_GET["type"];
$yearHead=$_GET["yearrange"];
$weekHead=$_GET["weekrange"];
echo $yearHead;
echo $weekHead;
$Startdate = new DateTime();
$Startdate->setISODate($yearHead, $weekHead);
$Enddate = new DateTime();
$Enddate->setISODate($yearHead, $weekHead, 7);
require_once "scripts/connect.php";
$sqlCommand = "INSERT INTO `hot_topics` (`total_votes`, `current_vote`, `article_url`, `article_title`) VALUES ('1', '1', '". $cururl ."', '". $type ." Chart: ". $Startdate->format('d/F/Y') ." - ". $Enddate->format('d/F/Y') ."')";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
}
}
}
?>

我收到的警告是:警告:mysqli_fetch_array()要求参数1为mysqli_result,布尔值在…中给定

<?php
function curPageURL() {
$pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
   $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$cururl = curPageURL();
echo $cururl;

require_once "scripts/connect.php";
$sqlCommand = "SELECT Count(*) as `Counter` FROM `hot_topics` WHERE `article_url` = '".   $cururl ."'";
$first_query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error()); 

while ($row = mysqli_fetch_array($first_query)){
    $Count = $row["Counter"];
echo $Count;

if ($Count == 1) {
echo '1';
require_once "scripts/connect.php";
$sqlCommand = "UPDATE `hot_topics` SET `total_votes` = `total_votes`+1, `current_vote`     = `current_vote`+1 WHERE `article_url` = '". $cururl ."'";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
} elseif ($Count == '0') {
echo '2';
$yearHead=$_GET["yearrange"];
$weekHead=$_GET["weekrange"];
if ($weekHead == 'Current') {
$type=$_GET["type"];
$yearHead = date("Y");
$weekHead = date("W");
$Startdate = new DateTime();
$Startdate->setISODate($yearHead, $weekHead);
$Enddate = new DateTime();
$Enddate->setISODate($yearHead, $weekHead, 7);
require_once "scripts/connect.php";
$sqlCommand = "INSERT INTO `hot_topics` (`total_votes`, `current_vote`, `article_url`, `article_title`) VALUES ('1', '1', '". $cururl ."', '". $type ." Chart: ". $Startdate->format('d/F/Y') ." - ". $Enddate->format('d/F/Y') ."')";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
} elseif ($weekHead == 'All') {
$type=$_GET["type"];
require_once "scripts/connect.php";
$sqlCommand = "INSERT INTO `hot_topics` (`total_votes`, `current_vote`, `article_url`, `article_title`) VALUES ('1', '1', '". $cururl ."', '". $type ." Chart: All Weeks')";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
} else {
$type=$_GET["type"];
$yearHead=$_GET["yearrange"];
$weekHead=$_GET["weekrange"];
echo $yearHead;
echo $weekHead;
$Startdate = new DateTime();
$Startdate->setISODate($yearHead, $weekHead);
$Enddate = new DateTime();
$Enddate->setISODate($yearHead, $weekHead, 7);
require_once "scripts/connect.php";
$sqlCommand = "INSERT INTO `hot_topics` (`total_votes`, `current_vote`, `article_url`,    `article_title`) VALUES ('1', '1', '". $cururl ."', '". $type ." Chart: ". $Startdate->format('d/F/Y') ." - ". $Enddate->format('d/F/Y') ."')";
$query = mysqli_query($myconnection,$sqlCommand) or die (mysqli_error());
}
}
}
?>

如果您没有注意到,我将第一个变量$query更改为$first_query。。以避免变化。循环与循环中重复使用$query混淆。