Jump to content

[SOLVED] echo/mysql


imdead

Recommended Posts

Hey Guys, Whats Wrong With This, I Cant Spot The Error. It's Not Echo'ing Anything Out >

<?php
if(session_is_registered('username')){
$sql = mysql_query("SELECT * FROM users");
	if (mysql_num_rows($sql) == 1){
	$row = mysql_fetch_assoc($sql);
	$userlevel = $row['userlevel'];
if ($userlevel === '1'){
echo"<a href='admin.php'>Admin Panel</a> // ";
}
}}
?><a href="http://jigsaw.w3.org/css-validator/check/referer">Valid CSS</a>

Link to comment
Share on other sites

I am not sure whether it will solve Or not:

change your session like:

And add mysql error reporting.

<?php
if($_SESSION('username')){
$sql = mysql_query("SELECT * FROM users") or die (mysql_error());
	if (mysql_num_rows($sql) == 1){
	$row = mysql_fetch_assoc($sql);
	$userlevel = $row['userlevel'];
if ($userlevel == '1'){
echo"<a href='admin.php'>Admin Panel</a> // ";
}
}}
?>

 

Link to comment
Share on other sites

Fatal error: Function name must be a string in C:\xampp\htdocs\footer.php on line 3

 

Also If I Add This

 

<?php
if(session_is_registered('username')){
$sql = mysql_query("SELECT * FROM users");
	if (mysql_num_rows($sql) == 1){
	$row = mysql_fetch_assoc($sql);
	$userlevel = $row['userlevel'];
if ($userlevel == '1'){
echo"<a href='admin.php'>Admin Panel</a> // ";
}
}
}else{
echo 'blah blah not logged in';
}
?>

 

When Your Not logged in, you get blah blah not logged in

Link to comment
Share on other sites

Did you try this:

<?php
if($_SESSION['username']){
$sql = mysql_query("SELECT * FROM users") or die (mysql_error());
	if (mysql_num_rows($sql) == 1){
	$row = mysql_fetch_assoc($sql);
	$userlevel = $row['userlevel'];
if ($userlevel == '1'){
echo"<a href='admin.php'>Admin Panel</a> // ";
}
else
{
echo "Error: not loged in";
}
}}
?>

Link to comment
Share on other sites

It mean that session is not registered.

try this:

and post output.

<?php
if($_SESSION['username']){
$sql = mysql_query("SELECT * FROM users") or die (mysql_error());
	if (mysql_num_rows($sql) == 1){
	$row = mysql_fetch_assoc($sql);
	$userlevel = $row['userlevel'];
if ($userlevel == '1'){
echo"<a href='admin.php'>Admin Panel</a> // ";
}
else
{
echo "Error: not loged in";
}
}}
else {
echo "session not registered";
}
?>

Link to comment
Share on other sites

You need to call session_start() prior to using the $_SESSION array.

 

<?php

session_start();
if (isset($_SESSION['username'])) {
  if ($result = mysql_query("SELECT userlevel FROM users WHERE username = '{$_SESSION['username']}'")) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($sql);
      $userlevel = $row['userlevel'];
      if ($userlevel == 1) {
        echo"<a href='admin.php'>Admin Panel</a> // ";
      }
    } else {
      echo "Error: not loged in";
    }
  }
}

?>

Link to comment
Share on other sites

<?php

if($_SESSION['username']){

$sql = mysql_query("SELECT * FROM users") or die (mysql_error());

if (mysql_num_rows($sql) == 1){

$row = mysql_fetch_assoc($sql);

$userlevel = $row['userlevel'];

if ($userlevel == '1'){

echo"<a href='admin.php'>Admin Panel</a> // ";

}else{

echo "Error: not loged in";

}

}

}else {

echo "session not registered";

}

?>

 

Suggestions:

1.) you need a where statement in your sql query in order to get one user;

2.) do a var_dump of $userlevel to see if its a string or an integer this will help you in choosing =="1" or ==1 in your if statement

3.) Are you sure you have called session_start(); on the pages that make use of this footer

Link to comment
Share on other sites

yes session start is called in the header.php

 

if i use a where

 

<?php
if($_SESSION['username']){
$sql = mysql_query("SELECT * FROM users WHERE username={$_SESSION['username']}") or die (mysql_error());
	if (mysql_num_rows($sql) == 1){
	$row = mysql_fetch_assoc($sql);
	$userlevel = $row['userlevel'];
if ($userlevel == '1'){
echo"<a href='admin.php'>Admin Panel</a> // ";
}
else
{
echo "Error: not loged in";
}
}}
else {
echo "session not registered";
}
?>

 

I Get Unknown column 'kevski' in 'where clause'

Link to comment
Share on other sites

I use sprintf when formulating a sql query thats my preference

http://www.php.net/sprintf have a read

<?php
if($_SESSION['username']){
$query = sprintf("SELECT * FROM users WHERE username='%s'", $_SESSION['username']);
$sql = mysql_query($query) or die (mysql_error());
	if (mysql_num_rows($sql) == 1){
		$row = mysql_fetch_assoc($sql);
		$userlevel = $row['userlevel'];
		if ($userlevel == '1'){
			echo"<a href='admin.php'>Admin Panel</a> // ";
		}else{
			echo "Error: not loged in";
		}
	}
}
else {
echo "session not registered";
}
?>

 

Im not sure about the line " if ($userlevel == '1'){ " because I dont know if the $userlevel variable is storing a int or string, do a var_dump($userlevel) to find this out.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.