Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post mysql_fetch_array fails bit does not send an error

    ************************SOLVED*************************

    Yeah... I have no idea how to approach this. I guess I'll show you the code first:
    PHP Code:
    while($n<$rows) {
        echo 
    "<tr>";
        
    $query="SELECT * FROM `survey` WHERE `id`=$id";
        
    $data=mysql_query($query) or die ('<b>ERROR 5: unable to retrieve data from database: '.mysql_error().'. Please try again later.</b>');
        
    $row=mysql_fetch_row($data) or die ('<b>ERROR 6: unable to fit data into an array: '.mysql_error().'. Please try again later.</b>');
            
    //......
            //code
            //......
            
    $n=$n+1;

    I get "ERROR 6..." thrown at me, but there is no error message from mysql to display. If it helps, I'm using mysql v4 not v5. If you need more surrounding code, just ask, thanks.
    Last edited by yonni; 07-21-2006 at 07:31 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mysql_error() won't work on fetch calls. m_e() is only to retreive an error message from the database when you use a function that directly communicates with that db (like mysql_connect(), mysql_query(), etc.). By the time you run a fetch function, you've already got a result set and are no longer working with the db itself.

    You don't want to run a die() routine with a fetch function anyway, because a FALSE return value isn't necessarily an error. Fetch functions return FALSE when they don't receive a row from the result set (meaning you've iterated through all the rows, or no rows matched your query conditions).

    To get to the bottom of your issue, I'd remove the fetch call's die() completely and run a mysql_num_rows() check on your result to set to see if you have any rows returned.

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that, but I still don't think that the mysql_fetch_array is working, as i cant get anything from it. Basically im using (for example):
    PHP Code:
    echo "<td>".$data[0]; 
    and ive tried:
    PHP Code:
    echo "<td>".$data['id']; 
    (where 'id' is the column header in the database), and neither are displaying anything.

  • #4
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Double-check you're getting results with mysql_num_rows().

    You also need to refer to the array you're creating, not your result resource:
    $row[0] instead of $data[0]. Also note that if you're using mysql_fetch_row(), you will only have numeric indices, so $row['id'] won't work. mysql_fetch_array() gives you numeric and string indices (and, to round it out, mysql_fetch_assoc() only gives string indices).

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have already checked, and I am getting results (3 rows to be precise). And i did mean "$row[]" rather than "$data[]", that was just an accident in the transcription. Thankyou for clearing up of what each mysql_fetch_... means, I had read that somewhere before, but could not quite remember. However, although I am using mysql_fetch_row() and have tried using both numerical and string indices, I still get no data displayed. For reference, here is the whole php code:
    PHP Code:
        <?php
        $dbh
    =mysql_connect("***""***""***") or die ('<b>ERROR 1: Could not connect to the database: '.mysql_error().' Please try again later</b>');
    mysql_select_db("johall0_1") or die ('<b>ERROR 2: unable to select the correct database: '.mysql_error().'. Please try again later.</b>');
    $query="SELECT * FROM `survey`";
    $fulldata=mysql_query($query) or die ('<b>ERROR 3: unable to retrieve data from database: '.mysql_error().'. Please try again later.</b>');
    $rows=mysql_num_rows($fulldata);
    $n;
    $id=0;
    while(
    $n<$rows) {
        echo 
    "<tr>";
        
    $query="SELECT * FROM `survey` WHERE `id`=$id";
        
    $data=mysql_query($query) or die ('<b>ERROR 5: unable to retrieve data from database: '.mysql_error().'. Please try again later.</b>');
        
    $row=mysql_fetch_row($data);
        echo 
    "<td>".$row[0];
        echo 
    "<td>".$row[1];
        echo 
    "<td>".$row[2];
        echo 
    "<td>".$row[3];
        echo 
    "<td>".$row[4];
        echo 
    "<td>".$row[5];
        echo 
    "<td>".$row[6];
        echo 
    "<td>".$row[7];
        echo 
    "<td>".$row[8];
        echo 
    "<td>".$row[9];
        echo 
    "<td>".$row[10];
        echo 
    "<td>".$row[11];
        echo 
    "<td>".$row[12];
        echo 
    "<td>".$row[13];
        echo 
    "<td>".$row[14];
        echo 
    "<td>".$row[15];
        echo 
    "<td>".$row[16];
        echo 
    "<td>".$row[17];
        echo 
    "<td>".$row[18];
        echo 
    "<td>".$row[19];
        
    $n=$n+1;
    }
        
    ?>

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Each iteration, you're looking for a row with id 0 -- you have three rows with id 0?

  • #7
    New Coder
    Join Date
    Jul 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    omg I cant believe i didnt see that. it's checking for something with id 0 each time, and i think mine start at id=8 or something (due to auto incriment). also, it's only looking 3 times (as there are only 3 rows). Ok, thanks for your help everyone, guess i just needed some sleep to work it out.


  •  

    Posting Permissions

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