我正在尝试更新以下代码以使用PDO,并且不太了解如何从上一个查询中获取结果:
现有代码:
$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;
到目前为止,我有:
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;
我真的很感激有人看一看,坦率地说,我很难过!