【发布时间】:2015-03-25 17:21:12
【问题描述】:
我是新来的。我已经搜索了所有但无法获得确切答案。我在 login.php 中执行 mysql_query 但它不工作。我尝试通过在 mysql_query 之后放置一个 if 条件来进行检查,并确认查询没有将结果返回为 true。我是否未能建立与 mysql 数据库的正确连接?请指导。
//登录.php
<?php
include_once("Connection.php");
if(!$con) {
die ("Cannot Connect" . mysql_error());
}
$username = $_POST['UserName'];
$password = $_POST['Password'];
$result = "SELECT * FROM log WHERE username = $username AND userpass = $password";
$sql=mysqli_query($con,$result);
if($sql == FALSE) {
echo "this"; // TODO: better error handling
}
else{
$res=mysql_fetch_array($sql);
}
$num_rows = mysql_num_rows($res);
echo $num_rows;
if ($num_rows > 0) {
session_start();
$_SESSION['login'] = "1";
header ("location: path.html");
}
else{
echo $num_rows;
//header ("location: index.htm");
//exit ();
}
?>
<?php
$con=mysqlI_connect("localhost","root","","taxisystem" ); /* Connecting to sql */
/* we have used $con to store it in variable as we will have to use it multiple times! */
/*update: Now we will use connection.php instead! :) */
?>
【问题讨论】:
-
你得到什么错误??
-
目前我已经放置了这个 if-else 条件并且 if 条件正在打印这个。证明 $res 不正确。另外,如果我删除了这些 if else 子句,则 mysql_fetch_array() 期望参数 1 是资源。
标签: php mysql login error-handling database-connection