从MySQL数据库获取数据而不刷新

时间:2015-04-21 00:29:44

标签: php jquery mysql

我一直在尝试使用jQuery和php从数据库中获取用户名,但到目前为止我还没有运气。它设法将文本字段中的值发布到name.php文件中,但我无法回显出与数据库中用户名链接的名称。

HTML页面:

<!DOCTYPE html> 
<html>
    <head>
    <meta charset="utf-8">
    <title>Retail Management Application</title>
    </head> 
    <body>             
        Name: <input type="text" id="username">
         <input type="submit" id="username-submit" value="Grab">
            <div id="username-data"></div>
        <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script src="js/global.js"></script>
    </body>
</html>

global.js文件:

 $('input#username-submit').on('click', function() {
    var username = $('input#username').val();

    if ($.trim(username) != '') {
        $.post('ajax/name.php', {username: username}, function(data){  
            $('div#username-data').text(data);
        });
    };
});

name.php:

<?php

    if (isset($_POST['username']) === true && empty($_POST['username']) === false) {

    require '../db/connect.php';

    $query = mysqli_query("
            SELECT `username`.`name` 
            FROM `users` 
            WHERE `users` . `username` ='". mysqli_real_escape_string(trim($_POST['username'])). "'
            ");
    /*  $query = DB::getInstance()->query("SELECT `username`.`name` FROM users 
                                            WHERE `users` . `username` 
                                            = '". mysqli_real_escape_string(trim($_POST['username']))."'"); */
    echo (mysqli_num_rows($query) !== 0) ?  mysql_result($query, 0, 'name') : 'Name not found!';
    //tenary operator.
    }
?>

connect.php:

<?php
    $con = mysqli_connect("localhost","root","root") 
                    or die("Error " . mysqli_error($con));     
    mysqli_select_db("retail_management_db");           
?> 

1 个答案:

答案 0 :(得分:0)

您必须使用mysqli_fetch_array获取结果,然后回显它。

将它放在你的mysql查询

if($query)
{
while($query_result = mysqli_fetch_array($query))
{
//This returns an array of the fetched values
$name = $query_result['name'];
}
echo $name;
}
else
{
echo "Query Failed";
}
然后

Name.php成为

<?php

if (isset($_POST['username']) === true && empty($_POST['username']) === false) {

require '../db/connect.php';




                   $query = mysqli_query("
                    SELECT `username`.`name` 
                    FROM `users` 
                    WHERE `users` . `username` ='". mysqli_real_escape_string(trim($_POST['username'])). "'
                    ");

if($query)
    {
    while($query_result = mysqli_fetch_array($query))
    {
    //This returns an array of the fetched values
    $name = $query_result['name'];
    echo $name;
    }
    }
    else
    {
    echo "Query Failed";
    }


/*  $query = DB::getInstance()->query("SELECT `username`.`name` FROM users 
                                        WHERE `users` . `username` 
                                        = '". mysqli_real_escape_string(trim($_POST['username']))."'"); */




    //You should not use mysqli with mysql
    echo (mysqli_num_rows($query) !== 0) ?  mysql_result($query, 0, 'name') : 'Name not found!';


    //tenary operator.
}
?>

编辑区域 不要将Mysql函数与Mysqli一起使用

你的name.php现在应该是

    <?php

    if (isset($_POST['username']) === true && empty($_POST['username']) === false) {

    require '../db/connect.php';




                       $query = mysqli_query("
                        SELECT `username`.`name` 
                        FROM `users` 
                        WHERE `users` . `username` ='". mysqli_real_escape_string(trim($_POST['username'])). "'
                        ");

    if($query)
        {
        //We check if the returned rows are at least one
        if(mysqli_num_rows($query) > 0)
        {
        while($query_result = mysqli_fetch_array($query))
        {
        //This returns an array of the fetched values
        $name = $query_result['name'];
        echo $name;
        }
        }
        else
        {
        echo mysqli_real_escape_string(trim($_POST['username'])) . "Name not Found";
        }
        }
        else
        {
        echo "Query Failed";
        }


    /*  $query = DB::getInstance()->query("SELECT `username`.`name` FROM users 
                                            WHERE `users` . `username` 
                                            = '". mysqli_real_escape_string(trim($_POST['username']))."'"); */




        //You should not use **mysqli** with **mysql**
        /*echo (**mysqli_num_rows**($query) !== 0) ?  **mysql_result**($query, 0, 'name') : 'Name not found!';*/


        //tenary operator.
    }
    ?>

修改

暂时使用此作为您的html文件检查name.php是否实际上回显任何内容如果它打印任何内容然后问题来自您的js 确保表单上的操作链接到正确的name.php文件

<!DOCTYPE html> 
<html>
    <head>
    <meta charset="utf-8">
    <title>Retail Management Application</title>
    </head> 
    <body>
      <form action = 'name.php' method = 'post'>             
        Name: <input type="text" id="username">
         <input type="submit" id="username-submit" value="Grab">
        </form>
            <div id="username-data"></div>

        <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script src="js/global.js"></script>
    </body>
</html>