我是PHP的新手,所以请耐心等待。
我编写了一个PHP代码片段,用于从MySQL数据库中检索复杂的数据结构。 (Think"有一个"对象关系。)我有一个SQL查询,可以将结果转换为PHP对象列表。这些PHP对象包含在其他MySQL表中定义的PHP对象(有时以数组的形式)。因此,第一个对象的构造函数在构造时必须执行自己的SQL查询,以构建内部对象。不幸的是,我不习惯这样做,所以我不断收到这个错误:
数据库错误:SQLSTATE [HY093]:参数号无效:参数未定义
如何设置它以便在调用get_tickets.php时它没有这些SQL查询问题?
<?php
// These variables define the connection information for your MySQL database
$dbhost = '####';
$dbuser = '####';
$dbpass = '####';
$dbname = '####';
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
// Attempt to connect to the database
try {
$db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8",
$dbuser, $dbpass, $options);
} catch (PDOException $ex) {
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// Disable magic quotes
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
// Tell browser to use UTF-8 encoding
header('Content-Type: text/html; charset=utf-8');
// Start the session
if(!isset($_SESSION)){
session_start();
}
?>
<?php
require_once('config.php');
require('data_access_objects.php');
// Check for hash
if (isset($_POST['hash'])) {
// Get user's hash
$hash = $_POST['hash'];
// Result array to return
$results = Array();
try {
// Get all ticket entries
$query = "SELECT *
FROM tickets
WHERE user = (
SELECT id
FROM users
WHERE hash = :hash)";
$stmt = $db->prepare($query);
$stmt->bindValue(':hash', $hash, PDO::PARAM_STR);
$stmt->execute();
// Convert cursor entries into class objects
while($row = $stmt->fetch()) {
array_push($results, new Ticket($row['id'], $row['status'], $row['room'],
$row['location'], $row['gps_loc'], $row['subject'],
$row['picture'], NULL));
}
// Display resulting array entries
echo json_encode(array_values($results));
} catch (PDOException $ex) {
// Display generic error on page
echo 'Database error: ' . $ex->getMessage();
}
} else {
// Failed, return nothing
echo '';
}
?>
<?php
/* Room data access object structure */
class Room
{
var $id;
var $name;
var $building;
function __construct($id, $name, $building)
{
$this->id = $id;
$this->name = $name;
$this->building = $building;
}
}
/* Ticket data access object structure */
class Ticket
{
var $id;
...
var $room;
...
var $messages = Array();
function __construct($id, $status, $room, $location, $gpsLocation,
$subject, $picture, $messages)
{
$this->id = $id;
...
$this->retrieveRoom($room);
...
$this->messages = $messages;
}
private function retrieveRoom($room_id)
{
require_once('config.php');
try {
// Get all ticket entries
$query = "SELECT *
FROM rooms
WHERE id = :room_id)";
$stmt = $db->prepare($query);
$stmt->bindValue(':id', $room_id, PDO::PARAM_INT);
$stmt->execute();
// Convert cursor entrie into room object
$row = $stmt->fetch();
$this->room = new Room($row['id'], $row['name'], $row['building']);
} catch (PDOException $ex) {
// Display generic error on page
echo 'Database error: ' . $ex->getMessage();
}''
}
...
}
?>
答案 0 :(得分:1)
看起来PDO抱怨无效参数,我确切地检查了你的方法绑定了什么:
private function retrieveRoom($room_id)
为了保护,在发出查询之前,$ room_id是数字。
我会补充说你当前的方法效率低下。您应该缩小查询并从已连接的记录中创建对象。