Results 1 to 7 of 7

Thread: Display Error

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Display Error

    Hello, I am attempting to make a page that will display all the jobs that have been done for a certain user and give them a link to download the files for each job. I have it mostly working, but ran into a small error. The error is that ecen though my sql query returns results, they are not displayed. Here is my code:
    PHP Code:
    <?php

    $user 
    $_SESSION['user'];

    // Query to get client data
    $sql "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
    $result mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());

    // Get number of results
    $results mysql_num_rows($result);

    // Put data in array
    $row mysql_fetch_array($result);

    // If no results found
    if($results 1) {
        echo 
    '
        You have no files to download because no jobs have been done for you. Click <a href="http://echo-designes.com/contact.php">here</a> to have a job done.
        '
    ;
    }

    // If there are results
    if($results >= 1) {

    echo 
    '
    <b>Select the files you would like to download.</b>
        <br /><br />'
    ;

    // For each result found    
    while($row mysql_fetch_array($result)) {
        
    extract($row);
        
    echo 
    '
    <a href="'
    .$download.'">'.$title.'</a>
        <br />'
    ;
    }
    }

    ?>
    When I take out the while statement and just use
    PHP Code:
    echo '
    <a href="'
    .$row['download'].'">'.$row['title'].'</a>
        <br />'

    It works fine. But because I want there to be multiple links, I need to use the while statement. Any ideas?

    EDIT: I removed the first $row = mysql_fetch_array($result); statement and it worked. But for another script, I need to get the data from the query before I run my while statement, any way to do that?
    Last edited by Titan85; 02-05-2007 at 06:45 PM.
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    For the second all you have to do is save your data in an array, and then reference back to it. Look at this example(based on the code you added above)
    PHP Code:
    <?php

    $user 
    $_SESSION['user'];

    // Query to get client data
    $sql "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
    $result mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());

    // Get number of results
    $results mysql_num_rows($result);

    // If no results found
    if($results 1) {
        echo 
    '
        You have no files to download because no jobs have been done for you. Click <a href="http://echo-designes.com/contact.php">here</a> to have a job done.
        '
    ;
    }

    // If there are results
    if($results >= 1) {
    $rows = array();//Set up storage array
    echo '
    <b>Select the files you would like to download.</b>
        <br /><br />'
    ;

    // For each result found    
    while($row mysql_fetch_array($result)) {
        
    $len =  count($rows);//get new index
        
    $rows[$len]['download'] = $row['download'];//add data
        
    $rows[$len]['title'] = $row['title'];//add data
    extract($row);
        
    echo 
    '
    <a href="'
    .$download.'">'.$title.'</a>
        <br />'
    ;
    }

    }

    ?>
    The $rows array will contain all the data you. Also, I wouldn't abuse extract() to much. It may be handy, but it reduces readability and I don't see any real benefit to it.

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, It seems that I get an error every time I try to get an array from more than one sql query. For example, I run query 1 and then use: $row = mysql_fetch_array($result). Then I run query 2 and use: $row1 = mysql_fetch_array($result2) to put the data in the array. It seems that it does not work for the second array. Any idea why?
    Thanks DD, you saved me countless times

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    There shouldn't be any problem with that(if I understand you right). Showing us the code will help us help you better.

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Shouldn't $results = mysql_num_rows($result);
    be: $results = mysql_numrows($result);
    ?
    - Mike

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by mburt View Post
    Shouldn't $results = mysql_num_rows($result);
    be: $results = mysql_numrows($result);
    ?
    No, that's right. It should be mysql_num_rows. Sorry, just got into the thread and need to re-read the original post to get a better idea of what's going on.

    Added Later: Post the code that you get an error in (in your second post). That will help out a lot.
    Last edited by thetestingsite; 02-07-2007 at 02:31 AM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, I will be posting the code later tonight, I have to go through it again to remember what I was doing since I started some other stuff at the same time .

    EDIT: Never mind, I figured out the problem, I had $row['$title'] instead of $row['title'], so it messed it all up. You gatta love stupid mistakes. Thanks for the help
    Last edited by Titan85; 02-08-2007 at 04:19 AM.
    Thanks DD, you saved me countless times

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •