Forum Moderators: coopster

Message Too Old, No Replies

getting all data from a mysql table row

php mysql data row fetch all results

         

huds

3:30 pm on Jun 19, 2005 (gmt 0)

10+ Year Member



Can anyone tell me how to echo all data from a specific row in a mysql db table?

This is my best effort so far, but just returns a blank page...

<?php

$columnselect = $_POST["selection"];

$rowreturn = "SELECT * FROM menu WHERE linktext = '$columnselect'";

$rowdata = mysql_query($rowreturn) or die('Query failed: ' . mysql_error());

// This is the row data bit

echo "<table>";

echo "<tr>";

while ($columnvalues = mysql_fetch_row($rowdata)); {
$columns = $columnvalues['*'];

echo "<td>$columns</td>";

}

echo "</tr>";

echo "</table>";
?>

in the part $columns = $columnvalues['*']; I'm not sure how to specify ALL data, that is why I used the '*' in $columnvalues.

dreamcatcher

4:34 pm on Jun 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi huds,

You aren`t too far away. fetch_row return numerical indices, so try something like this:

while ($columnvalues = mysql_fetch_row($rowdata)); {

echo "<td>" . $columnvalues[0] . "</td>";

}

If you have other rows you would use [1] and then [2] and so on.

Hope that helps.

dc

hughie

5:07 pm on Jun 19, 2005 (gmt 0)

10+ Year Member



or to get it all in one go for viewing purposes i use
...
while ($columnvalues = mysql_fetch_row($rowdata)); {
echo '<pre>';
print_r($columnvalues);
echo '</pre>';

}

hughie

huds

7:19 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Thanks guys,

Can I ask, is there any difference/benefit between echo and print (or print_r)?

Cheers

jatar_k

7:28 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



print_r is very different from the other 2
print_r() displays information about a variable in a way that's readable by humans. If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.

I use it for checking arrays to make sure what I want to be happening is actually happening.

there is a link on the page for echo [php.net] to a short dicussion about the difference.

echo is a language construct and print is a function so there are different benefits for each.

huds

8:25 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Thanks very much.

supermanjnk

9:00 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



yet another way:

while ($columnvalues = mysql_fetch_row($rowdata)); {
foreach ($columnvalues as $key=>$val) {
echo $key; //this is the column name
echo $val; // this is the data in the column
}
}