0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I get two errors when running this code:

<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];

$ses_sql=mysql_query("select username from admin where username='$user_check' ");

$row=mysql_fetch_array($ses_sql);

$login_session=$row['username'];

if(!isset($login_session))
{
header("Location: login.php");
}
?>

The code executes how I want it to but throws up these two errors:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/jblanksb/public_html/lock.php on line 9

and

Warning: Cannot modify header information - headers already sent by (output started at /home/jblanksb/public_html/lock.php:9) in /home/jblanksb/public_html/lock.php on line 15

When I try and hide the errors with error_reporting(0); the code doesn't work.

Community
  • 1
  • 1
blanksby
  • 27
  • 6
  • 1
    Please stop writing new code with the ancient `mysql_*` functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799) . Instead you should learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysql). If you care to learn, [here](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) is a quite good PDO-related tutorial. – tereško Apr 20 '12 at 19:09

3 Answers3

3

Just after this line

$ses_sql=mysql_query("select username from admin where username='$user_check' ");

add

echo(mysql_error());

which will give the error you are actually encountering.As it presently gives an error so it return FALSE which is boolean(as the output says.) Your query appears to be correct so maybe its the value of $user_check you need to check.

As for the header problem output started at /home/jblanksb/public_html/lock.php:9 you are surely outputting something at that line in lock.php.

http://php.net/manual/en/function.header.php

Sankalp
  • 1,108
  • 4
  • 14
  • 30
  • Ofcourse you are getting connected to the DB that's why you are getting the error in the first place. – Sankalp Apr 20 '12 at 19:17
  • Perfect! What a rookie mistake, turns out it should have been `$ses_sql=mysql_query("select user from admin where user='$user_check' ");` Wow I feel stupid.. But with this first error fixed the second has gone too? Thank you – blanksby Apr 20 '12 at 19:20
  • as i said you were outpitting something on those lines.turns out they were the errors that were being printed out so the header error. – Sankalp Apr 20 '12 at 19:36
  • Obviously, the code you posted is from `link.php`, so, when error happened, PHP outputed text of this error, that's why you had no ability to modify headers after – Timur Apr 20 '12 at 19:40
1
  1. use ob_start(); at the first line after php tag will remove the header problem.
  2. After the end of mysql_query bracket write.. die (mysql_error()); This will give you the query error. I think this is when $user_check is blank then u get this. Try to put the if(session checking ) before the sql query (db checking) .

This will solve your problem.

Thanks.

Timur
  • 6,578
  • 1
  • 25
  • 37
Tuhin Subhra Dey
  • 980
  • 6
  • 19
0

The problem is that your mysql_query fails, which gives $ses_sql the value false (which of course is not a resource).

Solving the problem with your mysql_query will prevent the error message, which will make your code redirect you to another page.

Use mysql_error() to find out what the problem is with your MySQL query.

Simon Forsberg
  • 12,789
  • 10
  • 59
  • 101