doulei3488
doulei3488
2017-06-22 18:42
浏览 118
已采纳

如何在php中使用SQL查询显示来自DB2数据库的数据

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.

<html>
<head><title>DB Testing</title></head>
<body>

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

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

if($conn) {
echo "DB2 Connection succeeded.<br/>";
}
    else{
    exit("failed".db2_conn_errormsg());
    }


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

//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
    $result = db2_execute($stmt);
    if(!$result){
        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);
}
db2_close($conn);

?>
<?php
function print_r2($val){
        echo '<table>';
    print_r($val);
    echo '</table>';
    }

    ?> 

</body>
</html>

图片转代码服务由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; 
 
&lt;?php 
 // 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;“; 
} 
其他{
 exit(”failed“.db2_conn_errormsg()); 
} 
 
 
 $ sql =”select'JUNK',apple, 来自
kitchen的香蕉,橙子,蛋糕,葡萄,鸡蛋“; 
 
 // 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
    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>';
    
    点赞 评论

相关推荐