I have a DB with two tables in it : Users and Commands. I’m trying to print on a web page a different array for the purchases of every user, based on their user ID.
I have this code :
try
{
$mysqlClient = new PDO(
'mysql:host=$host;dbname=FirstDB;charset=utf8',
'$user',
'$pwd'
);
}
catch (Excetion $e)
{
die(Error : ' . $e->getMessage());
}
$UsersSelectQuery = 'SELECT * FROM Users';
$UsersStmt = $mysqlClient->prepare($UsersSelectQuery);
$UsersStmt->execute();
$UsersRows = array($UsersStmt->fetchAll());
foreach ($UsersRows as $Row)
{
$sql = 'SELECT * FROM Commands WHERE UserID=?';
$stmt = $mysqlClient->prepare($sql);
$stmt->bind_param("i", $Row);
$stmt->execute();
$result = $stmt->get_result();
$response = $result->fetchAll();
echo '<pre>'; print_r($response); echo '</pre>';
}
This code results in a blank page and my “$response” variable is empty. I guess that the way I include the “foreach” auto PHP variable ($Row) in the SQL “select” query is wrong.
I’ve tried everything I found on the web to concatenate the PHP variable in the SQL query (with dots, single and double quotes, prepared statements, placeholders, params…), but each time, the “$response” variable is empty.
If I set a fixed value instead of the “$Row” variable (for example “1”), the array variable does display data on the web page (with the “print_r” command).
Does anyone know what I’m doing wrong ?
Thank you !