dpjpo746884 2016-05-21 17:46
浏览 37
已采纳

警告:mysqli_query()期望参数1为mysqli,给定[duplicate]的整数

This question already has an answer here:

what is probably wrong with this my codes?when ever i run it,i keep on getting this error in the browser "Warning: mysqli_query() expects parameter 1 to be mysqli, integer given in C:\wamp\www\login\core\function\users.php on line 4 "..Please i need help. my codes are as below:

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);

?>

function calls

<?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);
}
?>

Please tell me whats wrong with my codes.Thanks in advance

</div>
  • 写回答

1条回答 默认 最新

  • dongxiaofa6359 2016-05-21 18:09
    关注

    Store newly connection opened by mysqli_connect in a variable then in include connection.php in another file and use that variable in 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;
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀