dongzhanlian6289 2014-02-27 21:13
浏览 637
已采纳

在SQL查询中进行简单的数学运算

I have variable $page in my PHP page and in a query I want to use this like:

$query = mysqli_query($con, "SELECT * FROM third LIMIT (3*($page-1)),3");
//right here I want to this math but it gives me error.

I tried it in that way too

$query = mysqli_query($con, "SELECT * FROM third LIMIT (3*{$page-1}),3");

It gave me that message:

syntax error, unexpected '-', expecting '}'

How should do this math in SQL?

  • 写回答

2条回答 默认 最新

  • douyong6585 2014-02-27 21:18
    关注

    Please note that your problem is not one of SQL, but of how PHP interpolates strings.

    Your problem is that PHP will interpolate variables into strings, but not expressions.

    You can do this as suggested above:

    $query = mysqli_query($con, "SELECT * FROM third LIMIT " . (3*($page-1)) . ",3");
    

    Or you could do this:

    $limit = 3*($page-1);
    $query = mysqli_query($con, "SELECT * FROM third LIMIT $limit,3");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题