mysqli_query()和mysqli_fetch_array错误

时间:2019-03-13 14:41:03

标签: php mysqli mysqli-fetch-array

下面的代码是connect_db.php

<?php
 $dbhost = 'localhost';
 $dbuser = 'root';
 $dbpass = '';
 $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

 if(! $conn ){
 die('Could not connect: ' . mysqli_error());
 }
 echo 'Connected successfully';
 mysqli_close($conn);
 ?>

下面的index.php代码

<?php
include_once 'connect_db.php';
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$position = $_POST['position'];
switch ($position) {
case 'Admin':
$result = mysqli_query($conn, "SELECT admin_id, username FROM admin WHERE username='{$username}' AND password='{$password}'");
$row = mysqli_fetch_array($result);
if ($row > 0) {
session_start();
$_SESSION['admin_id'] = $row[0];
$_SESSION['username'] = $row[1];
header('location:http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/admin.php');
} else {
$message = '<font color=red>Invalid login Try Again</font>';
}
break;
}}
?>

运行index.php文件时,我在第9行和第10行出现错误

1 个答案:

答案 0 :(得分:2)

mysql_query()已在PHP7中删除。

如果不确定所运行的php版本,请编写以下代码:

echo 'PHP version: ' . phpversion();

您还可以使用function_exists()函数来查看该函数是否存在。

替代品:
PDO(https://secure.php.net/manual/en/book.pdo.php
mysqli(https://secure.php.net/manual/en/book.mysqli.php

更多信息的链接:
https://secure.php.net/manual/en/function.function-exists.php https://secure.php.net/manual/en/function.mysql-query.php