У меня есть какой-то древний код, который я хочу преобразовать в PDO:
<?php
function build_query() {
// db connection here
$the_query = "";
if ( empty( $_GET['c'] ) ) {
$the_query = "select * from table1";
if ( ( isset( $_GET['y'] ) ) && ( isset( $_GET['m'] ) ) ) {
$the_query .= " where y = " . $_GET['y'] . " and m = " . $_GET['m'];
}
} elseif ( ( $_GET['c'] == "1" ) || ( $_GET['c'] == "2" ) ) {
$the_query = "select * from table1 where GGG = " . $_GET['c'];
if ( ( isset( $_GET['y'] ) ) && ( isset( $_GET['m'] ) ) ) {
$the_query .= " and y = " . $_GET['y'] . " and m = " . $_GET['m'];
}
} else {
$the_query = "select * from table1";
if ( ( isset( $_GET['y'] ) ) && ( isset( $_GET['m'] ) ) ) {
$the_query .= " where y = " . $_GET['y'] . " and m = " . $_GET['m'];
}
$the_query .= " and c = " . $_GET['c'];
}
return // use the query to return results $the_data;
}
?>
Я не могу понять, как перекодировать это с помощью PDO.Я сделал старт ниже, но, похоже, не могу дальше:
<?php
function build_query() {
$the_data = "";
$DBH = new PDO( "mysql:host=server;dbname=database", "user", "pass" );
$DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$STH = $DBH -> prepare( "build query here" );
$STH -> bindParam( ':c', $_GET['c'], PDO::PARAM_INT );
$STH -> bindParam( ':y', $_GET['y'], PDO::PARAM_INT );
$STH -> bindParam( ':m', $_GET['m'], PDO::PARAM_INT );
$STH -> execute();
$ROWS = $STH -> fetchAll();
foreach($ROWS as $ROW) {
$output .= $ROW["a"] . " - " . $ROW["b"] . " - " . $ROW["c"] . " - " . $ROW["d"] . "<br />";
}
$DBH = null;
return $output;
}
?>