[英]PDO query not selecting data from one table only
我的 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 行)。 可能是什么原因?
忘记写where clause
。
您的查询将是
SELECT * FROM user_profiles WHERE user_id =:user_id
否则执行此操作没有用
$profile->execute(array(":user_id"=>$user_id));
更新
根据以下消息,问题是由于äö etc.
特殊情况而发生的äö etc.
在连接字符串中使用array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"))
来避免这种情况
您需要添加session_start(); 在此行上方header('Content-type: application/json'); 在你的代码中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.