douzhun4124 2014-03-28 14:59
浏览 16
已采纳

从数据库打印的结果并不是人们所期望的

 require 'db/connect.php';
if ($result = $db->query("SELECT last_name +' '+ first_name as full_name, bio FROM people")) {
    if ($count = $result->num_rows) {
        echo '<p>', $count , '</p>';

        // while ($rows = $result->fetch_object()) {
            // print_r($rows);
            // echo '<br/>';
        // }

        while ($rows = $result->fetch_object()) {
            echo $rows->full_name, ' ' , $rows->bio, '<br />';
        }

        $result->free();
    }
} else {
    die($db->error);
}

The database table people contains two rows of data where the columns are id, first_name, last_name, bio and created.

row 1: id=1, first_name=kelly, last_name='wafukho', bio=computer scientist.
row 2: id=2, first_name=gonzaga, last_name='situma', bio=graphic designer.

When the query below is run it gives me 0 computer scientist for row 1 and 0 graphic designer for row 2. What could be the reason for this? is the problem with my sql syntax or the php handles objects? Am using the mysql database.

  • 写回答

2条回答 默认 最新

  • douxing8939 2014-03-28 15:01
    关注

    You should use CONCAT function to join fields

    SELECT CONCAT(last_name, ' ', first_name) as full_name, bio FROM people
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM