以下是代码段:
$check = mysql_query("SELECT occurrence_number FROM occurrence WHERE word_id = '$word_id' AND page_id = '$page_id'");
$array_row = mysql_fetch_array($check);
if($array_row['occurrence_number']){
mysql_query("UPDATE occurrence SET occurrence_number = occurrence_number +1 WHERE word_id = '$word_id'");
print "Updating";
}else{
mysql_query("INSERT INTO occurrence (word_id,page_id) VALUES ($word_id,$page_id)");
print "Indexing: $cur_word<br>";
}
我希望查询执行的操作:
如果在表occurrence
中,存在word_id ='$word_id'
和page_id = '$page_id'
的行,则在向其添加1之后更新该行的occurrence_number
列。
执行此查询时我得到的是:
代码永远不会输入if
子句。每次执行代码时,都会生成一个新行,其中word_id
,page_id
,默认值occurrence_number
(设置为0
)和{{1} }(自动递增和主键)。
答案 0 :(得分:1)
尝试 is_numeric (),如
if(is_numeric($array_row['occurrence_number'])){
mysql_query("UPDATE occurrence SET occurrence_number = occurrence_number +1 WHERE word_id = '$word_id'");
print "Updating";
}else{
mysql_query("INSERT INTO occurrence (word_id,page_id) VALUES ($word_id,$page_id)");
print "Indexing: $cur_word<br>";
}
或者您可以尝试
if($array_row['occurrence_number'] || $array_row['occurrence_number'] == '0'){
如果您尝试使用带有while循环的 mysql_fetch_array ,那么主要的是它将通过所有相关记录,否则它将只返回一条记录
while($array_row = mysql_fetch_array($check)) {
//Put here the code...if else
}
并且不使用mysql_ *函数,因为它们被删除。相反,您可以使用 mysqli _ *或 PDO 语句
答案 1 :(得分:1)
这是因为MYSQL返回超过1行。 尝试使用 mysql_fetch_row 而不是 mysql_fetch_array 。
或者就这样做:
while($my_fetch_var = mysql_fetch_array($check))
{ your if clause }
P.S。:还试着检查,你有多少行:
echo count($array_row);
答案 2 :(得分:0)
我会检查row
是否存在而不是检查实际值
$check = mysql_query("SELECT occurrence_number FROM occurrence WHERE word_id = '$word_id' AND page_id = '$page_id'");
if($array_row = mysql_fetch_array($check))
{
mysql_query("UPDATE occurrence SET occurrence_number = occurrence_number +1 WHERE word_id = '$word_id'");
print "Updating";
}else{
mysql_query("INSERT INTO occurrence (word_id,page_id) VALUES ($word_id,$page_id)");
print "Indexing: $cur_word<br>";
}
请注意,mysql_*
函数已弃用,最好使用mysqli
或PDO
答案 3 :(得分:0)
if( $array_row['occurrence_number'] == '0' || $array_row['occurrence_number'] ){
while($array_row = mysql_fetch_array($check)) {
mysql_query("UPDATE occurrence SET occurrence_number = occurrence_number +1 WHERE word_id = '$word_id'");
print "Updating";
}