superglobals failure?

Codes here !

Moderators: macek, egami, gesf

Post Reply
johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: superglobals failure?

Post by johnj » Tue Jun 26, 2012 9:08 pm

If $_SESSION['username'] =="", that tells that your query returned '0' results. You need to check whether the user name and password that was submitted through the form is indeed picked by the sql query.

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 359
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: superglobals failure?

Post by freshnet » Wed Jun 27, 2012 6:35 am

Here's the problem code. Firstly you shouldn't be using mysql_query as its well outdated, but leaving that aside. You are executing a query that will return either FALSE or a resource. Neither of those are countable, so the count will always be zero.

Code: Select all

$result = mysql_query($query, MYSQL_ASSOC);
if(count($result) == 0) {
   $_SESSION['username'] = "";
} else {
   $_SESSION['username'] = $result['login'];
}
What you probably wanted to do was something like this...

Code: Select all

$queryResult = mysql_query($query, MYSQL_ASSOC);
$result = mysql_fetch_row($queryResult );
if($result == FALSE) //no rows are returned
{
   $_SESSION['username'] = "";
} else {
   $_SESSION['username'] = $result['login'];
}

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 359
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: superglobals failure?

Post by freshnet » Wed Jun 27, 2012 12:31 pm

$_POST variables are mapped by the field name, not id. You need to use name='username' etc., not the id.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests