douniuta4783 2013-08-04 18:37
浏览 9
已采纳

我该如何修复php函数bool? [关闭]

I dont know what is the problem with this function:

function server_grafico_expirar($data) {
    $data = sanitize($data);
    $query = mysql_query("SELECT `data_exp` FROM `settings` WHERE `data_exp` = '$data'");
    return (mysql_result($query, 0) == 1) ? true : false;
}

This function was supposed to return true if the date('d.m.Y') equals the date in mysql.

Im using like this:

$data = date('d.m.Y');
if(server_grafico_expirar($data)){
    echo "Today, is the date!";
}

The error is:

Parse error: syntax error, unexpected T_IF in /home/mcser325/public_html/checker.php on line 35
  • 写回答

2条回答 默认 最新

  • dpgjci27392 2013-08-04 18:54
    关注

    Firstly, you must make sure that the settings table does contain a row that has the data_exp column set to todays date in the format d.m.Y.

    mysql_result retrieves the contents of one cell from a MySQL result set. The cell that you are retrieving is data_exp. From your question I have assumed that data_exp is a date in the format of d.m.Y.

    With that said, mysql_result($query, 0) will never be equal to 1 as it will return the date you are selecting. You could approach this in two ways, you could either check if the cell equals $data and then return true

    function server_grafico_expirar($data) {
       $data = sanitize($data);
       $query = mysql_query("SELECT `data_exp` FROM `settings` WHERE `data_exp` = '$data'");
       return mysql_result($query, 0) == $data;
    }
    

    You could also check how many rows are returned. If more than zero rows are returned then you can return true.

    function server_grafico_expirar($data) {
       $data = sanitize($data);
       $query = mysql_query("SELECT `data_exp` FROM `settings` WHERE `data_exp` = '$data'");
       return mysql_num_rows($query) > 0;
    }
    

    Please note that the mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单