raqulka raqulka - 7 months ago 12
SQL Question

PDO query not selecting data from one table only

My PDO query is not selecting data from one table only, all other tables work fine.

Select query:

<?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));
?>


if I change the select to this:
$profile = $user_home->runQuery("SELECT * FROM tbl_users");


It works fine.

DB connection:

<?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;
}
}
?>


This is the table I am trying to query:

enter image description here

Thats the only table where it does not select any data- (there are 3 rows).
What could be the cause?

Answer

Forget to write where clause .

Your query would be

SELECT * FROM user_profiles WHERE user_id =:user_id

Otherwise no use for executing this

$profile->execute(array(":user_id"=>$user_id));

Updated

As per below messages the problem is occur due to special such as รครถ etc.

Use array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")) in your connection string to avoid this