Jump to content

Different Number of Rows Returned (PHP v MySQL)


cr750
Go to solution Solved by cr750,

Recommended Posts

I've written a piece of SQL that works at the MySQL prompt, but doesn't work as expected through php.

MySQL version 5.0.95

 
 
mysql> SELECT    ->   
a.date,    ->   a.author,    
->   a.comment,    ->   b.first AS 
p_first,    ->   b.last  AS 
p_last,    ->   c.first AS 
a_first,    ->   c.last  AS 
a_last    -> FROM    ->   
pilot_activity a,    ->   person 
b,    ->   person c    -> 
WHERE    ->   b.id = 
a.pilot_id    ->   AND c.id = 
a.author    -> ORDER BY    
->   p_last ASC    -> 
;+---------------------+--------+-----------------------------+---------+-----------+---------+-----------+| 
date                
| author | 
comment                     
| p_first | p_last    | a_first | a_last    
|+---------------------+--------+-----------------------------+---------+-----------+---------+-----------+| 
2010-03-21 10:02:06 |     50 | OPERATIONAL 
PROCEDURES....  | Phill   | Glasson   | 
John    | Wilkinson || 2013-08-12 00:00:00 
|     50 | 1 
1                         
| John    | Wilkinson | John    | Wilkinson 
|+---------------------+--------+-----------------------------+---------+-----------+---------+-----------+2 
rows in set (0.00 sec)

But the PHP script, using the same query

 
  $sql = <<<SQLSELECT  a.date,  
a.author,  a.comment,  b.first AS p_first,  
b.last  AS p_last,  c.first AS a_first,  c.last  AS 
a_lastFROM  pilot_activity a,  person b,  person 
cWHERE  b.id = a.pilot_id  AND c.id = a.authorORDER 
BY  p_last ASCSQL;

returns three rows!

 

 

 

<br>Phill Glasson<br>2010-03-21 10:02:06 OPERATIONAL PROCEDURES.... (John 
Wilkinson)<br>
<br>Phill Glasson<br>2010-03-21 10:02:06 OPERATIONAL PROCEDURES.... (John 
Wilkinson)<br>
<br>John Wilkinson<br>2013-08-12 00:00:00 1 1 (John Wilkinson)<br>

 

 

 

 

 

Link to comment
Share on other sites

  Here's a more complete code fragment...

$sql = <<<SQL
SELECT 
a.date,  
a.author,
a.comment,
b.first AS p_first,  
b.last  AS p_last, 
c.first AS a_first, 
c.last  AS a_last
FROM
pilot_activity a,
person b,
person c
WHERE  b.id = a.pilot_id  AND c.id = a.author
ORDER BY  p_last ASC
SQL;
    $result = mysql_query($sql, $db);    
    require ('QueryCheck.inc');
    while ($myrow = mysql_fetch_array($result)) {
        $line .= "<br>". $myrow["p_first"]. " ". $myrow["p_last"];
        $line .= "<br>". $myrow["date"];        
        $line .= " ".    $myrow["comment"];
        $line .= " (".   $myrow["a_first"]. " ". $myrow["a_last"]. ")<br>";
        echo $line;
    }
Edited by cr750
Link to comment
Share on other sites

that's pretty messy code. i had to clean it up to even tell what is was doing.

 

the problem is you are concatenating $line to itself and echoing it at the end of each pass through the loop, so it keeps adding each new row of data to what has already been built and echoed.

 

the first assignment to $line needs to remove the concatenation dot so that you are not reusing the previous contents in $line.

Link to comment
Share on other sites

  • Solution

Sorry about the messy code. A problem with copy/paste between putty and the "code" box. I tidied it up pretty much as soon as I posted it, but you really jumped the gun!   :happy-04:

 

And yes, the reuse of $line was the problem. Nicely sorted now.

 

Thanks mac_gyver!

 

Best regards,

Phill.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.