duanbu1421 2013-08-15 04:58
浏览 29
已采纳

php和mysql工作得很好,但是我收到了一个警告

I have been trying to figure this out and have been scouring Stackoverflow and other sites for anything related to my problem.

I have a php script that is updating a mysql table as people open the respective page. The whole thing works fine (as I have been having it print text each step of the way to verify that the data is there. The update is placing either the url and other data into the table if the page has never been visited or updating the total_votes and current_vote per visit. The total_votes is for a statistical reporting used on a different page. The current_vote diminishes in value each week.

For some reason I am getting a Warning. I figure that it is better to ask despite the whole thing working as I hate Warnings and errors that I can't resolve (even if there is no problems with the end result.

The database is set to utf8_general_ci so the url information shouldn't be an issue on that end.

Here is the chunk of code that I have for this section (the Warning is on 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());

}
}
}

?>

The warning that I have been getting is: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...

  • 写回答

1条回答 默认 最新

  • dongwen3437 2013-08-15 05:14
    关注
    <?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());
    
    }
    }
    }
    
    ?>
    

    if you haven't notice i change the first variable $query to $first_query.. to avoid change in out. the loop get confuse with the repeated use of $query inside the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)