mysql查询上的内部服务器错误500

时间:2017-12-28 08:28:38

标签: php mysql

因此我尝试使用此代码关闭时访问文件时出现内部服务器错误500。当我评论该行

$result = $conn->query("SELECT * FROM users WHERE username='$username'");
那时候一切都还好,但是当然我需要这个让我的代码工作。无法在代码中发现任何错误。完整代码如下。

<?php
include("assets/settings.php");
session_start();

$resp = array();

$username = $_POST["username"];
$password = $_POST["password"];

$resp['submitted_data'] = $_POST;

$login_status = 'invalid';

$result = $conn->query("SELECT * FROM users WHERE username='$username'");

if ($result->num_rows > 0) {
    $row = mysqli_fetch_assoc($result);
    if($row['password'] == md5($password)) {
        $login_status = 'success';
        $_SESSION["user"] = $row['id'];
    } else $login_status = 'success';
}
$login_status = 'success';
$resp['login_status'] = $login_status;

if($login_status == 'success')
{
    $resp['redirect_url'] = 'index.php';
}


echo json_encode($resp);
?>

1 个答案:

答案 0 :(得分:-1)

您的查询需要如下

setting.php

  <?php 

     $servername = "localhost";
     $username = "user"; $password = "pass"; $database = "db"; 
     // Create connection 
     $conn = new mysqli($servername, $username, $password, $database); 
     // Check connection 
     if (mysqli_connect_errno()) { 
        die("Connection failed: " .  mysqli_connect_error());

     }

     ?>

另一个文件应该是

<?php
    include("assets/settings.php");
    session_start();

    $resp = array();

    $username = $_POST["username"];
    $password = $_POST["password"];

    $resp['submitted_data'] = $_POST;

    $login_status = 'invalid';

    $result = mysqli_query( $conn ,"SELECT * FROM users WHERE username='$username'");

    if ($row = mysqli_fetch_assoc($result)) {
        if($row['password'] == md5($password)) {
            $login_status = 'success';
            $_SESSION["user"] = $row['id'];
        } else $login_status = 'success';
    }
    $login_status = 'success';
    $resp['login_status'] = $login_status;

    if($login_status == 'success')
    {
        $resp['redirect_url'] = 'index.php';
    }


    echo json_encode($resp);
    ?>

你错过了&#34; mysqli_query&#34;

$result = $conn->query("SELECT * FROM users WHERE username='$username'");

这需要改为

$result = mysqli_query( $conn ,"SELECT * FROM users WHERE username='$username'");