php - retrieving info from database

miocene2

New Member
Messages
15
Reaction score
0
Points
0
I have the following code to generate part of a table:
PHP:
$result2 = mysql_query("SELECT * FROM userpayments WHERE personid = {$user_id[$k]} AND paymentid = $payid ORDER BY paymentid ASC");
while($row2 = mysql_fetch_array($result2))
        {
        
        
echo "<td " . $style . ">" . $row2['payed'] . "</td><td " . $style . ">" . $row2['fs'] . "</td><td>" . $row2['topay'] . "</td>";
}
It effectively generates 3 table cells containing data obtained from my sql table for each row in the sql table.

My problem is when a record doesn't exist in the table that has both personid = {$user_id[$k]} and paymentid = $payid it doesn't do the echo it just skips it sometimes leaving my table missing a set of 3 cells on a row.

What I want it to do is (in psuedo-php)
PHP:
if ($result2 exists)
{echo "<td " . $style . ">" . $row2['payed'] . "</td><td " . $style . ">" . $row2['fs'] . "</td><td>" . $row2['topay'] . "</td>";}

else

{echo "<td " . $style . ">" . "-" . "</td><td " . $style . ">" . "-" . "</td><td>" . "-" . "</td>";}
so basically if there is no row in the table where personid = {$user_id[$k]} and paymentid = $payid just echo a row of cells containing hyphens
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
you can use mysql_num_rows($result2) > 0 in place of $result2 exists
 

miocene2

New Member
Messages
15
Reaction score
0
Points
0
Thanks I tried what you said and I also tried this:

PHP:
$result2 = mysql_query("SELECT * FROM userpayments WHERE personid = {$user_id[$k]} AND paymentid = $payid ORDER BY paymentid ASC");
while($row2 = mysql_fetch_array($result2))
        {
        
        if (!$result2)
    {echo "<td " . $style . ">" . "-" . "</td><td " . $style . ">" . "-" . "</td><td>" . "-" . "</td>";}    
else
{echo "<td " . $style . ">" . $row2['payed'] . "</td><td " . $style . ">" . $row2['fs'] . "</td><td>" . $row2['topay'] . "</td>";}

}

Niether seems to work.

See http://fairshare.x10hosting.com/statement.php to see what i mean!
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
$result2 is going to be a valid resource, as long as your query is good, so it won't evaluate to false if there are no rows. Your problem now is mysql_fetch_array($result2) evaluates to false. There are no rows to fetch. Your execution never reaches if(!$result2).

Code:
$result2 = mysql_query(....
if($result2) {
  if(mysql_num_rows($result2) == 0) {
    echo .... // there are no rows
  }
  else {
    echo .... // there are rows
  }
}
else {
//some error in your query
}
 

miocene2

New Member
Messages
15
Reaction score
0
Points
0
Thanks.
Tried that also but does the same as before.
I don't understand - surely if there are no rows in the query then it would register and echo the dashed table cells.
If there are rows in the query it would echo the tables cells containing the contents of the row.

Why does it echo nothing at all for the last one?
Edit:
OK sorted it out - was to do with the order of stuff
 
Last edited:
Top