无法从数据库phpMyAdmin中检索数据

时间:2017-08-09 09:22:17

标签: php phpmyadmin

我是学习如何设置数据库的初学者。 PHP脚本并按照示例 要做到这一点,那么当我运行login.php脚本时,我无法从数据库中检索数据, 我真的觉得这对其他人来说是一个非常简单的问题,但我试图解决它但是没有成功,那么有人可以看一下我的代码然后纠正它吗?

这是我的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:

<?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 .......";
    }
?>

this is the result

2 个答案:

答案 0 :(得分:0)

第一次:首先检查查询是executing还是failing

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

第二名:使用=代替like

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

第3名:您需要在查询中正确的间距

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

like '$user_name' and

答案 1 :(得分:0)

您的查询中有错误。

尝试此以查找错误

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

您的查询应如下所示......

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