Jump to content

Is there a simpler way to this (below-mysql query) ?


jd2007

Recommended Posts

if i want to display each row from a table:

 

$que1="select MAX(id) from table";
$res1=mysql_query($que1);
$row1=mysql_fetch_row($res1);

for ($i=0; $i<=$row1[0]; $i++)
{
$que2="select cola from table where id='$i'";
$res2=mysql_query($que2);
$row2=mysql_fetch_row($res2);
echo $row2[0]."<br />";
}

 

what function to use or query to use to make the above simpler ?

Actually a clever way, most people would not have thought of it.

It's another method of doing it, it's not "Wrong".

 

Disagree.

Imagine that you have three rows in such table

id    cola

85    boo

93    foo

132  boo2

 

Now you will query dbase 133times to read 3 rows with 129 error messages.

133 times?

 

And error messages could be avoidable(ever heard of the at(@) sign).

I just cannot think of any examples on how I could use that for an application that would be better than a while loop(maybe there isn't).

 

But it is "unique", and not wrong.

133 times?

First is the query for "select MAX(id)".

And the loop try to fetch 132 rows started from id = 1 to id = 132.

 

I'm not disagreeing that it will loop 133 times, I wanted to know why.

It starts counting at 0 not one, that's why it has 133 loops.

for ($i=0; $i<=$row1[0]; $i++)

 

Agree with gurroa .

The best option is to use while loop.

For what jd2007 wants it for, yes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.