我使用这段 php 来插入/更新 mysql db。请在相应行的评论部分查看我的问题。谢谢。
        //Connecting to your database
        mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later.");
        mysql_select_db($dbname);
        arr = array();
        if (strcasecmp($actionIn, 'insert') == 0) {
           $query = "INSERT INTO $usertable (id, fname, lname) VALUES ('$id', '$fname', 'lname'";
           $result = mysql_query($query) or die(mysql_error()); //AT THIS STEP, I would get error message if I insert a duplicated id into table, no following json_encode would not print out, that's what I want.
           if ($result) {
              $arr['inserted'] = 'true';
           }
           exit(json_encode($arr));
        }
        if (strcasecmp($actionIn, 'update') == 0) {
           $query = "UPDATE $usertable SET id = '$id', fname = '$fname', lname = '$lname' WHERE id = '$id'";
           $result = mysql_query($query) or die(mysql_error()); //AT THIS STEP, if I update a non-existent id, I don't get error, and the following steps continue to execute. I want the error info or return me a false.
           if ($result) {
              $arr['updated'] = 'true';
           }
       exit(json_encode($arr));
        }
我也试过这些,但是 num_rows 和affected_rows 都返回0。为什么?
       $row_cnt = $result->num_rows;
           printf("Result set has %d rows.\n", $row_cnt);
       $aff_cnt = $result->affected_rows;
       printf("Result set aff %d rows.\n", $aff_cnt);
谢谢你的帮助!