我是php脚本的新手,我正在尝试开发一个可以使用Android应用程序连接的sql数据库。现在我正在测试将查询我使用MAMP运行的数据库的脚本。
我拥有的两个脚本是: 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
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测试登录脚本时,我收到消息“connection successlogin not success”echo,这意味着查询没有找到匹配项,但我确实有一个条目使用用户名123和密码123
Server: localhost:8889
Database: employee101
Table: employee_data
我的登录脚本有什么问题?
答案 0 :(得分:0)
尝试一些调试。
$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
答案 1 :(得分:0)
你真的应该使用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}
这是表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');