Jump to content

How to have less query?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
look at below code
<?php

function checkplace($name)
{
    $query = mysql_query("SELECT * FROM `place` WHERE name=$name");
    while($p = mysql_fetch_array($query))
    {
        echo $p['name'];
    }
    
}

if($codecall == 'open')
{
    checkplace('open');
    //Some code here   
}
if($codecall == 'close')
{
    checkplace('close');
    //Some code here
}
if($codecall == 'good')
{
    checkplace('good');
    //Some code here
}

//Some code here
checkplace('all');
?>
I call function checkplace() many time in my files and this make many query reading now I want to have sth which make it less?
guide me to have less one ?
One Query or Two!

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
If you are calling many queries on the same page, to access the same resource, then you may as well put this up top:
$place = mysql_query("SELECT * FROM `place`");

function checkplace($name)
{
    global $place;
    while($p = mysql_fetch_assoc($place))
    {
        echo $p[$name]['name']; //edit here
    }
    
}

Your checkplace() function should get the data from $place instead of mysql_query(). This will make only one query used for the life of the script. You may need to edit the line that I commented on, I'm not sure how the array will be set up.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Queries are not the same!
[COLOR=#000000][COLOR=#dd0000]SELECT * FROM `place` WHERE name=[/COLOR][COLOR=#0000bb]$name[/COLOR][/COLOR]
name will change when place of code change.
$place = mysql_query("SELECT * FROM `place`"); 
get all I want but I don't know how check that there is sth for esp place?
look
I read all data from place and make array by the name $checkplace
one item of array will be:
array(
name=>$name,
place=>$place);
now how to check that we have sth for esp place?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You cannot reduce the number of queries on your original code, I simply gave you an alternate way to program your application, that only uses a single query.

If you put "SELECT * FROM `place`" it will select ALL entries, not just one, so you can choose from that single result with checkplace().
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
I need sth that I select all entries with one query and check them with one function!

Nullw0rm said:

You cannot reduce the number of queries on your original code, I simply gave you an alternate way to program your application, that only uses a single query.

If you put "SELECT * FROM `place`" it will select ALL entries, not just one, so you can choose from that single result with checkplace().


#6
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
any other idea?

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
almost as nullw0rm wrote, but preload them instead.
$place = array();
$res = mysql_query("SELECT name FROM `place`");
while ($row = mysql_fetch_assoc($res)) {
  $place[] = $row['place'];
}

function checkplace($pl) {
   global $place
   if (in_array($pl, $place) {
     echo $pl;
  }
}

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall