dongsi1944 2016-03-02 20:14
浏览 87
已采纳

在PHP PostgreSQL查询结果中找到或未找到结果时显示消息

I am trying to write a conditional statement which echo's the following messages :

if  {the query executed bring results from the database 
Process the query
echo 'Success' }

else {
If no results/ 0 results have been retrieved then
echo 'No results obtained'}

I am using PostgreSQL as my database. I am new to using PostgreSQL with PHP but so far I have managed to make my queries work. I am just confused on how I can do this and which part of the code this logic should be in. Is it before the query has been executed.

      <?php
// Connecting, selecting database
 $dbconn = pg_connect("host=localhost port=5432 dbname=sser user=postgres     password=password")
or die('Could not connect: ' . pg_last_error());


$name = pg_escape_string($_POST['name']);
$name2 = pg_escape_string($_POST['name2']);


$query = " SELECT y.name, y.time, z.name, z.time
FROM 
(SELECT * FROM departure_times  WHERE name ='$name') as y,
(SELECT * FROM departure_times  WHERE name ='$name2') as z
WHERE y.tram_id = z.tram_id ";

$result = pg_query($query) or die('Query failed: ' . pg_last_error());

// Printing results in HTML
echo "<table>
";
echo "These are the following trams from '$name' to '$name2' ";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>
";
foreach ($line as $col_value) {
    echo "\t\t<td>$col_value</td>
";
  }
  echo "\t</tr>
";
}
echo "</table>
";

// Free resultset
pg_free_result($result);


// Closing connection
pg_close($dbconn);
?>
  • 写回答

1条回答 默认 最新

  • douba6361 2016-03-02 21:53
    关注

    You have three situations to deal with:

    1. The query fails due to some error condition.
    2. The query succeeds and returns one or more rows of data.
    3. The query runs successfully, but returns no data because none match the query conditions.

    In the first instance, pg_query() returns the boolean value false into the variable $result; your or die() clause handles this. In the second and third cases, $result contains a result set resource which may or may not contain data, and you want to know whether it does or not.

    The PHP API for PostgreSQL contains a handy function for just that purpose called pg_num_rows(). This function takes a result set as its argument and returns an int showing the number of rows in the result. So your code would look something like this:

    $result = pg_query($query)
        or die('Query failed: ' . pg_last_error());
    if (pg_num_rows($result) > 0)
    {
        echo "I found some data.";
    }
    else
    {
        echo "I got nothin'.";
    }
    

    Replace the echo statements with your own logic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加