douyi8315 2016-03-02 23:27
浏览 85
已采纳

mysqli_query没有输出[关闭]

I've been trying to create a simple login script with php and mysql. I'm able to connect to the database and enter data into it, but I can't use SELECT to get data from the DB.

This is my register code (which works):

<?php

include_once "dbconnect.php";

$email = $_POST['email'];
$username = $_POST['username'];
$password = md5($_POST['password']);

$insert = "INSERT INTO users (username, email, password) VALUES  ('$username','$email','$password')";

if ($mysqli->query($insert) === TRUE) {

?>
    <script>alert('successfully registered ');</script>
    <?php
}else {
?>
<script>alert('error'); </script>

<?php
}


  header( 'register.html' );
  mysqli_close($mysqli);
?>

And then this is my login code (which doesn't work):

<?php
  session_start();
require_once("dbconnect.php");
?>
<br>
<?php
$username = $_POST['username'];
$password = md5($_POST['password']);
echo "error: ";
echo mysqli_error($mysqli);

?>
<br>
<?php
echo "username: ", $username;
$query = "SELECT password FROM users WHERE username='$username'";

$queryfinal = $mysqli->query($query, $mysqli);

$encpassword = md5($password);


?>
<br>
<?php
echo "password: ";
echo $encpassword;
?>
<br>
<?php
echo "query: ";
echo $queryfinal;
if ($queryfinal == $encpassword) {


  $_SESSION["username"] = "$username";

  header('Location: https://ruofftechnologies.com/login1/home.html');


} 
else {
session_destroy();
?>
<br>
<?php
echo "failed";
}
?>

My login code just returns this when I enter username 'hi' with password 'hi':

"Connected successfully error: username: hi password: 179085fcac495de6d84339f8e3b11a34 query: failed"

Any help is much appreciated!

-Gabriel

  • 写回答

1条回答 默认 最新

  • duanfengdian7014 2016-03-02 23:39
    关注
    $queryfinal = $mysqli->query($query, $mysqli);
    

    returns a mysqli_result object.

    $encpassword = md5($password);
    

    returns a string.

    if ($queryfinal == $encpassword) {
    

    compares a string to a non-string object, so of course it always returns false.

    Getting the data from the result is more complicated than just "converting to a string." A mysqli_result contains one or more rows of data, each containing one or more columns. To navigate this data structure you use the various mysqli_result::fetch_* functions. In your case I would use

    $resultrow = $queryfinal->fetch_assoc();
    $resultpwd = $resultrow['password'];
    

    This fetches the current row of the result into an associative array called $resultrow, then retrieves the array element named password.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题