我有一个脚本来从数据库中获取信息,并使用while循环来检查它是否符合某些条件。第一个条件总是满足,但第二个条件不满足。
这里是我使用的代码:While循环不满足这两个条件
//Get the new ad's id
$stmt = mysqli_prepare($db, "SELECT visits, description, url, views, time FROM paidAds WHERE id=? AND finished!=?");
//Bind Items
mysqli_stmt_bind_param($stmt, 'ii', $currentAd, $adFinished);
//Execute statement
mysqli_stmt_execute($stmt);
//Bind password to variable
mysqli_stmt_bind_result($stmt, $adVisits, $adDescription, $adUrl, $adViews, $adTime);
//Fetch password
mysqli_stmt_fetch($stmt);
//Close statement
mysqli_stmt_close($stmt);
//Get the new ad's id
$stmt2 = mysqli_prepare($db, "SELECT ip FROM adViews WHERE adId=? AND address=?");
//Bind Items
mysqli_stmt_bind_param($stmt2, 'is', $currentAd, $address);
//Execute statement
mysqli_stmt_execute($stmt2);
//Bind password to variable
mysqli_stmt_bind_result($stmt2, $userIp);
//Fetch password
mysqli_stmt_fetch($stmt2);
//Close statement
mysqli_stmt_close($stmt2);
if($stmt2 === false){
echo mysqli_error($db);
}
while($adDescription == '' && $userIp != ''){
$currentAd += 1;
//Get the new ad's id
$stmt = mysqli_prepare($db, "SELECT visits, description, url, views, time FROM paidAds WHERE id=? AND finished!=?");
//Bind Items
mysqli_stmt_bind_param($stmt, 'ii', $currentAd, $adFinished);
//Execute statement
mysqli_stmt_execute($stmt);
//Bind password to variable
mysqli_stmt_bind_result($stmt, $adVisits, $adDescription, $adUrl, $adViews, $adTime);
//Fetch password
mysqli_stmt_fetch($stmt);
//Close statement
mysqli_stmt_close($stmt);
//Get the new ad's id
$stmt2 = mysqli_prepare($db, "SELECT ip FROM adViews WHERE adId=? AND address=?");
//Bind Items
mysqli_stmt_bind_param($stmt2, 'is', $currentAd, $address);
//Execute statement
mysqli_stmt_execute($stmt2);
//Bind password to variable
mysqli_stmt_bind_result($stmt2, $userIp);
//Fetch password
mysqli_stmt_fetch($stmt2);
//Close statement
mysqli_stmt_close($stmt2);
if($userIp != ''){
echo 'error';
}
}
echo $userIp;
这个脚本的输出不显示任何“错误的,因为它应该,但打印变量$ USERIP当它是不是空的。
这是为什么?
为什么你要在循环内进行循环之前做同样的查询?必须有一个'JOIN'查询,你可以这样做,以防止你正在尝试的4个查询。 – Sean
第一个查询是获取循环中要检查的初始变量。我会研究JOIN查询,谢谢。 – Minifrij