gpt4 book ai didi

php - 当数据库中找不到值时如何中断循环

转载 作者:行者123 更新时间:2023-11-29 21:20:01 25 4
gpt4 key购买 nike

<?php

$str = get_the_ID(); //this is getting id in wordpress index.php page
$value = 0;
$count = 0;
$sql1 = "SELECT * FROM wphg_mr_rating_item_entry where post_id='" . $str . "'";
$result1 = $wpdb->get_results($sql1) or die(mysql_error());

foreach ($result1 as $results1) {
//if(!empty($results1->rating_item_entry_id)) {
if (!$wpdb->show_errors($results1->rating_item_entry_id)) {

$sql = "SELECT * FROM wphg_mr_rating_item_entry_value where rating_item_entry_id='" . $results1->rating_item_entry_id . "'";
$result = $wpdb->get_results($sql) or die(mysql_error());

foreach ($result as $results) {
$sum = $results->value . "<br/>";
$sum1 = $results->value . "<br/>";
//$sum[]= array_push($sum,$value);
//echo count($sum+=$value);
//print_r($value);
$count+=count($sum1);
$value+= $sum;
}
} else {

break;
}
}


echo $value;
echo $count;
$value = $value / $count;
?>

当 $str ="this is id "时,我们使用根据匹配来获取数据。示例:就像您的 id = 26 一样,在表中找到数据,这工作正常。但如果在数据库中找不到您的帖子 ID 数据,一切都会变得困惑。

请帮助我,我想在数据库表中有数据时成功运行。但如果数据根据 id 不匹配,则循环中断并继续下一个 id 。

谢谢

最佳答案

<?php 
$str = get_the_ID() ;
$value=0;
$count=0;
$sql1="SELECT * FROM wphg_mr_rating_item_entry where post_id='".$str."'";
$sql2= $wpdb->get_row( $wpdb->prepare( "SELECT * FROM wphg_mr_rating_item_entry where post_id= %s",$str)); // take second condition here before reaching to loop and check your condition .
//echo $sql1->post_title;
if($sql2!=''){ // if value not equal to null then going for this logic working fine for me.

$result1 = $wpdb->get_results($sql1) or die(mysql_error());
//var_dump($result1);
foreach( $result1 as $results1 ){
//if(!empty($results1->rating_item_entry_id)) {
//if(!$wpdb->show_errors($results1->rating_item_entry_id )) {

$sql="SELECT * FROM wphg_mr_rating_item_entry_value where rating_item_entry_id='".$results1->rating_item_entry_id."'";
$result = $wpdb->get_results($sql) or die(mysql_error());

foreach( $result as $results){
$sum = $results->value."<br/>";
$sum1 = $results->value."<br/>";
//$sum[]= array_push($sum,$value);
//echo count($sum+=$value);
//print_r($value);
$count+=count($sum1);
$value+= $sum;
//}
}
}



//echo $value;
//echo $count;
$value=$value/$count;
}
?>

在到达循环之前先在此处获取第二个条件并检查您的条件。如果值不等于 null,那么这个逻辑对我来说工作正常。

关于php - 当数据库中找不到值时如何中断循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com