mysqli_query无效。缺少连接?

时间:2015-01-27 06:40:18

标签: php mysql login error-handling database-connection

我是新来的。我搜索了所有但无法得到确切的答案。我正在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! :) */


?>

2 个答案:

答案 0 :(得分:0)

应该是 -

$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 ();
}


?>

请做一些逃避。

答案 1 :(得分:0)

您的查询不正确。您必须在变量周围添加单引号,因为这是char类型。

$result = "SELECT * FROM log WHERE username = '$username' AND userpass = '$password'";

使用sql的更好方法是准备语句。

另一个问题是连接类型必须是mysqli而不是mysqlI

$con=mysqli_connect("localhost","root","","taxisystem" );