sqli_query无法正常运行


sqli_query not functioning properly

我对php脚本相当陌生,我正在尝试开发一个可以使用Android应用程序连接的sql数据库。现在,我正在测试将使用 MAMP 查询我运行的数据库的脚本。

我有两个脚本是:康恩.php

$user = 'root';
$password = 'root';
$db = 'employee101';
$host = 'localhost';
$port = 8889;
$link = mysqli_init();
$conn = mysqli_real_connect(
                               $link,
                               $host, 
                               $user, 
                               $password, 
                               $db,
                               $port
                               );
if($conn){
    echo "connection success";
}else{
    echo "connection not success";
}

登录.php

require "conn.php";
$user_name = "123";
$user_pass = "123";
$mysql_qry = "SELECT * FROM `employee_data` WHERE `username` LIKE '$user_name' AND `password` LIKE '$user_pass'";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0){
    echo "login success";
}
else{
    echo "login not success";
}

每当我使用 localhost:8888/login 测试登录脚本时.php我都会收到消息"连接成功登录不成功"回显,这意味着查询没有找到匹配项,但我确实有一个用户名为 123 和密码 123 的条目

Server: localhost:8889 
Database: employee101 
Table: employee_data

我的登录脚本有什么问题?

你真的应该使用PDO。我创建了一个骨架,你可以用它来做这件事。

https://gist.github.com/ryantxr/d587c96dd3ad33aa3885

使用 require_once,这样您的文件就不会意外加载两次。

<?php
// DATABASE-HOSTNAME-OR-IPADDRESS-GOES-HERE
// MYSQL-DBNAME-GOES-HERE
    class LoginHandler {
        public $dbHostname = 'DATABASE-HOSTNAME-OR-IPADDRESS-GOES-HERE';
        public $dbDatabaseName = 'MYSQL-DBNAME-GOES-HERE';
        public $user = 'DATABASE_USERNAME';
        public $password = 'DATABASE_PASSWORD';
        //public $port = 3307;
        public function handleRequest($arg) {
            $username = $arg['username'] ? $arg['username']: null;
            $password = $arg['password'] ? $arg['password']: null;
            if ( ! $username || ! $password ) {
                $this->fail();
                return;
            }
            try  {
                $portChunk = ( isset($this->port) ) ? ';port=' . $this->port : null;
                $dsn = "mysql:dbname={$this->dbDatabaseName};host={$this->dbHostname}{$portChunk}";
                $pdo = new PDO($dsn, $this->user, $this->password);
                $sql="SELECT * FROM `user` WHERE `username`='$username' and `password`='$password'";
                $stmt = $pdo->query($sql);
                if ( $stmt === false ) {
                    $this->fail();
                    return;
                }
                elseif ( $stmt->rowCount() > 0 ) {
                    $this->success();
                    return;
                }
                else {
                    $this->fail();
                    return;
                }
            }
            catch(PDOException $e) {
                $this->log('Connection failed: ' . $e->getMessage());
                $this->fail();
            }
        }
        function success() {
            echo json_encode(['success' => 1]);
        }
        function fail() {
            echo json_encode(['success' => 0]);
        }
        function log($msg) {
            file_put_contents("login.log", strftime('%Y-%m-%d %T ') . "$msg'n", FILE_APPEND);
        }
    }
    $handler = new LoginHandler();
    $handler->handleRequest($_POST);
    // MacBook-Pro:~ me$ curl http://PUT_YOUR_HOSTNAME/apicall.php -d"username=drum&password=pass1"
    // {"success":0}
    // MacBook-Pro:~ me$ curl http://PUT_YOUR_HOSTNAME/apicall.php -d"username=drum&password=pass0"
    // {"success":1}

这是表格定义:-

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(100) DEFAULT NULL,
  `password` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `user` (`id`, `username`, `password`)
VALUES
    (1, 'drum', 'pass');

尝试一些调试。

$mysql_qry = "SELECT * FROM `employee_data` WHERE `username` LIKE '".$user_name."' AND `password` LIKE '".$user_pass"' "; // Use single and double quotes
$result = mysqli_query($conn, $mysql_qry) or die(mysqli_error($conn)); // It will throw an error if there's an error
echo mysqli_num_rows($result); exit; // This will return a number. Either 0 or 1.

希望这有帮助。

和平! xD