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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab