微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用mysql,php和ajax创建表(使用jquery)

对于我的新项目,我想要一种不需要在每个数据库请求上重新加载页面的现代方法. :)我希望脚本查询数据库并创建一个包含查询信息的表.

我尝试过在互联网上找到的不同脚本.下面的那个最接近我的需求.

的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);

?>

但我只在表格中得到一堆{$value}的表格.我试过只有$value但是得到了一堆零.

我尝试了一个简单的脚本

$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数据库中的信息创建一个表,并在重新加载页面时将其显示在屏幕上.

有任何想法吗?

解决方法:

您应该使用$value而不是{$value}.在while循环中不需要另一个foreach循环.

$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>';

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐