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

如何在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>
  • 写回答

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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题