0

I am stuck at the following piece of code that will not enter the while loop for some reason:

 <?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 returns a resource id when i echo it, so i know that the db is connected and is sending some value to the fetch_assoc function. I have tried replacing the function with fetch_array, but that doesn't work either. Moreover, i cannot see any error when using 'or die' function of php.

I have tried renaming retval to different variables, and also used mysqli to no avail. Any ideas?

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Fabulous
  • 735
  • 1
  • 10
  • 28

1 Answers1

1

So you have this:

while($row = mysql_fetch_assoc($retval))

Whether the loop gets entered or not has absolutely nothing to do with JavaScript, jQuery, code compilation or PHP versions. A while loop works like this:

while (expr)
    statement

The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once.

Since your expr is mysql_fetch_assoc():

Return Values

Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

... what happens is that your SQL queries do not return any rows.

You can forget about PHP by now and launch your favourite MySQL client (MySQL Workbench, HeidiSQL, whatever). Try your SQL code there until it works as expected.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • My queries did return Rows! i checked the rows against phpMyadmin. Thanks for your response though :) – Fabulous Jul 19 '16 at 11:10
  • Then you got a wrong diagnostic somehow. The legacy mysql extension is obsolete, insecure and unmaintained but not as broken as to return `false` while there're still rows to be retrieved. – Álvaro González Jul 19 '16 at 11:13
  • Yes!!!! Which is why i am still wondering about this while loop. in the end i cleaned up my code and used mysqli. thankfully it worked :) – Fabulous Jul 19 '16 at 11:16
  • Alright... I've issued a close vote for "This question was caused by a problem that can no longer be reproduced". – Álvaro González Jul 19 '16 at 11:25
  • well, i might as well mark your answer the right one then. Thankyou! :D – Fabulous Jul 19 '16 at 11:38