0

Reaching out for a second pair of eyes.

I just don't get what the issue is here, I've been using the same format throughout out my whole site until now I get this error:

<?php
session_start();

include"lib/config.php";

$logged_user = $_SESSION['username'];

$check_admin = "SELECT * FROM `members` WHERE `is_admin` = ".sql_val($logged_user);
$result = $conn->query($check_admin);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
$members_id = $row['members_id'];
$is_admin = $row['is_admin'];
$is_mod = $row['is_mod'];
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
$login_date = $row['login_date'];
$login_time = $row['login_time'];
$session = $row['session'];
}}

?>


<?php

if ($is_admin == 1){

echo 'Is Admin';
}
else
{

echo 'Is not Admin';
}

?>

this is my output:

Notice: Undefined variable: is_admin in C:\wwwroot\htdocs\snippet\is_admin.php on line 30 Is not Admin

which don't make sence.

TrevTech
  • 49
  • 6
  • place your `if $is_admin` inside the while loop. and if `$logged_user` is a string, you need to quote that in your query and make sure there's a value for it. *blah blah blah*. – Funk Forty Niner Feb 17 '16 at 18:49
  • You're defining `$is_admin` inside your while loop, which means it's only available inside there. See [Link](http://stackoverflow.com/questions/7337743/using-a-variable-outside-of-the-while-loop-scope) aswell. – ccKep Feb 17 '16 at 18:49
  • *"which don't make sence."* - Yes it does, it makes *perfect* sense. – Funk Forty Niner Feb 17 '16 at 18:55
  • @ccKep [That's not accurate, as proven further down on your link.](http://stackoverflow.com/questions/7337743/using-a-variable-outside-of-the-while-loop-scope#comment8849566_7337865) – Litty Feb 17 '16 at 18:56
  • @Litty I stand corrected, thanks! – ccKep Feb 17 '16 at 18:57
  • @Fred-ii- The question was only asked *13 minutes ago.* – Litty Feb 17 '16 at 19:01
  • @Litty Umm.... did you even look at their track record before pinging me? – Funk Forty Niner Feb 17 '16 at 19:02
  • Thank you, everyone, I learnt something new today. This is very new to me, I'm used to the old format with mysql, so still learning. – TrevTech Feb 17 '16 at 19:06
  • @Fred-ii- I'm working on the question at hand in the present, not digging up the user's past. No argument though, he should be accepting answers. – Litty Feb 17 '16 at 19:07
  • 1
    @Litty Sincerely wishing you well with your answer ;-) – Funk Forty Niner Feb 17 '16 at 19:09

2 Answers2

1

You need to define $is_admin on top of file as a null. Because if your if condition will not satisfy then you will not get $is_admin at the end of the file.

Like this :

<?php
session_start();

include"lib/config.php";
$is_admin = "";
$logged_user = $_SESSION['username'];

$check_admin = "SELECT * FROM `members` WHERE `is_admin` = ".sql_val($logged_user);
$result = $conn->query($check_admin);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
$members_id = $row['members_id'];
$is_admin = $row['is_admin'];
$is_mod = $row['is_mod'];
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
$login_date = $row['login_date'];
$login_time = $row['login_time'];
$session = $row['session'];
}}

?>


<?php

if ($is_admin == 1){

echo 'Is Admin';
}
else
{

echo 'Is not Admin';
}

?>
Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
  • Although this makes the notice go away, keep in mind that this might not "fix the real problem." Shouldn't `$is_admin` be a strict true or false? Allowing it to be a null value means a member wasn't found -- but in this case, a member should *probably* always be found. – Litty Feb 17 '16 at 19:03
0

When checking if $is_admin == 1, the variable $is_admin is not defined. In your code, this means...

  • Your SQL query returned no rows, so that while loop is never iterated. Is your table filled? Are you querying the correct table?

  • $row does not contain a value for $row['is_admin']. Is that column in your table? For this row, is the value NULL?

Litty
  • 1,856
  • 1
  • 16
  • 35