Solved

Quick query question

Posted on 2003-12-08
4
267 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

636 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