【发布时间】:2016-04-29 05:21:45
【问题描述】:
我的 PDO 查询不是只从一个表中选择数据,所有其他表都可以正常工作。
选择查询:
<?php
header('Content-type: application/json');
require_once 'class.user.php';
$user_home = new USER();
$user_id = $_SESSION['userSession'];
$profile = $user_home->runQuery("SELECT * FROM user_profiles");
$profile->execute(array(":user_id"=>$user_id));
$profile_info = $profile->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(array("profile_info" => $profile_info));
?>
如果我将选择更改为:$profile = $user_home->runQuery("SELECT * FROM tbl_users");
效果很好。
数据库连接:
<?php
session_start();
class Database
{
private $host = "localhost";
private $db_name = "hoidja";
private $username = "root";
private $password = "";
public $conn;
public function dbConnection()
{
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
这是我要查询的表:
这是唯一一个不选择任何数据的表(有 3 行)。 可能是什么原因?
【问题讨论】:
-
run_query function是做什么的? -
runQuery 是公共函数 runQuery($sql) { $stmt = $this->conn->prepare($sql);返回 $stmt; }
-
so
die and dumprunQuery回复,看看你得到了什么 -
var_dump 显示如下:
D:\Programs\wamp\www\Hoidja.ee\PHP\class.user .php:19: object(PDOStatement)[2] public 'queryString' => string 'SELECT * FROM user_profiles' (length=27)