PHP错误出现但代码仍然正确执行

时间:2012-04-20 18:59:54

标签: php mysql

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

运行此代码时出现两个错误:

<?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");
}
?>

代码执行我想要的方式,但会引发这两个错误:

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

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

当我尝试使用error_reporting(0);隐藏错误时,代码无效。

3 个答案:

答案 0 :(得分:3)

就在这一行之后

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

添加

echo(mysql_error());

这将给出你实际遇到的错误。因为它目前给出一个错误,所以它返回FALSE,这是布尔值(如输出所示。) 您的查询似乎是正确的,因此可能需要检查$user_check的值。

至于在/home/jblanksb/public_html/lock.php:9开始的标题问题输出,你肯定会在lock.php中的那一行输出一些内容。

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

答案 1 :(得分:1)

  1. 使用ob_start();在php标签后的第一行将删除标头问题。
  2. mysql_query括号结束后写.. die(mysql_error());这会给 你查询错误。我认为这是$ user_check空白然后你得到这个。尝试 将if(会话检查)放在sql查询(db检查)之前。
  3. 这将解决您的问题。

    感谢。

答案 2 :(得分:0)

问题是你的mysql_query失败了,这给了$ ses_sql值 false (当然这不是一个资源)。

使用mysql_query解决问题将阻止错误消息,这将使您的代码重定向到另一个页面。

使用mysql_error()找出MySQL查询的问题。

相关问题