Forum Moderators: coopster

Message Too Old, No Replies

how to print all the columns in a row?

         

xkid

4:10 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



Hi!

I want to display all the columns in a row without have to write the code for them all, is that possible?

Thanks! :)

directrix

5:24 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



Do you mean in SQL?

As in select * from myfile ... or, select myfile.* from myfile ...?

xkid

5:47 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



yeah, sorry, mysql :)

directrix

6:07 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



Then the above should do the trick.

xkid

6:26 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



not really, im afraid... :S

that would mean i have to do like this

$info = mysql_fetch_array($res);

and then use $info["blabla"] for every cloumn, thats what im trying not to do...

i wondered if its possible to make a some kind of loop to show the columns..

directrix

6:55 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



Ah, sorry, then I can't really help. I know generic SQL, but not MySQL and its scripting.

dreamcatcher

7:05 pm on Nov 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you not use a while loop?


$query = mysql_query("SELECT * FROM Table") or die(mysql_error());

while ($row = mysql_fetch_assoc($query))
{

echo $row['fieldname'];

}

dc

NomikOS

7:19 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



sure is possible!
but isn't too much tricky?

any way if you need display a (very) lot of columns then i understand you.

what do you think about:


if (is_array($array))
{
foreach ($array as $i -> $row)
{
$totalCols = count($row);
for ($j = 0; $j < $totalCols; $j++)
{
echo $row[$i][$j];
}
}
}

(using numerics arrays)

is only the idea, can be buggy.

jatar_k

9:01 pm on Nov 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



or if you just want raw output for debugging then

$info = mysql_fetch_array($res);
echo '<pre>';
print_r($info);
echo '</pre>';

IamStang

5:58 am on Nov 6, 2005 (gmt 0)

10+ Year Member



I just went through this same scenario. LOL. this is what I did for my client database.
<?
$username="<username>";
$password="<password>";
$database="<database>";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM <table_name>";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {
//use as many as necessary
$f01 = mysql_result($result,$i,"<feild1_name>");
$f02 = mysql_result($result,$i,"<feild2_name>");
$f03 = mysql_result($result,$i,"<feild3_name>");
$f04 = mysql_result($result,$i,"<feild4_name>");
$f05 = mysql_result($result,$i,"<feild5_name>");

// output data
echo "$f01<br>$f02<br>$f03<br>$f04<br>$f05";
$i++;
}

?>


Hope it helps someone!
Later!

xkid

12:42 pm on Nov 6, 2005 (gmt 0)

10+ Year Member



thanks a lot for all answers will try it now :)

EDIT: Wierd, it works now, but i cant figure out how...

$res = mysql_query("SELECT * FROM table WHERE id=$id",$db);
$row = mysql_fetch_array($res);

$array = Array($res);

if (is_array($array))
{
foreach ($array as $i -> $row)
{
$totalCols = count($row);
for ($j = 0; $j < $totalCols; $j++)
{
echo "".$row[$j]."<br>";
}
}
}

That gives me the correct output but what about $i and the whole array thing? yeye, as long as it works its good enough for me :) thanks everybody!

NomikOS

3:11 pm on Nov 6, 2005 (gmt 0)

10+ Year Member



glad for you!
Yes, seems correct, dependes on how you construct $array:


$row = mysql_fetch_array($res);

$array = Array($res);

you did a matrix (array of array), thats's why:


echo "".$row[$j]."<br>";

is better than


echo $row[$i][$j];

Only a suggest: always try to understand exactly you code is working. line by line a program gain his own life. you don't want a program untamable!