简体   繁体   中英

PHP Mysql LIMIT failing in query

I am trying to get messages with a joined query with 3 tables everything works fine as expected but when i am trying to limit the results the query is failing and not returning anything

query

    SELECT 
        P.patient_name, 
        DM.message, 
        DM.secsince, 
        DM.id, 
        DM.pid, 
        DM.did, 
        DM.mode, 
        DM.date, 
        DM.seen 
    FROM tbl_patient P, 
        tbl_doct D, 
        tbl_doct_messages DM 
    WHERE (DM.did = D.id AND DM.pid = P.id) 
        AND (DM.mode = 'in') 
        AND (D.id = '$id') 
    ORDER BY DM.secsince DESC;

This query works fine and gives me expected results

but when I use LIMIT it is failing and not returning anything for some reason

query

SELECT 
    P.patient_name, 
    DM.message, 
    DM.secsince, 
    DM.id, 
    DM.pid, 
    DM.did, 
    DM.mode, 
    DM.date, 
    DM.seen 
FROM tbl_patient P, 
    tbl_doct D, 
    tbl_doct_messages DM 
WHERE (DM.did = D.id AND DM.pid = P.id) 
    AND (DM.mode = 'in') 
    AND (D.id = '$id') 
ORDER BY 
   DM.secsince DESC; 
LIMIT=1;

here is the whole php code

<?php
include("conn.php");
if(mysqli_connect_error($con)) {
    echo "Failed To Connect";
}

$id = $_GET['id'];
$qry = "SELECT P.patient_name, DM.message, DM.secsince, DM.id, DM.pid, DM.did, DM.mode, DM.date, DM.seen 
        FROM tbl_patient P, tbl_doct D, tbl_doct_messages DM 
        WHERE (DM.did = D.id AND DM.pid = P.id) AND (DM.mode = 'in') AND (D.id = '$id') 
        ORDER BY DM.secsince DESC 
        LIMIT=1;";
$res = mysqli_query($con, $qry);
$flag = array();
while(($row = mysqli_fetch_array($res))){
    array_push($flag, $row);
}
echo json_encode($flag); 
mysqli_close($con);     
?>

As you can see in this picture it gives me results without the limit 在此输入图像描述

But no results so mysqli_fetch_array is failing when i add the limit

在此输入图像描述

Why this happening ? i basically am out of ideas.

因为正确的语法是LIMIT 1而不是LIMIT = 1

Update your query

from

SELECT 
    P.patient_name, 
    DM.message, 
    DM.secsince, 
    DM.id, DM.pid, 
    DM.did, DM.mode, 
    DM.date, DM.seen 
FROM 
    tbl_patient P, 
    tbl_doct D, 
    tbl_doct_messages DM 
WHERE 
    (DM.did = D.id AND DM.pid = P.id) AND 
    (DM.mode = 'in') AND (D.id = '$id') 
ORDER BY 
   DM.secsince DESC 
LIMIT=1

to remove the =

SELECT 
    P.patient_name, 
    DM.message, 
    DM.secsince, 
    DM.id, DM.pid, 
    DM.did, DM.mode, 
    DM.date, DM.seen 
FROM 
    tbl_patient P, 
    tbl_doct D, 
    tbl_doct_messages DM 
WHERE 
    (DM.did = D.id AND DM.pid = P.id) AND 
    (DM.mode = 'in') AND (D.id = '$id') 
ORDER BY 
   DM.secsince DESC 
LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM