Solved

Quick query question

Posted on 2003-12-08
4
225 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you both :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

17 Experts available now in Live!

Get 1:1 Help Now