473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

retrieving multiple rows advice

let's say I've an array of articles's id

$arr_art_id=(1, 551, 2015, 6 .......n )

what option is better (1 or 2), in order to achieve better performance?

(pseudo code)
1)
for i=0 to array count{
select * from articles where articles.id=$arr_art_id[i]
mysqlquery($sql)
//do whatever i have to do
}
2)

$or ='';
for i=0 to array count{
$sql.= $or . 'articles.id='. $arr_art_id[i];
$or ='or';
}

$sql = 'select * from articles where' .$sql

mysqlquery($sql)
//do whatever i have to do

tia

regards - julian maisano

Nov 22 '05 #1
8 1683
julian_m wrote:
let's say I've an array of articles's id

$arr_art_id=(1, 551, 2015, 6 .......n )

what option is better (1 or 2), in order to achieve better performance?

(pseudo code)
1)
for i=0 to array count{
select * from articles where articles.id=$arr_art_id[i]
mysqlquery($sql)
//do whatever i have to do
}
2)

$or ='';
for i=0 to array count{
$sql.= $or . 'articles.id='. $arr_art_id[i];
$or ='or';
}

$sql = 'select * from articles where' .$sql
//do whatever i have to do

tia

regards - julian maisano

Better than either:

$sql = 'select * from articles where articles.id in ($arr_art_id)';
mysqlquery($sql);
//do whatever i have to do
Nov 22 '05 #2
On Tue, 21 Nov 2005, julian_m wrote:
let's say I've an array of articles's id

$arr_art_id=(1, 551, 2015, 6 .......n )

what option is better (1 or 2), in order to achieve better performance?

(pseudo code)
1)
for i=0 to array count{
select * from articles where articles.id=$arr_art_id[i]
mysqlquery($sql)
//do whatever i have to do
}
2)

$or ='';
for i=0 to array count{
$sql.= $or . 'articles.id='. $arr_art_id[i];
$or ='or';
}

$sql = 'select * from articles where' .$sql

mysqlquery($sql)
//do whatever i have to do

tia

regards - julian maisano


Please for give me, regulars, if I'm out of line or out of my league for a
first post, but I thought I'd help out a bit now that I write PHP for a
living. (Whether I'm any good at it is another story, but at least I've
improved.)

If it's better performance your'e after, Julian, try something like this:

$sql = "SELECT title, body, author FROM articles ORDER BY date ASC";
mysqlquery($sql);
$results = mysql_num_rows($sql);
for ($i=0; $i < $results; $i++)
{
$rows .= mysql_fetch_array($sql);
}

foreach($rows as $row)
{
// Do things
}

Selecting only the columns you want in your SQL statement saves a little
bit of overhead, and you might as well get the database to put them in the
order you want them in. Then you put each row into an array, so it becomes
a two-dimensional array that looks just like the results table.

Then you can use the handy foreach loop to do whatever you want with the
output, one line at a time, without having to do any more queries.

My code is probably slightly off as it's been a while since I've written
procedural PHP code (my boss made me switch to object oriented, which has
sped us up when it comes to writing the projects but is probably slower at
executing the actual code, although not noticably so). But I'd recommend
naming columns, making a two-dimensional array, and using foreach on it,
for what it's worth.

Oh, and making sure you don't use a function as the middle argument in a
"for" loop helps. While it's less code, the line "for ($i=0; $i <
mysql_num_rows($sql); $i++)" would make PHP call up that function every
time it executed the code in the loop.

Does that help?

Zoe.
Nov 22 '05 #3
X-No-Archive: yes

Please for give me, regulars, if I'm out of line or out of my league for a
first post, but I thought I'd help out a bit now that I write PHP for a
living.
Sorry, Zoe, I think you missed the issue that the OP was trying to
resolve. Your advice is good as far as improving general performance is
concerned but your solution doesn't address how to restrict the SQL
query to ids listed in the array (first few lines of the OP):
let's say I've an array of articles's id $arr_art_id=(1, 551, 2015, 6 .......n )


(Don't let that put you off contributing.)

---
Steve

Nov 22 '05 #4

Steve wrote:
X-No-Archive: yes

Please for give me, regulars, if I'm out of line or out of my league for a
first post, but I thought I'd help out a bit now that I write PHP for a
living.


Sorry, Zoe, I think you missed the issue that the OP was trying to
resolve. Your advice is good as far as improving general performance is
concerned but your solution doesn't address how to restrict the SQL
query to ids listed in the array (first few lines of the OP):
let's say I've an array of articles's id

$arr_art_id=(1, 551, 2015, 6 .......n )


(Don't let that put you off contributing.)


indeed

regards - julian

Nov 22 '05 #5

Bob Stearns wrote:

Better than either:

$sql = 'select * from articles where articles.id in ($arr_art_id)';
mysqlquery($sql);
//do whatever i have to do


As you can imagine, I didn't know anything about "in".....

thanks a lot

regards - julian

Nov 22 '05 #6
julian_m wrote:
Bob Stearns wrote:
Better than either:

$sql = 'select * from articles where articles.id in ($arr_art_id)';
mysqlquery($sql);
//do whatever i have to do


As you can imagine, I didn't know anything about "in".....

thanks a lot

I've used IN for subqueries, but I'd never heard of using it with an
array. Can you really use the array just like that? After trying to find
more info on this, all I have seen indicates that you have to implode
the array into a comma separated list.

Si o no?

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Nov 22 '05 #7

Chuck Anderson wrote:
julian_m wrote:
Bob Stearns wrote:
Better than either:

$sql = 'select * from articles where articles.id in ($arr_art_id)';
mysqlquery($sql);
//do whatever i have to do
As you can imagine, I didn't know anything about "in".....

thanks a lot

I've used IN for subqueries, but I'd never heard of using it with an
array. Can you really use the array just like that? After trying to find
more info on this, all I have seen indicates that you have to implode
the array into a comma separated list.

Si o no?


Efectivamente, tiene usted absoluta razón ; )

It doesn't work just including the array into the sql. I think that Bob
Stearns wrote a sort of pseudo-code (just as i did) to "show the road"
....

*****************************
Chuck Anderson · Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************


By the way, a few days ago I saw in the news a guy who is travelling on
his bike all around the world. If you are interested, or even if you
want to improve your spanish, you can give it a look

www.acercandoelmundo.com

regards - julian

Nov 23 '05 #8
julian_m wrote:
Chuck Anderson wrote:

julian_m wrote:

Bob Stearns wrote:
Better than either:

$sql = 'select * from articles where articles.id in ($arr_art_id)';
mysqlquery($sql);
//do whatever i have to do


As you can imagine, I didn't know anything about "in".....
I've used IN for subqueries, but I'd never heard of using it with an
array. Can you really use the array just like that? After trying to find
more info on this, all I have seen indicates that you have to implode
the array into a comma separated list.

Si o no?


Efectivamente, tiene usted absoluta razón ; )

It doesn't work just including the array into the sql. I think that Bob
Stearns wrote a sort of pseudo-code (just as i did) to "show the road"
...

Okay. I wanted to be sure I understood how to use this properly.
By the way, a few days ago I saw in the news a guy who is travelling on
his bike all around the world. If you are interested, or even if you
want to improve your spanish, you can give it a look

www.acercandoelmundo.com

Thanks. .... I'm always getting myself in too deep this way. As I like
to say, I know just enough Spanish (German, too) to get myself into
"trouble." Just enough to make someone think that I know what I'm
talking about :-)

Anyway, I don't know enough Spanish to be able to read that site by
myself, but I *can* see that this is a very ambitious project. And, it
is a couple (man and woman) riding a tandem bicycle. They plan to stay
on the road for 10 years.

Re: language. I use "si o no" as a "universal phrase" ..... much like
the German kaput, or Italian ciao. I think those are all understood in
any country.
--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Nov 26 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RJ | last post by:
We currently send product releases to our customers, and often have to include scripts that need to be ran on the Oracle databases (also do it for SqlServer customers, but we use a different set of...
6
by: Dean Slindee | last post by:
Does anybody have an actual example of retrieving an Image data type column from a SQL Server table using a dataset (not a datareader)? I would like to see the statements that would move the...
3
by: simchajoy2000 | last post by:
Hi, I am trying to use a VB.NET listview object to display information from a datatable. I need to have two columns of information but I don't want the user to be able to select each column...
1
by: Glenn T. Kitchen | last post by:
Dear Group, I'm having problems retrieving the child rows of a parent row. The parent table is Users and the child table is Addresses. I used the Schema to create a DataRelation between the...
5
by: aniket_sp | last post by:
i am using a data adapter and a dataset for filling and retrieving data into .mdb database. following is the code..... for the form load event Dim dc(0) As DataColumn Try If...
4
by: Papachumba | last post by:
Hi guys, i have a little problem and was wandering if anyone can help. I just need my query modified a little bit to allow some extra values in there... Tables: offers Table OfferID...
0
by: yeltsin27 | last post by:
I need some advice on handling dynamically added controls in a GridView. My app takes an uploaded CSV file containing addresses, converts it to a DataTable, databinds the DataTable to a...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
3
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.