I'm having a slight problem with the if else section of my PHP code:
<?php
session_start();
echo "Session connection successfully started";
$dbServername = "localhost"; //default servername for XAMPP
$dbUsername = "root"; // default username for XAMPP
$dbPassword = ""; // default password for XAMPP
$dbName = "phpmyadmin"; // database that I want to connect to
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
if (isset($_SESSION['headless']) && ($_SESSION['headless'] = 'false'))
{
echo "connected to the linux distro and nonheadless database";
$sql = "SELECT linux_distro.*, nonheadlessdistro.Proprietary,
nonheadlessdistro.EaseInstall, nonheadlessdistro.SimilarTo,
nonheadlessdistro.Gnome, nonheadlessdistro.KDE, nonheadlessdistro.LXDE,
nonheadlessdistro.XFCE, nonheadlessdistro.MATE, nonheadlessdistro.Cinna
FROM linux_distro
LEFT JOIN nonheadlessdistro ON linux_distro.Headless =
nonheadlessdistro.Headless
WHERE nonheadlessdistro.LinuxName = linux_distro.LinuxName";
$result = $conn->query($sql);
}
else
{
echo "connected to only the linux distro database";
$sql = "SELECT * FROM linux_distro";
$result = $conn->query($sql);
}
...
I'm trying to set up a database for a quiz program. $_SESSION['headless'] is a variable I'm using that determines if the user wants a headless Linux distribution or not - my first SQL table contains base information for all of the Linux distributions, while my second SQL table contains information only relevant to desktop Linux distributions. What I'm trying to achieve here that if the user wants a nonheadless Linux distribution, headless will equate to false.
My If statement is meant to check if headless is not a null value, and is equal to false, then it will join the two tables together. To confirm that it works, I added an echo statement to confirm if it is the right connection.
Otherwise, it should default to else, it will only fetch the first table, and it will echo "only connected to the linux distro database";
With the current code I have, in both circumstances (headless evaluating to not null and "false", and otherwise) I get "connected to the linux distro and nonheadless database" echo. Changing else to:
else if ($_SESSION['headless'] = null);
Results in "connected to the linux distro and nonheadless databaseconnected to only the linux distro database", in both circumstances (headless evaluating to not null and "false", and otherwise).
I'm not sure why it has decided to append the else if echo to my if echo. I can't tell which issue (else vs else if) needs to be iterated upon - both give me slightly different outputs but neither of them seem to follow the if else structure.
Aucun commentaire:
Enregistrer un commentaire