Jump to content

[MariaDB] "This page isn’t working"?


Shohreh

Recommended Posts

Hello,

This is a newbie question.

On a test host at home, I first installed nginx and PHP-FPM, and made sure phpinfo(); worked.

Next, I installed MariaDB and imported a database.

Finally, I tried connecting to it from PHP… and it failed with:
 

Quote

 

This page isn’t working

192.168.0.7 is currently unable to handle this request.
HTTP ERROR 500

 

Here's the script I used:

<?php
// Database settings
$db="mydb";
$dbhost="localhost";
$dbport=3306;
$dbuser="root";
$dbpasswd="test";

$pdo = new PDO('mysql:host='.$dbhost.';port='.$dbport.';dbname='.$db.'', $dbuser, $dbpasswd);
$pdo->exec("SELECT * FROM mytable" );

$res = $pdo->fetchAll();
foreach ( $res as $row ) {
    echo $row['id'];
}

$pdo = null;
?>

 

Any idea why? Could it be I'm missing a module in PHP? What should I check?

Thank you.

Link to comment
Share on other sites

Thanks. No change:

<?php
// Database settings
$db="mydb";
$dbhost="localhost";
$dbport=3306;
$dbuser="root";
$dbpasswd="test";

error_reporting(E_ALL);

$pdo = new PDO('mysql:host='.$dbhost.';port='.$dbport.';dbname='.$db.'', $dbuser, $dbpasswd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pdo->exec("SELECT * FROM mytable" );

$res = $pdo->fetchAll();
foreach ( $res as $row ) {
    echo $row['id'];
}

$pdo = null;
?>

 

Link to comment
Share on other sites

If I first add an echo() the error message dispappears… but still no data:

<?php
// Database settings
$db="mydb";
$dbhost="localhost";
$dbport=3306;
$dbuser="root";
$dbpasswd="test";

error_reporting(E_ALL);

$pdo = new PDO('mysql:host='.$dbhost.';port='.$dbport.';dbname='.$db.'', $dbuser, $dbpasswd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Required to remove "This page isn’t working"
echo("Here");

$pdo->exec("SELECT * FROM mytable" );


$res = $pdo->fetchAll();
foreach ( $res as $row ) {
    //Still no output
    echo $row['group_id'];
}

$pdo = null;
?>


So it looks like PHP can successfully connect to MariaDB, and I only have to find why I get no data back (it works with the CLI).

Edited by Shohreh
Link to comment
Share on other sites

php's error_reporting should (always) be set to E_ALL. when learning, developing and debugging code/query(ies), display_errors should be set to ON. when on a live/public server, display_errors should be set to OFF and log_errors should be set to ON. these setting should be in the php.ini on your system so that ALL php errors will get reported (putting the settings in your code won't cause fatal parse errors to be reported since your code never runs to cause the settings to take effect) and so that they can be changed in a single place. while you are making changes to the php.ini, set output_buffering of OFF. stop and start your web server to get any changes made to the php.ini to take effect and confirm that the settings actually got changed by using a phpinfo() statement in a .php file that you request through the web server.

next, when you make the PDO connection -

  1. set the character set to match your database tables. so that no character conversion occurs over the connection.
  2. as already mentioned, set the error mode to exceptions (this is actually the default now in php8, but set it anyways), so that all the rest of the PDO statements, after the connection (which always uses exceptions for errors), will also use exceptions for errors.
  3. set emulated prepared queries to false, you want to run real, not emulated, prepared queries.
  4. set the default fetch mode to assoc, so that you don't need to specify it in each fetch statement.

since you are just getting started with the PDO extension, save yourself a lot of typing with prepared queries by using simple positional ? prepared query place-holders and use implicit binding, by supplying an array of input values to the ->execute([...]) call.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.