Can't get my PHP code to query database

Excellent.

So now we’re ready to query that database!

Two questions:

  • What happens when you just go to http://localhost
  • Why is there an alt in front of index.php

alt???


Additional question, what is HÜs?
Hüs

When you’re trying to access localhost/3-Verbindung-... ?

What happens if you try localhost/HÜs/3-Verbindung-... ?

I see the XAMPP dashboard/startpage

That’s just a file where I tried some slightly different code, I already deleted it.
The proper index.php returns the exact same error message

Any idea though why PHPStorm keeps opening the page on port 8000, while XAMPP is set to 80?
Remember PHPStorm has a webserver built-in (also set to port 80 though) - how can I tell which one is actually being used?

Exactly… and, I don’t know. You’d have to check if you can change that (the opening on port 8000 part)

PS: Did you see my question about HÜs?

Saw it now. It’s just a subfolder of the XAMPP webroot
It’s all messed up, the PHPStorm webserver was actually set to run on port 8000. I now set it to 80 AND removed its configuration from the project.
XAMPP 's apache is also set to run on port 80. However, when I open the index.php by clicking the browser icon in PHPStorm, it still opens it on port 8000. AND I’m also finally getting useful output ( :woozy_face:):

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1049] Unknown database ''Personen'' in /opt/lampp/htdocs/HÜs/3-Verbindung-Datenbank/index.php:5 Stack trace: #0 /opt/lampp/htdocs/HÜs/3-Verbindung-Datenbank/index.php(5): PDO->__construct('mysql:host=loca...', 'root', '') #1 {main} thrown in /opt/lampp/htdocs/HÜs/3-Verbindung-Datenbank/index.php on line 5

Did you restart PHPStorm after making these changes?

Well at least now we’re getting a response from the database!

I don’t remember if I restarted it then, but did now and the error persisted. I had unnecessary ’ ’ around the name of the database. After removing these, the query works! Just reposting the working code for th record:

<?php
include "config/config.php";


    $pdoObject = new PDO("mysql:host=localhost;dbname=Personen;charset=utf8", "root", "");

$sql = "SELECT id, firstname, lastname, birthdate FROM persons WHERE id = '5'";

$resultTable = array();
try {
    foreach ($pdoObject -> query($sql) as $row) {
        $resultTable[] = $row;
        echo $resultTable[0]['firstname'];
    }
    //iterieren für alle moeglichen zeilen
} catch (PDOException $exception) {
    error_log("PDO ERROR: querying database: " . $exception->getMessage()."\n".$sql);
}
1 Like

Recapping all the problems and solutions:

  1. server errors 500 and 502: I was unable to connect to the database because my php files were not in the webroot directory.
    solution:

…which is the default web root of XAMPP. I’m guessing a different webroot directory can be specified in the server config, but I don’t know whether or not it has to be under the server root directory.

  1. I had trouble telling which of the available webservers (built-in from PHPStorm vs. Xampp vs. the system Apache on my Linux. Xampp runs in the /opt/lampp directory) were being used. I looked into configuring the behavior of the PHPStorm, but not successfully yet, it’s a bit complicated in that regard.
    Solution: Don’t use the browser buttons provided in PHPStorm’s code editor for running the files. I decided to use Xampp and type localhost/relativepath/from/xampp-webroot into the browser’s address bar. That way, any configurations PHPStorm might have regarding the webserver are bypassed.

  2. My code was initially querying a non-existant database, it was just an example.
    Solution: Query correct fields from the actual database I wanted. Don’t forget to also create that db first and fill it with some records.

  3. When querying the right values, they were not printed to the screen.
    solution:

or

print_r is also possible.

  1. The way this code is structured is far from best practise. It’s just a simple query only useful for a one-time demonstration.
    Solution: numerous remarks are in this post how connection to a database can be structured professionally, go through the whole thing. Simple examples also here and here
1 Like