Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run "show slave status" with PHP PDO

I am trying to get the slave status of my MySQL server using PHP's PDO implementation. Running fetchAll() returns an empty array.

// DB IP, name, username, and password are fake here. I can connect.
$db = new PDO('mysql:host=192.168.0.0;dbname=production', 'username', 'password');
$result = $db->query("SHOW SLAVE STATUS");
$result->execute();

if ($result != false)
{
    $slave = $result->fetchAll(PDO::FETCH_ASSOC);
}

I have also tried removing the ->execute() call, but it's the same result. Is there something completely obvious that I'm missing here? I've looked up and down the PDO::query documentation and it's not helping much.

like image 287
Andrew Ellis Avatar asked Jul 16 '12 18:07

Andrew Ellis


1 Answers

Do you have permission to execute the query?

Try:

$db = new PDO('mysql:host=192.168.0.0;dbname=production', 'username', 'password');

$result = $db->query("SHOW STATUS");

if (!$result) {
    echo $db->errorInfo()[2]; // php 5.4
} else {
    foreach($result->fetchAll() as $row) {
        var_dump($row);
    }
}
like image 106
drew010 Avatar answered Oct 08 '22 04:10

drew010