tags:

views:

145

answers:

2

I have an issue, I'm looping threw a set of values and then creating a PDO mySql query with every loop, now the problem is the first query is executing and returning results, but the second upwards aren't returning results. If I manually execute the queries on the server they return results. This is weird, maybe I'm doing something wrong here. My code below

if($num_results > 0){
 for($i=0;$i<$num_results;$i++){
   $sql_sub = "SELECT * FROM menu_config WHERE client_id =".$client_id ." AND id =".$data[$i]['root_menu_id'];

                    $results_s = $pdo->query($sql_sub);

                    $data_s = $results_s->fetchAll(PDO::FETCH_ASSOC);

                    $sub_menu_title = "<strong>".$data[$i]['title']."</strong>";

                    if(empty($data_s[0]['title'])){
                        $main_menu_title = '<span style="color:#FF0000;font-weight:bold;">No Main Menu Assigned to Sub Menu</span>';
                    }else{
                        $main_menu_title = $data_s[0]['title'];
                    }
                    $men_title = $data[$i]['title']
 }
}
+3  A: 

(this may be a little more than you asked for)

You seem to be missing out on some good things that prepared statements do.

First off, you don't usually want to pass the values directly into the query. (sometime's it's necessary, but not here). By doing that, you take out all the good stuff that protects from sql injection. Instead you want to send them as parameters after you've prepared the query.

Secondly, when in a loop, you can save yourself time and resources if you're running the same query over and over by preparing the statement, and then only changing the values you send to to that prepared statement using the PDOStatement::bindParam() function.

Thirdly, fetchAll() does not take a 'fetch_style' of PDO::FETCH_ASSOC. fetch() does. But I think you can get by with the default or none using fetchAll. You'll have to check into that and see what you need. Here are the fetchAll docs

$sql_sub = "SELECT * FROM menu_config WHERE client_id = :client_id AND id = :id ";
$query = $pdo->prepare($sql_sub);
for($i=0;$i<$num_results;$i++){
   $query->bindParam(':client_id', $client_id);
   $query->bindParam(':id', $data[$i]['root_menu_id']);
   $query->execute();

   $data_s = $query->fetchAll();
   $sub_menu_title = "<strong>".$data[$i]['title']."</strong>";

   if(empty($data_s[0]['title'])){
      $main_menu_title = '<span style="color:#FF0000;font-weight:bold;">
            No Main Menu Assigned to Sub Menu</span>';
   }else{
      $main_menu_title = $data_s[0]['title'];
   }

   $men_title = $data[$i]['title'];
}
munch
very nice rewrite, it's always better and safer to use prepared statements
Juraj Blahunka
A: 

That was clever. But i think that does not solve the PDO issue. say, if my sql query is changing for each iteration (using some if conditions or switch cases) the same issue comes back. The Issue here is one PDO connection cannot is not able to handle two simultaneous execute calls. I would love to see any solutions for this.

sri4php