dongpu5874 2016-04-20 04:51
浏览 8
已采纳

php论坛登录抛出错误[重复]

This question already has an answer here:

Ive tried my best to try and fix this multiple time but something is still wrong with the code and I keep getting errors coming from the php

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/a9114464/public_html/api/api.php on line 10
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/a9114464/public_html/api/api.php on line 12

Now here is the api.php file

$conn = mysqli_connect("EXAMPLE", "EXAMPLE", "EXAMPLE");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

function login($username, $password){
$sql = "SELECT * from users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);

$check_user = mysqli_num_rows($result);
if($check_user>0){

$_SESSION[‘username’]=$username;

}

}

Thanks in advance :)

</div>
  • 写回答

2条回答 默认 最新

  • dongya4089 2016-04-20 04:53
    关注

    Solution:

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/a9114464/public_html/api/api.php on line 10

    --> $conn is defined and assigned outside function. Use global to get use $conn in the function scope.

    function login($username, $password){
     global $scope;
    

    OR, pass $conn as third parameter to login. And call login() with three parameters.

    function login($username, $password, $conn) {
    

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/a9114464/public_html/api/api.php on line 12

    --> When first problem fixed, this will also be fixed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算