Forum Moderators: coopster

Message Too Old, No Replies

Avoiding MYSQL calls

Would this be efficient? How to do it?

         

StoutFiles

6:38 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nothing bogs down a server like repeated MySQL calls. Here's what I'm thinking of doing:

I have a database of 1000 widgets with name, color, description, id, etc. The database is updated weekly. What I'd like to do is output all this information once a week from MySQL into a php array instead of having visitors calling mysql for the information when accessing pages.

$widgetColor = array("red","green","blue","black","white");
$widgetname = array("one","two","three","four","five");
etc.

So if i went to the page example.com/widgets/three, i'd get the color blue to describe it. The thing is, to get the location for the color, I have to use a for loop to fin the number of the location.

$name = $_GET['name'];
for($i=0, $i<=1000; $i++)
{
if($widgetname[$i] == $name)
$color = $widgetColor[$i];
}

This still seems inefficient but i think better than using MySQL. Good idea or not? Anyway to make this more efficient?

RonPK

9:18 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What about using an associative array?
$widgets = array("one"=>"red", "two"=>"green", [etc.]);

That removes the need for a lookup-loop.

Also, recent versions of MySQL have query caching options for repeated identical queries on an unchanged table. So such queries should not be a bottleneck in your app.

StoutFiles

10:01 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An associative array would be a lot better, thank you.

I'd have a lot of people accessing the table...not sure if there's a limit on allowed users.