创建具有MySQL,PHP和AJAX(使用jQuery)的表

问题描述:

有关我的新项目,我想不需要重新加载的每个数据库请求一个页面让现代的方法。 :)我希望脚本来查询数据库,并创建一个表,查询信息。

For my new project i want the so modern approach of not needing to reload a page on every database request. :) I want the script to query the database and create a table with the query information.

我已经尝试了不同的脚本,我发现在互联网上。下一个是最接近我的需要。

I have tried different scripts i have found on the internet. The one below was closest to my needs.

的index.php

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Display Page</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
</head>

<body>
<button type='button' name='getdata' id='getdata'>Get Data.</button>

<div id='result_table'>

</div>

<script type='text/javascript' language='javascript'>
$('#getdata').click(function(){

$.ajax({
        url: 'getdata.php',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#result_table').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call    

});
</script>
</body>
</html>

getdata.php

<?php
include('conn.inc.php');
//Query of facebook database
$facebook = mysql_query('SELECT * FROM users')
or die(mysql_error());

//Output results
if(!$facebook)
{
    mysql_close();
    echo json_encode('There was an error running the query: ' . mysql_error());
}
elseif(!mysql_num_rows($facebook))
{
    mysql_close();
    echo json_encode('No results returned');
}
else
{
    $output_string = '';
    $output_string .=  '<table border="1">';
    while($row = mysql_fetch_assoc($facebook))
    {
        $output_string .= '<tr>';
        foreach($row as $value)
        {
            $output_string .= '<td>{$value}</td>';
        }
        $output_string .= '</tr>';
    }
    $output_string .= '</table>';
}

mysql_close();
// This echo for jquery 
echo json_encode($output_string);

?>

但我只拿到一张桌子和一帮{$值}在表内。我试过,只有$价值,而且有一堆零。

But i only get a table with a bunch of {$value} inside the table. I've tried with only $value but got a bunch of zeros.

我想一个简单的脚本

$query = "SELECT users_name, users_password FROM users WHERE users_name = 'name'";
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result);

echo $row['users_name'];

然后我得到SOM的结果,但这个剧本我必须刷新每个搜索页面。需要明确的是我希望能够创建一个表从MySQL数据库中的信息,并与重新加载页面显示在屏幕上。

And then i get som results but with this script i have to refresh the page on every search. To be clear i want to be able to create a table with the information from a mysql database and display it on screen with reloading the page.

任何想法?

您应该只使用$值,而不是{$值}。你不需要while循环内的另一个foreach循环。

You should just use $value instead of {$value}. You don't need another foreach loop inside the while loop.

$output_string = '';
$output_string .=  '<table border="1">';
while($row = mysql_fetch_assoc($facebook))
{
    $output_string .= '<tr>';
    $output_string .= '<td>'.$row['Your table column name here'].'</td>';
    $output_string .= '</tr>';
}
$output_string .= '</table>';