dongnuo2879 2013-04-05 19:25
浏览 37
已采纳

如何在执行MYSQL查询之前检查条件? [关闭]

I am trying to check to see if a condition exists before executing a mysql update. This code below WORKS:

if (!mysql_query(
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
))

{
die('Sorry, a database error occurred');
}

...but I'm trying to add another condition to it like this:

if (!mysql_query && ($tradein_status == 'Accepted CREDIT')(
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
))

{
die('Sorry, a database error occurred');
}

..which DOESN'T work - it generates this error: "Parse error: syntax error, unexpected '(' in /home/bohemeth/public_html/tradeins/save_tradeins.php on line 235"

The second thing I am wondering is why is this part:

if (!mysql_query(
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
))

...not written with two parens and curly braces after the condition, which it seems you would do anywhere else for a conditional test, like so:

if (!mysql_query){
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
}

THANK you for any help!!

  • 写回答

4条回答 默认 最新

  • dtlc84438 2013-04-05 19:28
    关注

    mysql_query is a function. It's used like this: mysql_query('SQL QUERY');

    It was written like in your question as an attempt to make it more readable:

    if(!mysql_query(
    'SQL QUERY'
    ))
    

    It could also be written this way:

    if(!mysql_query('SQL QUERY'))
    

    So, to add another clause, add it before or after the mysql_query call.

    if($tradein_status == 'Accepted CREDIT' && mysql_query('SQL QUERY'))
    

    In an if statement the {} can be omitted. In that case, the very next line is the one ran if the case is true.

    So, to finalize:

    if($tradein_status == 'Accepted CREDIT' && mysql_query('SQL QUERY')){
        die('Sorry, a database error occurred');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!