我的代码可能有什么问题?当我运行它时,我继续在浏览器中收到此错误"警告:mysqli_query()期望参数1为mysqli,C:\ wamp中给出的整数第4行\ www \ login \ core \ function \ users.php " ..我需要帮助。 我的代码如下:
user.php的
<?php
require('core/database/connect.php');
global $username;
function user_exists($con,$username){
$username = sanitize($username);
$query = mysqli_query($con,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'") OR die(mysqli_error());
return (mysqli_fetch_assoc($query) ==1) ? true : false;
}
function user_active($username){
$username = sanitize($username);
$query = mysqli_query($con,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1") OR die(mysqli_error());
return (mysqli_fetch_assoc($query) ==1) ? true : false;
}
?>
** connect.php ***
<?php
$connect_error = 'sorry,we are experiencing connection problem.';
$con = mysqli_connect('localhost','root','') or die($connect_error);
mysqli_select_db($con,'login_registration') or die($connect_error);
?>
函数调用
<?php
include 'core/init.php';
if(empty($_POST) === false){
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) || empty($password)){
$errors[] = 'You need to enter a username and password';
}else if(user_exists($con,$username) === false){
$errors[] = 'The account dosen\'t exit.Please register';
}else if(user_active($username ) === false){
$errors[] = 'Please u have not login into ur account';
}else{
//here
}
print_r($errors);
}
?>
请告诉我我的代码有什么问题。提前谢谢
答案 0 :(得分:0)
将mysqli_connect
打开的新连接存储在变量中,然后将connection.php
包含在另一个文件中,并在user.php
中使用该变量。
<强> connection.php 强>
<?php
$con = mysqli_connect('127.0.0.1','root','');
?>
<强> user.php的强>
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . 'Project Name/' . 'core/database/connection.php');
function user_exists($username){
$username = sanitize($username);
$query = mysqli_query($con, "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'") OR die(mysql_error());
return (mysql_result($query,0) ==1) ? true : false;
}
function user_active($username){
$username = sanitize($username);
$query = mysqli_query($con, "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1") OR die(mysql_error());
return (mysql_result($query,0) ==1) ? true : false;
}
?>