php - PHP While 循环的非常奇怪的问题
问题描述
我正在检查并从 MYSQL 表中获取数据,如果结果大于 0,我正在使用 while 循环处理它。代码如下
<?php
ini_set('max_execution_time', '0');
ini_set('max_input_vars', '100000');
ini_set('memory_limit', '-1');
include_once('includes/connection.php');
//include('bapu.php');
date_default_timezone_set('Asia/Kolkata');
$current_time = date("Y-m-d H:i:s", time());
$post_query = "SELECT * FROM tbl_posts LEFT JOIN tbl_users ON tbl_posts.post_user = tbl_users.id WHERE post_status = 1 AND post_time < '$current_time'";
echo $post_query;
echo "<br>";
$result = mysqli_query($mysqli,$post_query);
$row_cnt = mysqli_num_rows($result);
if($row_cnt<1){
echo "no data";
exit();
}
$i =0;
while($row = mysqli_fetch_array($result)){
$i++;
$pid = $row['pid'];
echo date("Y-m-d H:i:s", time());
echo "<br>";
$post_user = $row['post_user'];
$title = $row['post_title'];
$language = $row['post_language'];
$hashtags =$row['post_tags'];
$genre = $row['post_category'];
$subGenre = $row['post_sub_category'];
$post_tags = $row['post_tags'];
$image = $row['post_image'];
$dh_user_id = $row['dh_user_id'];
$login_status = $row['login_status'];
$login_expiry = $row['login_expiry'];
$cookie = $row['cookie'];
$mobile = $row['dh_mobile'];
if($login_status!=1 || strtotime($login_expiry) <= time()){
$q = "UPDATE tbl_users SET login_status=0, login_expiry = '00-00-00 00:00:00'";
$r = mysqli_query($mysqli,$q);
//EMAIL TO USER FOR ERROR OF LOGIN
}else{
$dir = "uploads/".$post_user;
$output="";
$variableAry=explode(",",$hashtags); //you have array now
$total = count($variableAry);
foreach($variableAry as $i=>$var)
{
$start = '{"title":"';
$end = '","id":""},';
if(($i+1)==$total){
$end = '","id":""}';
}
$output.=$start.$var.$end;
}
$hashtags = $output;
$userId =$dh_user_id;
$cookie = 'Cookie: '.$cookie;
$img = $dir.'/'.$image;
file_put_contents(time().'.txt',$img);
//:: FIRST STEP :://
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com/api/image?isFile=true',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST=> 0,
CURLOPT_VERBOSE=> 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('imageFile'=> new CURLFILE("$img")),
CURLOPT_HTTPHEADER => array(
"'$cookie'"
),
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close($curl);
$json = json_decode($response);
$str = explode("#/", $json->fileName)[1];
$height = $json->height;
$width = $json->width;
$str = explode("#/", $json->fileName)[1];
$format = explode(".", $json->fileName)[1];
$height = $height;
$width = $width;
$url =$str;
$format =$format;
$data = '{"post":{"fields":{"icon":{"height":'.$height.',"width":'.$width.',"url":"'.$url.'","id":"'.$url.'","format":"'.$format.'"},"image":{"height":'.$height.',"width":'.$width.',"url":"'.$url.'","id":"'.$url.'","format":"'.$format.'"},"title":"'.$title.'","hashtags":['.$hashtags.']},"locations":[""],"language":"'.$language.'","type":"IMAGE","tags":{"dhTags":{"genre":["'.$genre.'"],"subGenre":["'.$subGenre.'"]}},"ttl":{"id":"2","name":"Infinite","type":"NORMAL","value":"31536000"},"action":"submit","postId":null,"updatePublishedDate":false},"userId":'.$userId.'}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api/post/upsert/update');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$data");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0';
$headers[] = 'Accept: application/json, text/plain, */*';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Content-Type: application/json;charset=utf-8';
$headers[] = 'Origin: https://dhcreator.dailyhunt.in';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Referer: https://example.com/app/post/create?type=IMAGE';
$headers[] = "$cookie";
$headers[] = 'Sec-Fetch-Dest: empty';
$headers[] = 'Sec-Fetch-Mode: cors';
$headers[] = 'Sec-Fetch-Site: same-origin';
$headers[] = 'Te: trailers';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$json = json_decode($result, true);
$status = $json['data']['success'];
if($status){
$q = "UPDATE tbl_posts SET post_status = 0 WHERE pid = $pid";
$r = mysqli_query($mysqli,$q);
}
echo "OK ".$status;
}
sleep(10);
}
echo "I:".$i;
?>
它仅适用于一行而不处理其他行,我的意思是先循环停止。我收到如下警告
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in /home/myusername/public_html/dh/cron.php on line 19
我已经检查并验证了以下内容
1)。我尝试回显我的查询并尝试在 phpmyadmin 中运行查询,有两个结果。2)如果我在从while循环分配变量后删除代码并尝试运行,它的工作正常并处理两行。3)我在while循环中没有任何其他可以停止循环的代码。
我从上一小时开始尝试各种方法,但仍然找不到任何错误,即使我正在记录所有类型的错误和警告,错误日志中也没有生成任何其他错误或警告。让我知道这里是否有人可以帮助我解决难题。
谢谢!
解决方案
$result = mysqli_query($mysqli,$post_query);
...
while($row = mysqli_fetch_array($result)){
稍后在您的循环中:
$result = curl_exec($ch);
你失去了 MySQL $result
,你需要为你的 cURL 结果使用另一个变量名。
推荐阅读
- sql - PL/SQL 检查变量是否在立即执行的列表中
- java - Java:使用 Bouncy Castle 进行 PGP 加密
- terraform - most_recent=true 的“aws_ami”会影响未来的更新吗?
- angular - angular-stl-model-viewer 不更新纹理
- python - 解决错误:Python 中的“列表索引超出范围”
- r - 所有调整方法都会在 spdep 的 localmoran() 中产生相同的调整 P 值
- python - 从字符串末尾删除 [edit]
- css - 弯曲孩子来确定父母的宽度
- angular - 加载 ngFor 后第一页加载时的数据绑定
- excel - 同样简单的公式;不同的工作簿=不同的结果