Jump to content

did it break???


dogbiter

Recommended Posts

have they broken loops between php and mysql in recent versions??? i have PHP Version 5.2.4 and mysql 5.0.51a and i'm trying to get a simple patch of code to display everything in any given table. i've gotten it to work befor but it was over a year ago and almost forgot everything about php and mysql LOL. so i'm trying to keep it simple. basically here is the code .....

function DB($db){
$query = "select * from $db";
$query1 = mysql_query($query) or die(mysql_error());
$query2 = mysql_fetch_array($query1) or die(mysql_error());
echo "<table border=3>";
        for ($i=0;$i<=mysql_num_rows($query1);$i++){
                $id = $i;
                echo "<tr>";
                $query7 = mysql_query("select * from $db");
                $query6 = mysql_fetch_array($query7);
                for ($d=0;$d<mysql_num_fields($query7);$d++){
                        $query3 = mysql_fetch_array($query7);
                        echo "<td>".$query2[$d]."</td>";
                }
                echo "</tr>";
        }
echo "</table>";
}

 

i've gotten the loop to work but it just fills it with the same lines over and over again. sorry it a little jumbled but i've been poking at it for a couple hours now or more. i had it working but i added a where statement to make the numbers the same as an ID column in the table BUT then it only works with tables with ID columns in sequential order.

Link to comment
Share on other sites

yes there are a few that are identical and i noticed that when i set variables in different places they did different things. and i'm sorry about "between" it's 5 am and no sleep but have they changed the way you have do loops??? or is what i have supposed to work??? like i said i've gotten it to work in the past but i cannot figure it out and most of that is off of memory.

Link to comment
Share on other sites

The code snippet is very confusing...

 

1) you named variables "query" that are really "results"

2) query3 is the same as query7, but you use query2, which is never defined...

3) query7 is the same as query1

 

No wonder you're getting strange things... GIGO.

Link to comment
Share on other sites

oh here we go

 

function DB($db){
$query = "select * from $db";
$result1 = mysql_query($query) or die(mysql_error());
$result2 = mysql_fetch_array($result1) or die(mysql_error());
echo "<table border=3>";
        for ($i=0;$i<=mysql_num_rows($resutl1);$i++){
                $id = $i;
                echo "<tr>";
                for ($d=0;$d<mysql_num_fields($result2);$d++){
                        echo "<td>".$result2[$d]."</td>";
                }
                echo "</tr>";
        }
echo "</table>";
}

 

ok that's simplified to what it was when i first started working on it

Link to comment
Share on other sites

Nothing has changed to do with loop between PHP or MySQL versions. It is actually to do with your logic which is incorrect.

 

mysql_fetch_array does not return all results. Each time you call mysql_fetch_array it'll return one row from the result set. Your code cleaned up

 

<?php

function DB($db)
{
    $query   = "select * from $db";

    $result = mysql_query($query) or die(mysql_error());

    // start HTML Table
    echo '<table border="1" cellspacing="1" cellpadding="5">' . "\n  <tr>\n";

    // display field name as headings

    // get the number of fields in the table
    $f = mysql_num_fields($result);
    // loop through the number of fields in the table
    for($i = 0; $i < $f; $i++)
    {
        // display table heading
        echo '  <th>' . mysql_field_name($result, $i) . "</th>\n";
    }

    echo "  </tr>\n"; // close table row

    // now display the data from the table
    // mysql_fetch_rows returns an array
    while($row = mysql_fetch_row($result))
    {
        // we are using implode to extract the data from the $row array into sperate table cells
        echo "  <tr>    <td>" . implode("</td>\n    <td>", $row) . "</td>\n </tr>\n";
    }

    echo '</table>'; // close the table
}

?>

Link to comment
Share on other sites

thank you for the help I'll try that(i connot right now as i'm on windows *need to restart to linux) and "implode()" is new to me and i know that the "fetch_array" got rows but the reiteration was supposed to display each row. I've gotten it to work by starting the starting at $i=1 instead of 0 and using the ID column in a "where" section of the query but that only works if the table has an ID column and it's sequential.

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.