A database query returns different results when run in PHP CLI and MySQL directly

I had a problem in the following script. When running the script below through the PHP CLI, the marked request returns a single result. If I exit the script screen and run it directly on MySQL, the query returns 14 results (as it should). I could not find the reasons why the difference occurred.

I consider only the first loop iteration at this point. The first cycle is executed for the request. What is strange is that at the time the second query is executed, the number of results is correct. Therefore, I focus only on the first iteration at this stage.

//This line allows the GROUP_CONCAT in the next query to pull all the values 
//    it should rather than being limited too far.
$select = "SET SESSION group_concat_max_len = 6000";
mysql_query($select);

$select = "SELECT 
        COUNT(*) AS Notes, n.PID, n.CreatedDate, p.ClientID, n.Body, 
        GROUP_CONCAT(n.CNID SEPARATOR ',') AS NoteIDs 
    FROM CNTbl n INNER JOIN PTbl p ON n.PID = p.PID 
    WHERE n.Body LIKE '%>No Contact Letter<%' 
    GROUP BY n.PID, n.CreatedDate, n.Body 
    HAVING Notes > 1 
    ORDER BY n.CreatedDate";

$result=mysql_query($select);

while($row = mysql_fetch_array($result)) {
    list($NoteCount, $PID, $CreatedDate, $ClientID, $Body, $NoteIDs) = $row;
    $NoteIDList = explode(',', $NoteIDs);
    $DateParts = explode(' ', $CreatedDate);

    ############## The issue is with this query. ##############
    $getNoteList = "SELECT p.PID 
        FROM MTbl m 
            INNER JOIN ATbl a ON m.AID = a.AID 
            INNER JOIN PTbl p ON a.PID = p.PID 
        WHERE DATE(m.CreatedDate) = date('$CreatedDate') 
            AND p.ClientID = $ClientID";
    $resultNoteList = mysql_query($getNoteList);

    $place = 0;
    while ($row2 = mysql_fetch_assoc($resultNoteList)) {
        $newBody = preg_replace('/'.$PID.'/', $row2['PID'], $Body);
        $getNoteID = "UPDATE CNTbl 
            SET PID={$row2['PID']}, Body='$newBody' 
            WHERE CNID={$NoteIDList[$place++]}";
        mysql_query($getNoteID);
    }
}

Table Details:

MTbl

Field       Type         Null Key   Default
MID         bigint(20)   NO   PRI   NULL
AID         bigint(20)   YES  MUL   NULL
CreatedDate timestamp    NO   ""    CURRENT_TIMESTAMP

ATbl

Field   Type         Null Key   Default
AID     bigint(20)   NO   PRI   NULL
PID     bigint(20)   YES  MUL   0

PTBL

Field      Type         Null Key   Default
PID        bigint(20)   NO   PRI   NULL
ClientID   bigint(20)   YES  MUL   0

CNTbl

Field         Type         Null  Key   Default
CNID          bigint(20)   NO    PRI   NULL
Body          text         YES   ""    NULL
CreatedDate   datetime     NO    ""    NULL
PID           bigint(20)   YES   MUL   NULL

Additional Information:

  • , PHP mysql, , mysqli - , .
  • PHP 5.1.6
  • MySQL 5.0.77

. , , - , .

+3
1

mySQL CLI, Apache.

, (). - , .

apache mysql, , . , , - "" . mysql..., mySQL, . , , . , ( , . http://dev.mysql.com/tech-resources/articles/mysql_intro.html).

sudo ( ) root (sudo man page). , mysql apache, .

+2

All Articles