简体   繁体   English

sqli_query 无法正常运行

[英]sqli_query not functioning properly

I'm fairly new to php scripting and i'm trying to develop a sql database that i can connect to using a android app.我对 php 脚本相当陌生,我正在尝试开发一个 sql 数据库,我可以使用 android 应用程序连接到该数据库。 Right now i'm testing the scripts that will query the database i have running using MAMP.现在我正在测试将查询我使用 MAMP 运行的数据库的脚本。

The two scripts i have are : conn.php我拥有的两个脚本是:conn.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";
}

login.php登录.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";
}

Whenever i test the login script using localhost:8888/login.php i get the message "connection successlogin not success" echo which is meant to say that the query didn't find a match, but i do indeed have a entry with the username 123 and password 123每当我使用 localhost:8888/login.php 测试登录脚本时,我都会收到消息“connection successlogin not success”echo,意思是说查询没有找到匹配项,但我确实有一个带有用户名的条目123和密码123

Server: localhost:8889 
Database: employee101 
Table: employee_data

What is wrong with my login script?我的登录脚本有什么问题?

Try a bit of debugging.尝试一些调试。

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

Hope this helps.希望这可以帮助。

Peace!和平! xD xD

You really should be using PDO.你真的应该使用 PDO。 I created a skeleton that you can use for this.我创建了一个可以用于此目的的骨架。

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

use require_once so your file does not accidentally get loaded twice.使用 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}

Here is the table def :-这是表定义:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM