免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 929 | 回复: 2
打印 上一主题 下一主题

关于查询mysql 后的显示问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-26 16:17 |只看该作者 |倒序浏览
这是抄来的一段代码,编译通过,有个地方看不懂,所以请教大家,源码舅下
[root@localhost c]# cat test.c
  1. #include <sys/time.h>;
  2. #include <stdio.h>;
  3. #include <mysql.h>;

  4. int main (char **args)
  5. {
  6.         MYSQL_RES *result;
  7.         MYSQL_ROW row;
  8.         MYSQL *connection, mysql;
  9.         int state;

  10.         /* connect to database */
  11.         mysql_init(&mysql);
  12.         connection = mysql_real_connect(&mysql,"192.168.3.113","root","654321","bm",0,0,0);

  13.         /* check for connection error */
  14.         if (connection == NULL)
  15.         {
  16.                 /* print error message */
  17.                 printf(mysql_error(&mysql));
  18.                 return 1;
  19.         }
  20.         state = mysql_query(connection, "SELECT sn, id FROM account");
  21.         if (state != 0)
  22.         {
  23.                 printf(mysql_error(connection));
  24.                 return 1;
  25.         }

  26.         /* must call mysql_store_result() before we can issue any other
  27.          * * query calls
  28.          * */
  29.         result = mysql_store_result(connection);
  30.         printf("Rows: %dn", mysql_num_rows(result));

  31.         /* process each row in result set */
  32.         while ((row = mysql_fetch_row(result)) != NULL)
  33.         {
  34.                 printf("id: %s, val: %sn",
  35.                                 (row[1] ? row[1] : "NULL"),
  36.                                 (row[0] ? row[0] : "NULL"));
  37.         }

  38.         /* free the result set */
  39.         mysql_free_result(result);

  40.         /* close the connection */
  41.         mysql_close(connection);
  42.         printf("Done.n");
  43. }
复制代码


下面这一段我就看不懂了,(row[1] ? row[1] : "NULL",是怎么得也来的,还有就是我如果需要把这些值拿出来做计算,那是不是要重新定义变量呢,谢谢

while ((row = mysql_fetch_row(result)) != NULL)
        {
                printf("id: %s, val: %sn",
                                (row[1] ? row[1] : "NULL",
                                (row[0] ? row[0] : "NULL");

论坛徽章:
0
2 [报告]
发表于 2004-03-26 16:35 |只看该作者

关于查询mysql 后的显示问题

其实row是一个指针数组,指向一条记录值,row就是第i个字段的值的指针。
    row[1] ? row[1] : "NULL"是一个条件表达式,如果row[1]的值是真的话表达式取row[1]的值,否则取“NULL”值。

    可以用row来进行运算,但是如果是数值运算,记得用atoi(row)或者 atol(row)之类的函数来进行类型转换。

论坛徽章:
0
3 [报告]
发表于 2004-03-26 16:50 |只看该作者

关于查询mysql 后的显示问题

谢谢,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP