Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Too many mysql_queries in php script

Status
Not open for further replies.

commun1

Programmer
May 26, 2004
41
DE
So I have an array which has our alphabet:
$array_letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

I have a mysql database with articles in it and I need an "alphabet index" which outputs me the results according to its starting letter.

Something like this:


<?php
$i = 0;
while($i < 26) {
$letter = $array_letters[$i];
if(mysql_num_rows(mysql_query("SELECT description FROM articles WHERE description LIKE '$letter%'"))!= 0) {?>
<a href="letter.php?letter=<?= $letter;?>"><?= $letter?></a>
<? } $i++;
}
?>


It works fine and only gives me the links to alphabet letters who really have results in the database. However it takes far too long for each array index to be queried, I suppose that's because there are 26 mysql queries to be made before the output starts.

Is there a faster way to do this? Since I have around 100.000 articles in the database the mysql_select checks all of them....
 
do a single query and order the results by description (gettind rid of LIKE).

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
To elaborate on DaZZleD's advice:
Code:
$SQL = "SELECT upper(substring(description,1,1)) as initial
FROM articles GROUP BY initial";
This is a quick query that only outputs the initial letters (all uppercase) of existing article descriptions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top