在登录时遇到此错误,我安装了新版本的xampp / php仍然面临着很多mysqli的问题......
下面是login.php的代码,然后转到lock.php,代码在下面......
<?php
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from form
$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);
$sql="SELECT id FROM mc_admin WHERE username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;
header("location: home.php");
}
else
{
$error="Your Login Name or Password is invalid";
}
}
?>
下面的lock.php代码:
<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysql_query("select username from mc_admin where username='$user_check' ");
$row=mysql_fetch_array($ses_sql);
$login_session=$row['username'];
if(!isset($login_session))
{
header("Location: login.php");
}
?>
答案 0 :(得分:6)
MYSQL查询在出错时返回FALSE,因此您的一个查询在执行时出错。
根据PHP文档mysql_query() returns a resource on success, or FALSE on error
所以在尝试调用mysql_fetch_array
检查之前,如下所示:
您可以打印错误
$result = mysql_query('your query');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
来代替mysql_ *
答案 1 :(得分:-1)
转到mysqli!检查一下我是如何解决我的。当查询是INSERT时,我收到相同的错误,因为它返回空,所以我只在需要时调用fetch_assoc()
。
<强> mysqlcon.php 强>
<?php
function mysqlquery($query,$dbcon,$returnValue = false){
$result = $dbcon->query($query);
if(!$result){
die('There was an error running the query [' . $dbcon->error . ']');
}
//Do not use `fetch_assoc()` in INSERT query to prevent
//`PHP Fatal error: Call to a member function fetch_assoc() on a non-object` error.
if($returnValue){
return $result->fetch_assoc();
}
}
function dbcon(){
$db_address = 'localhost';
$db_user = 'root';
$passwd = 'your_password';
$db = 'your_db';
$dbcon = new mysqli($db_address, $db_user, $passwd, $db);
if($dbcon->connect_errno > 0){
die('Unable to connect to database [' . $dbcon->connect_error . ']');
}
return $dbcon;
}
使用示例:
<强> exaple.php 强>
<?php
require_once('mysqlcon.php');
$dbcon = dbcon();
$result = mysqlquery("SELECT * FROM TABLE WHERE COLUMN='hello'",$dbcon,true);
echo $res = $result['db_column'];