Solved

Quick query question

Posted on 2003-12-08
4
238 Views
Last Modified: 2006-11-17
I've been using this format for displaying results of queries, I just wanted to know if this was the most efficient way of doing so or is there another way:

$query = mysql_query("SELECT * from tbl");

while($row = mysql_fetch_array($query))
{
echo "
Field 1=$row[0];
Field 2=$row[1];
Field 3=$row[2];
}

0
Comment
Question by:drakkarnoir
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:aolXFT
aolXFT earned 250 total points
ID: 9897687
mysql_fetch_row, would use less memory. However the PHP Website says that mysql_fetch_array is not significantly slower than mysql_fetch_row, or mysql_fetch_assoc.

I don't think you can really simplify your query any, to make it any faster, unless maybe you only need part of the information. Do you need everything from that table, or do you just need the first three rows?

If so it would be faster to

SELECT name_of_row1, name_of_row2, name_of_row3 from tbl, if there are more rows in the table. It follows the principle of only selecting what you actually need.

0
 
LVL 11

Accepted Solution

by:
shmert earned 250 total points
ID: 9897710
That is an efficient way of doing it.  But not quite the _most_ efficient way of doing it.
Since you are only referencing the fields by their numeric indices, you should use mysql_fetch_row($query);
Also, it looks like your code to echo the data will produce a parse error.  Try something like this:


$query = mysql_query("SELECT * from tbl");

while($row = mysql_fetch_row($query))
{
    echo 'Field 1=' . $row[0] . '<br />';
    echo 'Field 2=' . $row[1] . '<br />';
    echo 'Field 3=' . $row[2] . '<br />';
}
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9897796
using list _might_ also be faster.

Accessing array elements isn't that fast, since it involves the use of hash-tables, and hashing functions.

try

while(list($row1, $row2, $row3) = mysql_fetch_row($query))
{
  echo "Field 1 = $row1 <br />"
  ...
  ...
}

I haven't tested this, but you might want to try it.
0
 

Author Comment

by:drakkarnoir
ID: 9900502
Thank you both :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now