0

I have different paramter of particular mysql query I am running so I need to run them in bunch.. so say

$server = 'server_';
for ($a=0 ; $a<5 ; ++$a) {
     $criteria = $server . $a;
     $query1 = 'select blabh blah not important ' . $criteria . 'order by date desc limit 10';
     $query_result = mysql_query($query1);

}

Above only comes back results for server_4

I naively thought I could just do

$query_result .= mysql_query($query1);

But clearly that doesn't work. I hope no one says why don't you run mysql as like '$server%'

I am looking for to see if what I am trying to do is possible. appending.. I guess string append is possible but I perahps simply don't understand what's coming back from mysql?

fetch code sample

select * from tableName where server like $criteria order by date desc limit 10

========================================================= code sample

$data = array(); 
$dataMaster = array();

   for ( $x = 0; $x <= 8; $x++ ) {
           $server = ‘server_’ . $x;
           $myquery = 'select * from serverTable where  servername like ' . '"' .  $server . '"'  . ' order by date1 desc limit 100';


           $query = mysql_query($myquery);
           if ( ! $query ) {
                   echo mysql_error();
                   die;
           }
           for ( $x = 0; $x < mysql_num_rows($query); $x++ ) {
              $data[] = mysql_fetch_assoc($query);
           }

           $data = array_reverse($data);
            array_push($dataMaster, $data);
   }

   echo json_encode($dataMaster)
14
  • Where are you fetching? $query_result is only going to be the last iteration. Commented Aug 10, 2016 at 21:28
  • so am I just not thinking in mysql way? Is there no way to append to (whatever is coming back from mysql? like array?) Commented Aug 10, 2016 at 21:29
  • 1
    The fact that you only see the last one indicates that you're overwriting your result variable with each execution rather than appending to it. How to append rather than overwriting depends on how you're fetching the results, which isn't shown in your question. Commented Aug 10, 2016 at 21:33
  • 1
    Update the question with the code that is fetching. I'd also consider, dev.mysql.com/doc/refman/5.7/en/union.html. Commented Aug 10, 2016 at 21:33
  • 1
    you dont need 5 queries, you could do this in one Commented Aug 10, 2016 at 21:51

1 Answer 1

2

Try to avoid SQL inside PHP loop, you can do the following instead:

SELECT * FROM serverTable WHERE servername IN 
('server_0', 'server_1', 'server_2', 'server_3', 'server_4')
Sign up to request clarification or add additional context in comments.

2 Comments

I am also looking at this.
Thank you. It turns out, this is what I needed. I guess in SQL, you don't run inside of for loop in php and I can see why since I can always do something like this. This got what I needed . thank you and accepted!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.