Apache server doesn't work!

Hello there! could you help me please ? I’m stuck in my project. I use a local web server and data server (ALL INCLUDED IN XAMPP), and all servers are running as you can see
Capture d’écran 2018-12-31 à 17.04.34
everything is fine except that after created my database using PhpMyAdmin and I did set a password which is 72981352Mmm and the default username is root. but I wanna show my database’s table using php from the web browser. the main file is an index.html which contains a button and when the button is submitted the database’s table data must be displayed in the browser. but if I try this, it says THIS PAGE DOESN’T WORK. I do not know why. here are my code:

Capture d’écran 2018-12-31 à 17.06.34
Capture d’écran 2018-12-31 à 17.06.54
help me please !!!

I think you need to create an instance of the PDO base class. Check this php documentation, it’s just a few simple steps:
PDO connections

But that is just the connection to the database. If you want to connect your db to a form and post stuff, you’ll need to create a basic API.

should I just put this code in my PHP file and everything will be done ?

Well, then you are connected (if you replace the settings with your own), but it won’t print anything. If you want to test your connection, use the try - catch block from the documentation.

would you mind sending me the entire code please ?

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    foreach($dbh->query('SELECT * from FOO') as $row) {
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";

it works but it seems a little weird, so difficult to read

You just print out the array. Don’t use print_r then but echo the array items separately.

I replaced the print_r() fonction by ECHO and things get more complicated
Capture d’écran 2022-02-10 à 09.03.46
please send me code to print the result in a good manner in the browser

I’m here to help out if you can’t find the bug in your code, but I’m not going to write it for you from scratch.
Have a look at this CC php course, that should be what you are looking for:
Printing associative arrays

thank you very much for your help !

1 Like

Hello ! firstly I wanna tank you for your help, everything is working properly with PDO, I want just know why it doesn’t work with MySQLi ?

1 Like

What do you mean exactly? Why your initially posted code didn’t work?
Which php version do you use? The mysql_connect() function you used in your first code is deprecated since version 5.5 and was removed with 7.0:
What does your code with mysqli look like?

hello Sir ! can you help me ?

Hmm, with this old task? I tried to understand what you need help with, exactly. That’s why I asked these questions. If you answer them, maybe I can help.

not about this topic, you helped me and it worked fine. it’s just that I’m working on files with mysql and php. I wanna save a picture in the server and then display it through PHP. friends told me that it’s a best practice to not store images files in mysql, instead I have to store only the path of the file in mysql VARCHAR(225) type . but my code doesn’t work, nothing is shown in the browser. I created many topics about it on this forum but nobody answers me, that is why I came back to you because I remember you helped me in my first question. help me please Sir. so here is my code:
THE TABLE IS ALREADY CREATED IN MYSQL, AND THE TABLE NAME IS profil, the table contains two rows : id the primary key auto_increment and the pic row VARCHAR(225).

// the image file is : /Applications/images/;
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $user, $password);
$sql = "SELECT pic FROM profile";
$img = $conn->exec($sql);
echo $img;
```  but no image is shown in the browser.

I don’t know if I’m the best to help you with this as I’m not so firm with php and MySQL.
But if you have a query "SELECT pic FROM profile" ‘pic’ would be the name of the column. That means that the query would give you all image paths stored in the database.

If you want to see if you get any valid result from the query, I recommend that you either use var_dump() or print_r() rather than echo. echo prints strings. If your result is either null or an array, echo would not print anything or not anything of great use.

(fyi: about 50% of the world’s population is female :nail_care: :wink:)

consider I have one image path stored in the table.
by using the following code, will it display the image in the browser ?

$sql = "SELECT pic FROM profile";
$img = $conn->exec($sql);

According to the docs it looks like ->exec returns a number or false, so: no.
It would still return the result as an array. So you will have to do it differently. You could use the code snippet from above:

You could replace the asterisk by the column name ‘pic’ and get the path as a string with implode() as described in this lesson.

You would then have to embed the php result in an html image tag.

1 Like

you’re my hero Sir :grinning: :grinning: thank you very much, it works