我是新来的。我已经搜索了所有内容,但无法获得确切答案。我正在login.php中执行mysql_query,但是它不起作用。我尝试通过在mysql_query之后放置一个if条件来检查,并确认查询未将结果返回为true。建立与mysql数据库的正确连接失败了吗?请指导。
//login.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 ();
}
?>
//connection.php
<?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! :) */
?>
最佳答案
它应该是 -
$con=mysqli_connect("localhost","root","","taxisystem" );
在连接页面上。并且您将其与mysql混合在一起-
<?php
include_once("Connection.php");
if(!$con) {
die ("Cannot Connect" . mysqli_error($con));
}
$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=mysqli_fetch_array($sql);
}
$num_rows = mysqli_num_rows($sql);
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 - mysqli_query不起作用。缺少连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28164640/