ali ak ali ak - 1 year ago 112
PHP Question

Session always returns null

I have my index.php page, which is where users can register and login. I want to display Welcome Admin once the user logs in. I have realized that session is null what can cause

to become null?



try {
$db = new PDO("mysql:host=; dbname=blog", "root", "");
} catch (PDOException $ex) {
echo $ex->getMessage();

$v = $db->prepare("select * from uyeler where uye_sifre=? and uye_ad=?");
if ($_POST) {

$isim = $_POST["isim"];
$sifre = $_POST["sifre"];

$v->execute(array($sifre, $isim));
$x = $v->fetchAll(PDO::FETCH_ASSOC);
$d = $v->rowCount();

if ($d) {
$_SESSION["ad"] = $x["uye_ad"];
$_SESSION["sife"] = $x["uye_sifre"];
$_SESSION["durum"] = $x["uye_durum"];
echo "Giriş başarılı";
} else {
echo "Geçersiz Kullanıcı";

} else {
if ($_SESSION) {
echo "adam üye";
} else {

echo '<form action="" method="post">

<table cellpadding="5" cellspacing="5">
<td>Uye Ad</td>
<td><input type="text" name="isim" id=""></td>

<td>Uye Şifre</td>
<td><input type="password" name="sifre" id=""></td>

<td><input type="submit" id=""></td>


this is the var_dump output

public 'queryString' => string 'select * from uyeler where uye_sifre=? and uye_ad=?' (length=51)
array (size=3)
'ad' => null
'sife' => null
'durum' => null

Answer Source
  1. Run your query manually to see what your query returns.
  2. fetchAll returns a multidimentional array. So fix these lines

    $_SESSION["ad"] = $x["uye_ad"];
    $_SESSION["sife"] = $x["uye_sifre"];
    $_SESSION["durum"] = $x["uye_durum"];

    to this:

    $_SESSION["ad"] = $x[0]["uye_ad"];
    $_SESSION["sife"] = $x[0]["uye_sifre"];
    $_SESSION["durum"] = $x[0]["uye_durum"];

    $x is an multidimensional and looks like this:

    $x = array(
    array('uye_ad'=>'any value', 'uye_sifre'=>'any value2', 'uye_durum'=>'any value3',)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download