duanan6043 2016-02-26 12:32
浏览 57
已采纳

用mysqli编写PHP程序代码

I am less familiar with mysqli, and I write Procedure style, not Object orientated style. I know of some of the benefits of object orientated style but it trips my mind so I am slow to adjust with it. My policy is, if I cannot support it, I don't write it.

So... I have a simple function which contains the following:

mysqli_query( $DB, "CREATE TABLE $TABLE ( $SCHEMHA )" );

if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}

The query works when creating the table, so long as I don't have a bug in my schema. If my schema fails, my conditional statement does not trap the failure (thus the connect failed message never gets called).

Where am I going wrong?

My goal is to be able to exit/stop/end on selective errors. So for example, if a table already exists (errno 1050), I am comfortable ignoring that error, however if there is an error 1072 (Key column 'whatever' doesn't exist in table) I would like to stop processing.

All help appreciated...

  • 写回答

4条回答 默认 最新

  • douguan8940 2016-02-26 12:52
    关注
    1. You'll want to examine the return value of mysqli_query to know whether the query succeeded or failed.
    2. You can get the exact error code using mysqli_errno.
    3. The _connect_ functions you're using are irrelevant in this context, they give you information about errors which happened during a connection attempt.
    if (!mysqli_query(..)) {
        switch (mysqli_errno($DB)) {
            case 1072:
                ...
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b