douji6940 2012-07-13 02: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 02: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 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交