php论坛登录抛出错误

时间:2016-04-20 04:51:01

标签: php html

我已尽力尝试多次修复此问题,但代码仍有问题,我不断收到来自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

现在这里是api.php文件

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

}

}

提前致谢:)

2 个答案:

答案 0 :(得分:1)

<强>解决方案:

  

警告:mysqli_query()期望参数1为mysqli,null给定   在第10行的/home/a9114464/public_html/api/api.php

- &GT; current_category = '' all_users = Users.order('category, name asc').all all_users.each_slice(2) do |two_users| <% if two_users[0].category != current_category current_category = two_users[0].category %> <tr><td colspan="2"><%= two_users[0].category %></td></tr> <% end %> <tr> <td style="text-align:right"> <%= two_users[0].name %> </td> <% if two_users[1].present? %> <td> <%= two_users[1].name %> </td> <td> <% else %> <td></td> <% end %> </tr> <% end %> 是在函数外部定义和分配的。使用$conn在函数范围内使用global

$conn

或者,将function login($username, $password){ global $scope; 作为第三个参数传递给$conn。并使用三个参数调用login

login()
  

警告:mysqli_num_rows()期望参数1为mysqli_result,   第12行/home/a9114464/public_html/api/api.php中给出的null

- &GT;当第一个问题得到解决时,这也将得到解决。

答案 1 :(得分:0)

将$ con对象传递给函数

function login($username, $password,$conn){
$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;

}

}