Jump to content

Recommended Posts

Hi all,

 

I would like to display everything from a table in the browser.

Do i need to use fetch_array or fetch_assoc? And what loop would work best?

 

Thanks.

 

The code i have so far:

 

$sql = "SELECT * FROM `trainingsdata` WHERE 1 LIMIT 0, 30 "; 
$result = mysql_query($sql) or die('Eror selecting data');
$row = mysql_fetch_array($result);
$num = mysql_num_rows($result);

I added a loop but get Resource id #5 multiple times

 

Here the code so far:

 

$sql = "SELECT * FROM `trainingsdata` WHERE 1 LIMIT 0, 30 "; 
$result = mysql_query($sql) or die('Error selecting data');
$row = mysql_fetch_array($result);
$num = mysql_num_rows($result);


for ($i=0; $i<$num; $i++)
{
echo $result;
}

Directly out of the mysql_fetch_array section of the php manual -

 

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    // code to access your fields in the $row array
}

$row becomes an array, and the keys are the name of the columns in your table. Inside the loop put this:

 

echo $row['columnName'];
echo "<br />";

 

Where columnName is the name of a column in the table you are selecting data from.

thanks

Thanks ProjectFear,

 

I still get Resource id #5 multiple times

 

This is the code i have now:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo $row['columnName'];
echo "<br />";
}

Hmm, try this:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<pre>", print_r($row), "</pre><br />";
}

 

And see if anything is outputted then.

I am getting this as output:

 

Array

(

    [id] => 1

    [datum] => 2008-09-09

    [cursus] => php

    [plaats] => Washington

)

1

 

 

Array

(

    [id] => 2

    [datum] => 2008-09-09

    [cursus] => php

    [plaats] => New York

)

1

 

 

Array

(

    [id] => 3

    [datum] => 2008-09-09

    [cursus] => php

    [plaats] => Chicago

)

etc etc.

 

The  code so far:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo "<pre>", print_r($row), "</pre><br />";

}

 

I would like to have the output on one row.

 

Strange, before it should've been echoing something. Try this now:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row['cursus'];
echo "<br />";
}

 

See if that outputs anything.

Thanks  ProjectFear,

 

It now displays all from cursus.

 

So when I add date and place it works. But doesn't show nicely.

 

Here is the code now:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row['datum']." ".$row['cursus']." ".$row['plaats'];
echo "<br />";
}

Well you need to format it then. You can use normal markup here, just your basic HTML. Just make sure you place it in the right spots.

 

For example:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "Date: ".$row['datum']."<br />Cursus: ".$row['cursus']."<br />Plaats: ".$row['plaats'];
echo "<br /><br />";
}

 

Of course you can do anything else, like place it in a table.

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    echo $row['columnName'];

echo "<br />";

}

 

 

Perhaps the problem...    echo $row['columnName'];

 

...did you replace "columnName" with an actual field name from your table?

 

 

Adam

 

 

Hi Adam, yes I did. And works well now.

 

This is the final code:

 

$result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") 	or die(mysql_error());
echo '<table cellpadding="5" cellspacing="5"><tr>';
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>";
}
echo "</table>";

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.