如何将此语句转换为mysqli。我尝试转换它但不起作用。我错过了什么我想连接到登录表单。
这是我转换的代码。
<?php
include('admin/dbcon.php');
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
/* teacher */
$query_teacher = mysql_query("SELECT * FROM teacher WHERE username='$username' AND password='$password'")or die(mysql_error());
$num_row_teacher = mysql_num_rows($query_teacher);
$row_teahcer = mysql_fetch_array($query_teacher);
/* admin */
$query_admin = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'")or die(mysql_error());
$num_row_admin = mysql_num_rows($query_admin);
$row_admin = mysql_fetch_array($query_admin);
if ($num_row_teacher > 0){
$_SESSION['id']=$row_teahcer['teacher_id'];
echo 'true';
}else if ($num_row_admin > 0){
$_SESSION['id']=$row_admin['user_id'];
echo 'true_admin';
}else{
echo 'false';
}
?>
这是转换后的mysqli,但我仍然无法登录,我错过了一些东西。如果你能帮助我解决这个问题,我将非常感激。
<?php
// establishing the MySQLi connection
$con = mysqli_connect("localhost","root","","retreat");
if (mysqli_connect_errno())
{
echo "MySQLi Connection was not established: " . mysqli_connect_error();
}
// checking the user
session_start();
$username = mysqli_real_escape_string($con,$_POST['username'];
$password = mysqli_real_escape_string($con,$_POST['password'];
/* teacher */
$query_teacher = "SELECT * FROM teacher WHERE username='$username' AND password='$password'";
$num_row_teacher = mysqli_query($con,$query_teacher);
$row_teahcer = mysqli_num_rows($num_row_teacher);
/* admin */
$query_admin = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$num_row_admin = mysqli_query($con,$query_admin);
$row_admin = mysqli_num_rows($num_row_admin);
if ($row_teahcer > 0){
//$_SESSION['user_email']=$email;
$_SESSION['']=$row_teacher['teacher_id'];
echo 'true';
}else if ($num_row_admin > 0){
$_SESSION['id']=$row_admin['user_id'];
echo 'true_admin';
}else{
echo 'false';
}
?>
答案 0 :(得分:0)
<?php
//db details
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'retreat';
//Connect and select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
/* teacher */
$query = $db->query("SELECT * FROM teacher WHERE username='$username' AND password='$password'")or die(mysqli_error());
$num_row_teacher = mysqli_num_rows($query);
$row_teahcer = mysqli_fetch_array($query);
/* admin */
$query_admin = $db->query("SELECT * FROM users WHERE username='$username' AND password='$password'")or die(mysqli_error());
$num_row_admin = mysqli_num_rows($query_admin);
$row_admin = mysqli_fetch_array($query_admin);
if ($num_row_teacher > 0){
$_SESSION['id']=$row_teahcer['teacher_id'];
echo 'true';
}else if ($num_row_admin > 0){
$_SESSION['id']=$row_admin['user_id'];
echo 'true_admin';
}else{
echo 'false';
}
enter code here
?>
答案 1 :(得分:0)
你遗失了
$row_teahcer=mysqli_fetch_array($num_row_teacher);
$row_admin=mysqli_fetch_array( $num_row_admin);