douyi4991 2013-07-02 15:48
浏览 19
已采纳

mysql_query语法首选项和错误原因[关闭]

$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
if(!result) echo 'error';
     (or)
// assume connection to database is established in the beginning itself
if($result = mysql_query("SELECT * FROM `table`")) // do something 
else echo 'error';

I guess both perform the same operation indeed. If it is wrong correct me.The question is why does people like to prefer this syntax mysql_query($query, $connection); , is it some standard to follow or user preference? or do they differ in functionality.

and also kjndly clear me this, what could be the cause for mysql_query to return false or fail to execute? and the functions like mysql_num_rows,mysql_fetch_array do they also fail to execute at times due to server or database issues?

kindly let me know and it would be greatly appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • douxian9010 2013-07-02 15:58
    关注

    It is preferred to split them apart because it is far easier to debug. For example:

    $query = "SELECT * FROM `table`";
    $results = mysql_query($query, $connection);
    if(!result) echo "error with query $query"; // You can print the query for easy debugging
    

    or allow for simple debugging:

    $query = "SELECT * FROM `table`";
    if($debug) echo $query;
    

    The mysql_fetch_* will not fail if you already checked your result (like in the above code). If there was an error executing the query then all the code down the line will fail. We see far too much code here that never checks error codes and handle the errors properly before trying to get results.

    Hope this helps. As noted in the comments. It would be much preferred to NOT use the mysql_* functions as they are deprecated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题