mysql_num_rows()期望参数1是资源,布尔值是给定的

时间:2014-11-17 13:58:27

标签: php mysql

我正在为我的galleri制作一个投票系统。我得到这个错误:mysql_num_rows()期望参数1是资源,给定布尔值。看到有很多重复的答案。也许我已经在所有代码中失明了,但我找不到错误。

  <?php
   $ip = $_SERVER['REMOTE_ADDR']; 

    if($_POST['id'])
  {
     $id = $_POST['id'];
   $id = mysql_escape_String($id);

      $ip_sql = mysql_query("SELECT ip_add FROM voting_ip WHERE id_fk='$id' AND ip_add='$ip'");
     $count = mysql_num_rows($ip_sql);

   if($count==0)
     {
 // Update
  $sql = "UPDATE votes SET up=up+1 WHERE id='$id'";
 mysql_query( $sql);
   // Insert IP address and Message Id in Voting_IP table.
  $sql_in = "INSERT INTO voting_ip (id_fk,ip_add) VALUES ('$id','$ip')";
    mysql_query( $sql_in);
    echo "<script>alert('Thanks for the vote');</script>";
   }
  else
  {
   echo "<script>alert('You have already voted');</script>";
   }

 $result = mysql_query("SELECT up FROM votes WHERE id='$id'");
 $row = mysql_fetch_array($result);
 $up_value=$row['up'];
 echo $up_value;

  }
  ?>

新代码:

<?php

    include ('config/connection.php'); 

    $ip = $_SERVER['REMOTE_ADDR']; 

    if($_POST['id'])
    {
    $id = $_POST['id'];
    $id = mysqli_real_escape_String($id);
    //Verify IP address in Voting_IP table
    $q = "SELECT ip_add FROM voting_ip WHERE id_fk='$id' AND ip_add='$ip'";
    $r = mysqli_query($dbc, $q);
    $count = mysqli_num_rows($q);

    if($count==0)
    {
    // Update Vote.
    $sql = "UPDATE votes SET up=up+1 WHERE id='$id'";
    // Insert IP address and Message Id in Voting_IP table.
    $sql_in = "INSERT INTO voting_ip (id_fk,ip_add) VALUES ('$id','$ip')";
    echo "<script>alert('Thanks for the vote');</script>";
    }
    else
    {
    echo "<script>alert('You have already voted');</script>";
    }

    $result = "SELECT up FROM votes WHERE id='$id'";
    $row = mysqli_result($result);
    $up_value=$row['up'];
    echo $up_value;

    }
 ?>

错误:

  1. mysqli_real_escape_string()需要2个参数,1个给出: $ id = mysqli_real_escape_String($ id);

  2. :mysqli_num_rows()期望参数1为mysqli_result,字符串在 $ count = mysqli_num_rows($ q);

  3. 3:在以下位置调用未定义的函数mysqli_result(): $ row = mysqli_result($ result);

1 个答案:

答案 0 :(得分:0)

首先需要创建与数据库的连接,使用mysql_connect()执行此操作,如下所示:

$con = mysql_connect(DATABASE, USERNAME, PASSWORD, TABLE);

然后在进行查询时传递此连接。

同时将所有函数调用更改为mysqli _ *