1

I am trying to update the following code to use PDO and cannot quite understand how to get a result from the last query:

THE EXISTING CODE:

$q=mysql_query("select * from type where parent_id=0");
    while($dd=mysql_fetch_array($q)):

     $typename = mysql_real_escape_string($dd[type_name]);

         echo "<span class='category-title'>" . $typename . " </span>";


      $r2=mysql_query("select * from type where parent_id=".$dd[id]);

        while($d3=mysql_fetch_array($r2)):

$rr=mysql_query("select * from code where lid=" . $d[id] . " and pid=" .$d3[id]);



            if(mysql_num_rows($rr)):

    echo "" . mysql_real_escape_string($d3[type_name]) .", ";   

            endif;

    endwhile;

endwhile;

So far I have:

  try{
   $stmt = $dbh->prepare('SELECT * FROM type  WHERE parent_id = 0 ');
   // THIS SELECTS ALL THE CATEGORY TITLES ONLY
   $stmt->setFetchMode(PDO::FETCH_ASSOC);
   $stmt->execute();
 } catch (PDOException $e) {

  print "Error selecting FROM  type!: " . $e->getMessage() . "<br/>";
                          die();
                  }

// ** LOOP THROUGH RESULTS FOR TYPE CATEGORIES**//
          foreach ($stmt->fetchAll() as $row):

//** OUTPUT THE CATEGORY TITLES **//              
          echo "<strong>".$row['type_name'] . "</strong><br />\t";



try{                       
   $stmt = $dbh->prepare('SELECT * FROM type  WHERE parent_id = :value ');
   // THIS SELECTS THE SPECIFIC CATEGORY DATA **//
   $stmt->bindParam(':value', $row ['id']);
   $stmt->setFetchMode(PDO::FETCH_ASSOC);
   $stmt->execute();
    } catch (PDOException $e) {
   print "Error selecting FROM type!: " . $e->getMessage() . "<br/>";
 die();
}

foreach ($stmt->fetchAll() as $row):
  $array[] = $row['type_name'];


try{                       
$stmt = $dbh->prepare('SELECT * FROM code_data  WHERE lid = :value AND pid = :value ');
$stmt->bindParam(':value', $id);
$stmt->bindParam(':value', $row['id']);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$num_rows = $stmt->fetchAll();
      } catch (PDOException $e) {
   print "Error selecting FROM code_data!: " . $e->getMessage() . "<br/>";
  die();
                  } 


  if ($num_rows > 1):

    echo $row['type_name']. "<br />";// THIS JUST OUTPUTS ALL THE DATA


           endif;
                  endforeach;
    endforeach;
 endif;

I really would appreciate somebody taking a look at this as quite frankly I am stumped!

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Throw `error_reporting(-1);` at the top of your script and fix all the errors. Also, please indent your code properly; it is impossible to follow the flow of your code. – Sverri M. Olsen Feb 11 '14 at 21:25
  • 1
    What's wrong with what you have now? – Madara's Ghost Feb 11 '14 at 21:26
  • You could combine the two `select * from type` queries into a single joined one. pretty much anytime you have nested loops and the inner loop depends on data from the outer one, a join could replace both. – Marc B Feb 11 '14 at 21:29
  • Basically the returned results should look like this: Category1 subcat subcat Category2 subcat subcat and so on. I get a list of the Categories but ALL the subcats are being returned for each Category instead of just the relevant ones. – GalaxyTramp Feb 11 '14 at 21:29

0 Answers0