douji6940 2012-07-13 10:06
浏览 153
已采纳

mysql_query有效但错误检查会返回错误

This code works and enters all of the correct information into the database but the error check returns an error. I could remove the error check but I'm afraid of creating some nightmare that comes back to haunt me later or that I'm missing a fundamental issue:

    $sql5a = mysql_query("SELECT id FROM categories WHERE category='$category'");
        $categoryresult = mysql_fetch_array($sql5a);
        $oldcategoryid = $categoryresult['id'];
        $sql6a = "INSERT INTO lookupcategory SET
        fbid='$fbid2',
        categoryid='$oldcategoryid'";
            if ( @mysql_query($sql5b) ) { 
                                echo('sql5b updated successfully<br>'); 
                            } else { 
                                echo('Error: sql5b not updated<br>'.mysql_error() );
                        }
                            if ( @mysql_query($sql6b) ) { 
                                echo('sql6b updated successfully<br>'); 
                            } else { 
                                echo('Error: sql6b not updated<br>'.mysql_error() );
                        }

The output is: "Error: sql5b not updated You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #7' at line 1" "sql6b updated successfully"

When I check the database all entries are correct. If sql5a didn't work, sql6b couldn't work, hence my confusion over the error.

A sample Category would be: Travel/Leisure The category was originally created from a form response: $category = htmlentities($fbdetail['category'], ENT_QUOTES); and entered into the database successfully. An id number was assigned using AUTO_INCREMENT.

  • 写回答

2条回答 默认 最新

  • doulu7921 2012-07-13 10:32
    关注

    You assign query to variable $sql5a, but call @mysql_query($sql5b). $sql5b doesn't exist (at least in this sample). Same with $sql6a...

    You can use INSERT syntax without VALUES, but you need to ommit INTO keyword.

    $sql5a = mysql_query("SELECT id FROM categories WHERE category='$category'");
    if ( $res5a = @mysql_query($sql5a) ) { // first execute query and store resource in variable
        echo('sql5a selected successfully<br>'); 
    } else { 
        echo('Error: sql5a failed<br>'.mysql_error() );
    }
    $categoryresult = mysql_fetch_array($res5a); // fetch array passing the RESOURCE var, NOT query string
    $oldcategoryid = $categoryresult['id'];
    $sql6a = "INSERT lookupcategory SET
    fbid='$fbid2',
    categoryid='$oldcategoryid'";
    
    if ( @mysql_query($sql6a) ) { 
        echo('sql6a inserted successfully<br>'); 
    } else { 
        echo('Error: sql6a failed<br>'.mysql_error() );
    }
    

    I don't know where you get $fbid2 or $category from, since it's not in this piece of code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?