Solved

Quick query question

Posted on 2003-12-08
4
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 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