Search code examples
phpmysqlmamp

MAMP - All MySQL Queries Return Null


I recently installed MAMP. But my I can't successfully execute MySQL queries from php. I can connect to MySQL from php, but all my queries come back as NULL. I don't get any error message or other message that would help me diagnose the problem.

Oddly, I can successfully query MySQL with phpMyAdmin and MySQLWorkbench.

Here is my code:

<?php
$link     = mysqli_init();
$con      = mysqli_real_connect($link, 'localhost', 'root', 'root', 'mrmk', 8889);
$db       = 'mrmk';

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

$sql    = "SHOW TABLES FROM `$db`";
$result = mysqli_query($con, $sql);
echo "</br>" . "1. MySQL Error: " . mysqli_error();
echo "</br>" . "result: ";
var_dump($result);

$sql    = "SHOW TABLES FROM $db";
$result = mysqli_query($con, $sql);
echo "</br></br>2. MySQL Error: " . mysqli_error();
echo "</br>" . "result: ";
var_dump($result);

$sql    = "SELECT * FROM `transactionTable` WHERE `attorneyName` LIKE \'%howard%\'";
$result = mysqli_query($con, $sql);
echo "</br></br>3. MySQL Error: " . mysqli_error();
echo "</br>" . "result: ";
var_dump($result);

if (!$result) {
    echo "</br></br>" . "4. MySQL Error: " . mysqli_error();
    exit;
} else {
    echo "we should have results.";
}

while ($row = mysqli_fetch_row($result)) {
    echo "Table: {$row[0]}\n";
}

mysqli_free_result($result);

?>

When I access the page through localhost I get this:

Host information: Localhost via UNIX socket 
1. MySQL Error: 
result: NULL 

2. MySQL Error: 
result: NULL 

3. MySQL Error: 
result: NULL 

4. MySQL Error:

When I execute the same queries from phpMyAdmin, they execute successfully and return non-NULL results.

I have two questions:

  1. What code can I add to help diagnose this problem?
  2. Do I have a error in my code that is causing me to get NULL results.

Solution

  • Have you enabled mysqli extension in PHP? Check apache error log. Look at this question for example. mysqli is disabled by default, you need to uncomment line

    extension=php_mysqli
    

    in php.ini file.