I am attempting to retrieve plan information for all plans attached to a specific user. They are logged in and their username is saved in the session, and I know I need to use this, in combination with the MySQL WHERE statement. Here's the code I have:
<?php
$servername = "localhost";
$username = "root";
$password = "XXX";
$dbname = "name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT plan_id, plan_name, plan_type, plan_active FROM plans WHERE user_name ='$_SESSION['user_name']'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["plan_id"]. "<br>";
echo "id: " . $row["plan_name"]. "<br>";
echo "id: " . $row["plan_type"]. "<br>";
echo "id: " . $row["plan_active"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Notice I use $_SESSION['user_name'] in the WHERE statement. What did I do wrong? Nothing gets displayed at all.
${varname[key]}; But an even better approach would be to usesprintf:$sql = sprintf("SELECT plan_id, plan_name, plan_type, plan_active FROM plans WHERE user_name ='%s'", $_SESSION['user_name']);... but again use prepared statements instead."My name is '$_SESSION['name']' and Im new to PHP."to screen. Had i actuall posted an answer i would have first mentioned this, then gone over string concatenation, then addressed the issue with the db sepcific stuff by giving an example using prepared statements..