Mysql行总是= 1

时间:2012-05-20 10:49:31

标签: php mysql sql

您好我正在尝试检查数据库中是否有一行,但是对于一些resson,它一直说有但是没有。我在第一侧有大量的staments,所以我认为这就是问题。

<?php
 if (isset($_POST['A'])) {
$item2= mysql_real_escape_string($_POST['A']);
$item = strip_tags($item2);

$pokemonchoice= mysql_real_escape_string($_POST['B']);
$pokemonchoicee = strip_tags($pokemonchoice);



?><?php  
 if ( $pokemonchoicee == "Add to pokemon 1" ) {
    $pokemonchoicee = '1';
}
 if ( $pokemonchoicee == "Add to pokemon 2" ) {
    $pokemonchoicee = '2';
}
 if ( $pokemonchoicee == "Add to pokemon 3" ) {
    $pokemonchoicee = '3';
}
 if ( $pokemonchoicee == "Add to pokemon 4" ) {
    $pokemonchoicee = '4';
} 
 if ( $pokemonchoicee == "Add to pokemon 5" ) {
    $pokemonchoicee = '5';
}
 if ( $pokemonchoicee == "Add to pokemon 6" ) {
    $pokemonchoicee = '6';
}

 ?>

 <?php


 $query = "SELECT * FROM items WHERE item='".$item."' AND belongsto='".$_SESSION['username']."'";
if(mysql_num_rows($query)!=0)
{
echo "You do not have this item";
}
else
{
echo "You have this item";
}

 ?>

 <?php
 echo $pokemonchoicee ;
 echo $item ;
 }

 ?>

保持打印为真的位是

 <?php


 $query = "SELECT * FROM items WHERE item='".$item."' AND belongsto='".$_SESSION['username']."'";
if(mysql_num_rows($query)!=0)
{
echo "You do not have this item";
}
else
{
echo "You have this item";
}

 ?>

我删除了所有行,但仍然说你有这个项目

2 个答案:

答案 0 :(得分:3)

mysql_num_rows()获取mysql结果类型资源,在mysql_num_rows之前使用$query = mysql_query($query)

答案 1 :(得分:0)

if语句中有错误。如果返回的行数是> 0,你打印“你做有这个项目”。它应该是相反的方式,所以更改!=检查或让回声切换位置