Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array is not working well

I am new in PHP. I have a code in which i use 2 sql commands. First command fetch 1st latest row and second command fetch 2nd latest row. This code is place in file sqlquery.php

here is code of sqlquery.php

<?php
include ("connection.php");

Problem of my code is my array print same record in all rows. But in Db there is different records. My code is print only first record in each row

I want to output of my code is like this

like image 404
sunny Avatar asked Feb 01 '26 07:02

sunny


1 Answers

The problem is the double loop, now for every result from the first query you add an array item for each result of the second query duplicating the array items effectively, you can change this

while($row1 = mysql_fetch_assoc($result1)){
        while($row2 = mysql_fetch_assoc($result2)){

To:

while($row1 = mysql_fetch_assoc($result1) && $row2 = mysql_fetch_assoc($result2))
{

It would be better to change your sql query though to incorporate all values in one request.

Another thing you can do, though is less nice:

while($row1 = mysql_fetch_assoc($result1))
{        
    $opinion[]= $row1['opinion'];
    $action[]= $row1['atitle'];
    $long_term[]= $row1['ltitle'];
    $outlook[]= $row1['otitle'];
    $rating_type[]= $row1['ttitle'];
    $short_term[]= $row1['stitle'];
}
while($row2 = mysql_fetch_assoc($result2))
{
    $p_long_term[]= $row2['ltitle'];
    $p_short_term[]= $row2['stitle'];
}
like image 81
Bas van Stein Avatar answered Feb 02 '26 21:02

Bas van Stein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!