Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Quick query question

Posted on 2003-12-08
4
Medium Priority
?
278 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 1000 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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

927 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