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 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架