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

时间:2012-11-08 07:01:18

标签: php jquery mysql ajax

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

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

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

有什么想法吗?

4 个答案:

答案 0 :(得分:2)

您应该使用$ 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>';

答案 1 :(得分:0)

而不是

$output_string .= '<td>{$value}</td>';

$output_string .= "<td>{$value}</td>";

即。用双引号替换单引号。

请参阅文档here,其中包含:

  

双引号中指定字符串时...会在其中解析变量。

  

变量...当单引号字符串出现 时, 将被展开。

答案 2 :(得分:0)

您可以使用自动执行此操作的库(例如http://phery-php-ajax.net)反转逻辑,并在客户端执行此操作 而不是在服务器端创建表,将其作为JSON发送到浏览器,这更快,并构建您的表:

Phery::instance()->set(array(
'load' => function(){
  /* rest of mysql code */
  $rows = array();
  while($row = mysql_fetch_assoc($facebook)) { $rows[] = $row; }
  return PheryResponse::factory()->json($rows);
})->process();

然后在$(function(){});

内的客户端
$(function(){
  var $result_table = $('#result_table'); 
  $result_table.phery('make', 'load');
  $result_table.bind('phery:json', function(e, data){
    var $this = $(this);
    for (var i = 0; i < data.length; i++) {
      $this.append($('<tr/>', {
        'html': '<td>' + data[i].row_name + '</td>'
      }));
    }
  });
  $result_table.phery('remote');
});

答案 3 :(得分:0)

100% 工作 只需删除结束标签。例如,当打开标签并将其存储在 $output_string 中时,切勿包含结束部分...

然后不要在引号中包含 $value... 把它放在引号外面,然后用点分号分隔 $value 和右引号。