+ Reply to Thread
Results 1 to 5 of 5

Thread: php - retrieving info from database

  1. #1
    miocene2 is offline x10Hosting Member miocene2 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    15

    php - retrieving info from database

    I have the following code to generate part of a table:
    PHP Code:
    $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 Code:
    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

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: php - retrieving info from database

    you can use mysql_num_rows($result2) > 0 in place of $result2 exists
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    miocene2 is offline x10Hosting Member miocene2 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    15

    Re: php - retrieving info from database

    Thanks I tried what you said and I also tried this:

    PHP Code:
    $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!

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: php - retrieving info from database

    $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
    }
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  5. #5
    miocene2 is offline x10Hosting Member miocene2 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    15

    Re: php - retrieving info from database

    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 by miocene2; 07-28-2009 at 12:41 PM. Reason: Automerged Doublepost

+ Reply to Thread

Similar Threads

  1. [PHP] Basics of PHP Scripting
    By jeeter in forum Tutorials
    Replies: 21
    Last Post: 02-03-2010, 04:29 PM
  2. PHP PostgreSQL database backup.
    By javilla in forum Programming Help
    Replies: 2
    Last Post: 05-14-2009, 04:23 AM
  3. database in php
    By lordsofvine in forum Programming Help
    Replies: 2
    Last Post: 09-11-2008, 02:30 PM
  4. Sigo con problemas con phpbb2
    By reciecho in forum Soporte
    Replies: 7
    Last Post: 10-20-2007, 06:28 PM
  5. search database with php
    By niahossain in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 10-17-2007, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers