2017-06-22 18:42
浏览 121


I'm using php to build a simple front end web application to my db2 database. This php query connects to the database and pulls the data from my selected table. I'm trying to insert my data into an html table to display it better (the rows are showing up in array formatting right now due to the "db2_fetch_array" function. How do I put my data into an html table? My php code is below, what should I add? Most questions I could find only dealt with mySQL and didn't have the same specification as I do.

<head><title>DB Testing</title></head>

//db2 express c (v10.5)
$database = "database";
$user = "db";
$password = "password";

$conn = db2_connect($database, $user, $password);

if($conn) {
echo "DB2 Connection succeeded.<br/>";

$sql = "select 'JUNK', apple, banana, orange, cake, grapes, egg from 

//db2_execute executes a sql statement that was prepared by db2_prepare
    $result = db2_execute($stmt);
        echo "exec errormsg: " .db2_stmt_errormsg($stmt);
    echo '<table>';
while($row = db2_fetch_array($stmt)) {
    echo '<tr>';
    echo '<td>' . $row['apple'] . '</td>';
    echo '<td>' . $row['banana'] . '</td>';
    echo '<td>' . $row['orange'] . '</td>';
    echo '<td>' . $row['cake'] . '</td>';
    echo '<td>' . $row['grapes'] . '</td>';
    echo '<td>' . $row['egg'] . '</td>';
    echo '</tr>';   
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);

function print_r2($val){
        echo '<table>';
    echo '</table>';



图片转代码服务由CSDN问答提供 功能建议

我正在使用php为我的db2数据库构建一个简单的前端Web应用程序。 此php查询连接到数据库并从我选择的表中提取数据。 我正在尝试将我的数据插入到html表中以更好地显示它(由于“db2_fetch_array”函数,行现在以数组格式显示。如何将我的数据放入html表?我的php代码是 下面,我应该添加什么?我能找到的大多数问题只涉及mySQL,并且没有和我一样的规范。

 &lt; html&gt; 
&lt; head&gt  ;&lt; title&gt;数据库测试&lt; / title&gt;&lt; / head&gt; 
&lt; body&gt; 
 // db2 express c(v10.5)
 $ database =“database”; \  n $ user =“db”; 
 $ password =“password”; 
 $ conn = db2_connect($ database,$ user,$ password); 
if($ conn){
echo“DB2 Connection 成功。&lt; br /&gt;“; 
 $ sql =”select'JUNK',apple, 来自
 // db2_execute执行由db2_prepare 
if($ stmt){
 $ result = db2_execute($ stmt); 
 n生成的sql语句 if(!$ result){
 echo“exec errormsg:”。db2_stmt_e  rrormsg($ stmt); 
 echo'&lt; table&gt;'; 
while($ row = db2_fetch_array($ stmt)){
 echo'&lt; tr&gt;'; 
 echo'&lt; td&gt;  '。  $ row ['apple']。  '&lt; / td&gt;'; 
 echo'&lt; td&gt;'  。  $ row ['banana']。  '&lt; / td&gt;'; 
 echo'&lt; td&gt;'  。  $ row ['orange']。  '&lt; / td&gt;'; 
 echo'&lt; td&gt;'  。  $ row ['cake']。  '&lt; / td&gt;'; 
 echo'&lt; td&gt;'  。  $ row ['grape']。  '&lt; / td&gt;'; 
 echo'&lt; td&gt;'  。  $ row ['egg']。  '&lt; / td&gt;'; 
 echo'&lt; / tr&gt;';  
echo'&lt; / table&gt;'; 
} else {
echo“exec errormsg:”。db2_stmt_errormsg($ stmt); 
db2_close($ conn); 
?n?&gt; \  n&lt;?php 
function print_r2($ val){
 echo'&lt; table&gt;'; 
 print_r($ val); 
 echo'&lt; / table&gt;'; 
?&gt;  ;  
&lt; / body&gt; 
&lt; / html&gt; 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshao8471 2017-06-22 20:08

    I assume the syntax error here:

    $sql = "select rows from table ;

    is the result of you changing your query to ask this question. In any event, fix it, the query string is not closed. Also, I guess you don't have a field named "rows" in your table, you need to list the columns you want.

    You already have the right loop, just change the <pre> tags with a table structure:

    echo '<table>';
    while($row = db2_fetch_array($stmt)) {
        echo '<tr>';
        echo '<td>' . $row['columnName'] . '</td>';
        // repeat with your other columns
        echo '</tr>';
    echo '</table>';
    解决 无用
    打赏 举报

相关推荐 更多相似问题