whiskedaway Posted July 19, 2008 Share Posted July 19, 2008 Hi everyone! I was hoping you might be able to pick up what I've done wrong. $cxn = mysqli_connect($host, $user, $password, $dbname) or die ("Connection failed."); $sql3 = "SELECT status FROM members WHERE username = '$_POST[fusername]'"; $result3 = mysqli_query($cxn, $sql3) or die ("Couldn't execute query 3."); if ($result3 == "Super Administrator") { $message = "Super Admin.<br />"; } else { $message = "You do not have permission to post comments.<br />"; } Even when the status of the user is "Super Administrator", it is skipping the if bracket and going straight to the else. So I think there's a problem, probably with the way I'm calling $result3 in the if statement. If anyone can help, it would be greatly appreciated. Thank you! Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/ Share on other sites More sharing options...
wildteen88 Posted July 19, 2008 Share Posted July 19, 2008 $result3 is a result resource from the mysql query. You need to use mysqli_fetch_assoc in order to get the data retrieved from the query. Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594182 Share on other sites More sharing options...
whiskedaway Posted July 20, 2008 Author Share Posted July 20, 2008 Thanks, I forgot that statement. *headdesk* However, it's still not working. Here's the updated code: $cxn = mysqli_connect($host, $user, $password, $dbname) or die ("Connection failed."); $sql3 = "SELECT status FROM members WHERE username = '$_POST[fusername]'"; $result3 = mysqli_query($cxn, $sql3) or die ("Couldn't execute query 3."); $staff = mysqli_fetch_assoc($result3); if ($staff == "Super Administrator") { $message = "Super Admin.<br />"; } else { $message = "You do not have permission to post comments.<br />"; } Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594497 Share on other sites More sharing options...
MasterACE14 Posted July 20, 2008 Share Posted July 20, 2008 $result3 = mysqli_query($cxn, $sql3) or die ("Couldn't execute query 3."); $staff = mysqli_fetch_assoc($result3); $staff = $staff['status']; Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594500 Share on other sites More sharing options...
whiskedaway Posted July 20, 2008 Author Share Posted July 20, 2008 It's still not working. Oh and I queried the database just to make sure it was actually returning "Super Administrator", and it is. So it's a problem with the code. Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594507 Share on other sites More sharing options...
Btown2 Posted July 20, 2008 Share Posted July 20, 2008 try this... $status = mysql_fetch_array($result3) or die(mysql_error()); if($status[0] == "Super Administrator") Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594510 Share on other sites More sharing options...
whiskedaway Posted July 20, 2008 Author Share Posted July 20, 2008 There's a problem with that statement: $staff = mysqli_fetch_array($result3) or die("Couldn't execute fetch array"); if ($staff[0] == "Super Administrator") { It brings up "Couldn't execute fetch array". Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594515 Share on other sites More sharing options...
MadTechie Posted July 20, 2008 Share Posted July 20, 2008 try this <?php $cxn = mysqli_connect($host, $user, $password, $dbname) or die ("Connection failed."); $fusername= $mysqli_real_escape_string($_POST['fusername']); //stop SQL injection $sql3 = "SELECT status FROM members WHERE username = '$fusername' "; $result3 = mysqli_query($cxn, $sql3)or die ("Couldn't execute query 3."); $staff = mysqli_fetch_assoc($result3); if ($staff['status'] == "Super Administrator") { $message = "Super Admin.<br />"; }else { $message = "You do not have permission to post comments.<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594527 Share on other sites More sharing options...
whiskedaway Posted July 20, 2008 Author Share Posted July 20, 2008 That worked! Thank you. Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594531 Share on other sites More sharing options...
MadTechie Posted July 20, 2008 Share Posted July 20, 2008 welcome, can you click topic solved please (bottom right) Link to comment https://forums.phpfreaks.com/topic/115582-solved-if-statement-problem/#findComment-594535 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.