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.

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

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)