【问题标题】:While loop not being compiled (PHP V 5.3.8)未编译循环 (PHP V 5.3.8)
【发布时间】:2016-10-24 20:50:02
【问题描述】:

由于某种原因,我被困在以下不会进入 while 循环的代码中:

 <?php 
// get All data from company and set into table 
if(!isset($_SESSION)){
    session_start();
}
?>

<?php 
$user_id1="";
if (isset($_SESSION['user_id']))
{$user_id1=$_SESSION['user_id'];}

?>

<script type="text/javascript" language="javascript">
var $j = jQuery.noConflict();
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.2.min.js"></script>

<script src="js/plugins/jquery.spincrement.js"></script>


<div id="container" class="row">
<div class="col-sm-8">
<div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">Trending Stocks List</h3>
        </div> </div>

    <ul class="grid">

<?php 

include 'db.php';
$json = "";
//$json=array();
$con=GetMyConnection();


// check for sell if not holding any stocks 
$comp_sell_id=array();
$query_2="select distinct(comp_id) from tbl_buy_sell_share where (reedim_status!='y' or reedim_status is null) and user_id='$user_id1'";
$retval = mysql_query( $query_2, $con );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$i=0;
while($row = mysql_fetch_assoc($retval))
{
$comp_sell_id[$i]=$row['comp_id'];
$i++;
}

//$queryt="SELECT id_com_manual,current_price,image_path,fb,twitter,instagram,profile_id,(current_price - close_price) as profit FROM tbl_company_manual where status='1' and  //profile_id!='' ORDER BY profit desc company_name asc limit 40";
$queryt="SELECT id_com_manual,company_name,min_price,max_price,close_price,image_path,market_cap,profile_id,current_price,fb,twitter,instagram,ABS(current_price - close_price) as profit FROM tbl_company_manual where status='1' and  profile_id!='' ORDER BY profit DESC,company_name ASC limit 40";
// get the data from database
$retval = mysql_query( $queryt, $con );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$countrow=0;
while($row = mysql_fetch_assoc($retval))
{
         $comp_id=$row['id_com_manual'];
        $countrow++;            
     $actualprices = ($row['current_price']);
     $pr = ($row['profit']);
        $closeprice = ($row['close_price']);
        $imagepath = ($row['image_path']);
        $fb = ($row['fb']);
        $twitter = ($row['twitter']);
        $insta = ($row['instagram']);

retval 在我回显它时返回一个资源 ID,所以我知道数据库已连接并且正在向 fetch_assoc 函数发送一些值。我尝试用 fetch_array 替换该函数,但这也不起作用。此外,在使用 php 的“or die”功能时,我看不到任何错误。

我尝试将 retval 重命名为不同的变量,也使用 mysqli 无济于事。有什么想法吗?

【问题讨论】:

  • 停止使用已弃用的mysql_*API。将 mysqli_*PDO 与准备好的语句一起使用
  • 除了not using mysql_ functions,PHP 5.3 是EOL for nearly 2 years。是时候升级了。
  • 开始使用mysqli。到目前为止没有问题。仍然不知道为什么上面的代码不起作用
  • 这个问题你解决了吗? :)
  • 不,我没有。我重新排列了代码并使用了 mysqli,因此它起作用了。不知道为什么上面的代码为什么上面的代码是错误的

标签: php mysql


【解决方案1】:

所以你有这个:

while($row = mysql_fetch_assoc($retval))

是否进入循环与 JavaScript、jQuery、代码编译或 PHP 版本完全无关。 while loop 的工作方式如下:

while (expr)
    statement

while 语句的含义很简单。它告诉 PHP 执行 重复嵌套语句,只要 while 表达式 计算结果为 TRUE。每次检查表达式的值 循环的开始,所以即使这个值在 嵌套语句的执行,执行不会停止,直到 迭代结束(每次 PHP 运行 循环是一次迭代)。有时,如果 while 表达式计算 从一开始就到FALSE,嵌套语句甚至不会 运行一次

因为你的exprmysql_fetch_assoc()

返回值

返回对应于 获取的行,如果没有更多行,则 FALSE

...发生的情况是您的 SQL 查询没有返回任何行。

您现在可以忘记 PHP 并启动您最喜欢的 MySQL 客户端(MySQL Workbench、HeidiSQL 等)。在那里尝试您的 SQL 代码,直到它按预期工作。

【讨论】:

  • 我的查询确实返回了行!我检查了针对 phpMyadmin 的行。不过感谢您的回复:)
  • 那么您以某种方式得到了错误的诊断。旧的 mysql 扩展已过时、不安全且未维护,但没有像返回 false 那样损坏,同时仍有要检索的行。
  • 是的!!!!这就是为什么我仍然想知道这个while循环。最后我清理了我的代码并使用了 mysqli。谢天谢地,它奏效了:)
  • 好吧...我已对“这个问题是由无法再复制的问题引起的”投了赞成票。
  • 好吧,那我不妨把你的答案标记为正确的答案。谢谢! :D
猜你喜欢
  • 2012-09-14
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2016-01-05
  • 2012-01-25
  • 1970-01-01
  • 2011-12-20
相关资源
最近更新 更多