0

我一直在试图解决这个问题,并且一直在搜索 Stackoverflow 和其他网站以查找与我的问题相关的任何内容。

我有一个 php 脚本,当人们打开相应的页面时,它正在更新一个 mysql 表。整个事情工作正常(因为我一直让它在每一步都打印文本以验证数据是否存在。如果从未访问过页面或更新total_votes 和 current_vote 每次访问。total_votes 用于在不同页面上使用的统计报告。current_vote 的值每周都会减少。

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

数据库设置为 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,布尔值在...

4

1 回答 1

0
<?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 混淆。

于 2013-08-15T05:14:28.367 回答