简体   繁体   English

无法从数据库phpMyAdmin检索数据

[英]can't retrieve data from database phpMyAdmin

I'm a beginner in learning how to set up database & PHP script and follow example to do that, then when I run login.php script I can't retrieve data from the database , I really feel that is a very simple question for others but I tried to solve it But didn't succeed, so can someone take look on my code then Corrects it? 我是一个学习如何设置数据库和PHP脚本并按照示例进行操作的初学者,然后当我运行login.php脚本时,我无法从数据库中检索数据,我真的觉得这是一个非常简单的问题其他人,但是我试图解决它,但是没有成功,所以有人可以看看我的代码然后改正它吗?

here is my php script : 这是我的PHP脚本:

init.php : init.php:

<?php
    $db_name = "webapp";
    $mysql_username = "root";
    $mysql_password = "";
    $server_name = "localhost";
    $con=mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);

    if (!$con)  {
        echo "Connection Error ......." . mysqli_connect_error();
    } else {
        echo "<h3>Database connection Success .....</h3>";
    }
?>

login.php : login.php:

<?php
    require "init.php";
    $user_name = "YASER";
    $user_phone = "123456";

    $sql_query = "select name from user_info where user_name like'$user_name'and
        user_phone like'$user_phone';";

    $result = mysqli_query($con,$sql_query);
    if (mysqli_num_rows($result)>0)
    {
        $row = mysqli_fetch_assoc($result);
        $name = $row["name"];
        echo "<h3> Hello And Wellcome" . $name . "</h3>";
    }  else  {
        echo " No Info Is Available .......";
    }
?>

这是结果

1st : First check that query is executing or failing 1st:首先检查查询是否正在executingfailing

if(!$result){ echo mysqli_error($con); }

2nd : use = instead of like 第二:=代替like

$sql_query = "select name from user_info 
              where user_name='$user_name' and
              user_phone='$user_phone'";

3rd : You need to give proper spacing In query 第三:您需要在查询中提供适当的间距

like'$user_name'and
   ^^^        ^^^ 

To

like '$user_name' and

You have error in your query. 您的查询中有错误。

try this to find error 试试这个发现错误

$result = mysqli_query($con,$sql_query) or die(mysqli_error($con));

Your query should be like as follow... 您的查询应如下所示...

'SELECT name FROM user_info WHERE user_name LIKE "'.$user_name.'" AND user_phone LIKE "'.$user_phone.'"';

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

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