简体   繁体   中英

Database query is returning different results when run in PHP CLI and MySQL directly

I am having an issue in the following script. When running the script below via PHP CLI, the marked query returns a single result. If I echo the script to the screen, and run it directly on MySQL, the query returns 14 results (as it should). I have been unable to find any reason why the difference would occur.

I am looking at only the first iteration of the loop at this point. There are approximately 200 results for the query running the first loop. What makes it odd is that some of the times the second query runs, the number of results is correct. So I am focusing only on the first iteration at this point.

//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:

  1. I am stuck using the PHP mysql functions rather than switching to mysqli due to this being a collaboration among several people on an old project.
  2. I am using PHP 5.1.6
  3. I am using MySQL 5.0.77

I have tried to include all necessary information. Please let me know if there is anything I am missing that would be of help.

You are getting a different instance of mySQL with CLI than you are when you run the query via Apache.

When you connect to the server in a shell, you are connected as a non-privileged user (presumably). When you execute the any commands, you do so with the privileges of your user account.

The instances of apache and mysql that run on your server and respond to external requests are running under a privileged user. So, when you connect to your shell, these instances are not able to be directly interacted with - they don't "belong" to your user. When you execute the mysql... command, the OS starts a new instance of mySQL server running with the privileges of your user. This is an entirely new instance, separate from the one that answers external requests. This new instance will use the default configuration files unless you've specifically set up your server to use a different one on a per-user basis (configurations vary, see http://dev.mysql.com/tech-resources/articles/mysql_intro.html ).

The sudo command allows a user (with permission to do so) to execute commands as the privileged root account instead of the current user ( sudo man page ). When you execute commands at this level, you are able to interact with the persistent instances of mysql and apache that are answering external requests.

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