Вот пример для использования PDO для соединения с DB, чтобы сказать ему выдавать исключения вместо php ошибок (поможет с отладкой), и использование параметризованных операторов вместо того, чтобы заменить динамическими значениями в запрос, самостоятельно (настоятельно рекомендованный):
// $attrs is optional, this demonstrates using persistent connections,
// the equivalent of mysql_pconnect
$attrs = array(PDO::ATTR_PERSISTENT => true);
// connect to PDO
$pdo = new PDO("mysql:host=localhost;dbname=test", "user", "password", $attrs);
// the following tells PDO we want it to throw Exceptions for every error.
// this is far more useful than the default mode of throwing php errors
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare the statement. the place holders allow PDO to handle substituting
// the values, which also prevents SQL injection
$stmt = $pdo->prepare("SELECT * FROM product WHERE productTypeId=:productTypeId AND brand=:brand");
// bind the parameters
$stmt->bindValue(":productTypeId", 6);
$stmt->bindValue(":brand", "Slurm");
// initialise an array for the results
$products = array();
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$products[] = $row;
}
}
// set PDO to null in order to close the connection
$pdo = null;
Согласно документация PHP , говорит, что необходимо быть в состоянии, чтобы сделать следующее:
$sql = "SELECT * FROM widgets WHERE something='something else'";
foreach ($database->query($sql) as $results)
{
echo $results["widget_name"];
}
я не эксперт, но это должно работать.