duangou2028 2015-08-30 15:00
浏览 140

SQL查询(select * where)在php中不起作用

    <?php
$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_pass = '';
$mysqli_db = 'fives';

$link = mysqli_connect($mysqli_host,$mysqli_user,$mysqli_pass,$mysqli_db);

if (isset($_GET['username'])) {
    $username = mysqli_real_escape_string($link,$_GET['username']);
    print $username. "<br>";
    if (!empty($username)) {
        $username_query = mysqli_query($link,"SELECT * FROM users");
            while($row = mysqli_fetch_array($username_query)) {
                print "username: " . $row["username"]. " - password: " . $row["Password"]. " - date:" . $row["date"]. "<br>";
            }
            print $username_result = mysqli_num_rows($username_query) . "<br>";//print 1

            $username_query = mysqli_query($link,"SELECT * FROM users WHERE username = '" . $username . "'");
            while($row = mysqli_fetch_array($username_query)) {
                print "username: " . $row["username"]. " - password: " . $row["Password"]. " - date:" . $row["date"]. "<br>";
            }
            print $username_result = mysqli_num_rows($username_query);//print 0
    }
}
?>

what it prints is

yariv
username: "yariv" - password: "1234" - date:2015-08-26
1
0

well there's no problem with the first query.. it gets all of the data from [Users] which is 1 row, but when im trying to get it by using where username = '" . $username . "' the sql query gets 0 rows. why is it like that?

  • 写回答

1条回答 默认 最新

  • duanlu1793 2015-08-30 15:14
    关注

    The first query shows the result because you did not compare the given user name to the username existing in the database, but in the second query you were cheeking the username to the database username. so are you sure that you gave the correct user name same as the username in the database?

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用