中給出我正在爲我的投票製作投票系統。而我得到這個錯誤:mysql_num_rows()期望參數1是資源,布爾在給出。是看到有很多重複的答案。也許我在所有的代碼中都是盲目的,但我找不到錯誤。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;
}
?>
錯誤:
mysqli_real_escape_string()期望的是2個參數,1中給出: $ ID = mysqli_real_escape_String($ ID); ()); mysqli_num_rows()期望參數1爲mysqli_result,在 中給出的字符串$ count = mysqli_num_rows($ q);在
3:呼叫未定義的函數mysqli_result(): $行= mysqli_result($結果);
請,或不使用'mysql_ *'功能(http://stackoverflow.com/questions/12859942/why-shouldnt-i-use -mysql-functions-in-php),它們不再被維護,並且[正式被棄用](https://wiki.php.net/rfc/mysql_deprecation)。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。你還會想[防止SQL注入!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –
不需要mysql_query需要sql和連接var? oO –
你的連接到MySQL服務器在哪裏? –