Connect mysql with php using docker and workbench

I am on page 135 of php &mysql ninja book. So far so good. At this point I got lost because the codes given had no obvious instruction as to their file names and types and also as to which software they should be uploaded to.

I am using mysql workbench and docker
Look at this from page 135
new PDO(‘mysql:host=hostname;dbname=database’, ‘username’, ‘password’)

where is that put and what file type is it. Is there a webpage that lays out step by step how to connect mysql using docker and workbench. I do not see a step by step process in the book even though there is sifficient explanations about this and about that.

This really downgrades this book, if the author assumed that everyone knows how to do this and would not need to be walked through step by step.

I did read through some related posts on this community but there did not seem to be any using docker. In the beginning of the book up to page 135, the writer took care for step by step and suddenly in chapter 4 novices are left in the dust. This is terrible! And it is not a cheap book.

new PDO( is a PHP statement.

If you keep reading though that section, on the next page Tom tells you how to store that PHP statement’s result as a variable, then how to handle exceptions (137-139), and on 140 he mentions “The complete code can be found in Example: MySQL-Connect

Though on that note… Flag for SP Staff: In the physical form of the book, the footnote link on page 140 is not correct, and lists the URL for this example as, when it is in the Github repo under the MySQL-Connect-Complete branch instead (or is it the PHPMySQL-Connect branch? There seem to be two identical branches?); there is no Branch named MySQL-Connect. Can someone confirm the footnote in the online form?

@m_hutley ,
Thanks for the prompt reply. The assumption about this book by the author is that every reader knows where to put file. Okay, it is a php file as you stated. But once it is created or even copied from Github, then where does the file go, especially since there are several locations, like the public folder or the templates folder and the file type??? sometimes he uses .php and other times .html.php.

I realize an experienced developer knows this intuitively but it is not so with inexperienced student and this book is for novices.

I don’t see how I could get through this book unless I get help.

So for clarity… you went through Chapter 1, including page 21, where it shows you where to put a file in order for it to be viewable?
And the explanation of how the folders work on page 22, which shows you how you can make several sites on the vagrant host?

@m_hutley , I gather from your reply, that thru the book, the specific folder for a file may not be listed and one has to remember or re-read the information on 21-22. But this really means that the writer assumed that most persons using this book would be developers who are familiar with these processes but the book is not advertized like that.
Anyway it is what it is. I will follow your advise. Hopefully, I will be able to proceed but it is tough beyond chapter 4 because I read ahead of that and was totally lost.

Well even if the book is for… “Novices to Ninjas”… then yes, if i were the author, i would expect people to read chapter 1 first and understand it, then move on to chapter 2, not jump into the middle of the book and go “why isnt this explained to me”.

Incidentally, I did master php and css to some extent. I wanted to learn basic php next and to get the jist of its applications.

I did read and do everything through the first three chapters and I did so painstakingly. But in chapter 4 there is a change because some instructions are missing. I feel however that a seasoned develop reading the book would have no lack of integration because of being familiar with much of the methods and script already.

But a novice would not be equiped like that. Even after doing the first 3 chapters, that novice would not suddenly be able to fill in missing steps.

Okay… well, we disagree then. In my opinion, even a novice that read and understood chapter 1, where it tells you about the file structure, and chapter 2, where it shows you PHP code and to call your file [something].php, when given no less than 20 examples in github repos and explicit definitions of how to create, name, extension, and place files throughout the chapter, should be able to comprehend a 21st example from the github repository that uses the exact same file structure, format, and extensions.


I don’t have time to read the whole book right now, but the above is just example code.
The actual code that makes the connection is that found at:-

Though again it looks to me like a working example of making a connection, as ordinarily I don’t think you would say 'Database connection established.', but just get on and do what the application does.
But I do see text telling where to put the files.

@SamA74 ,
Thanks for that. Yes I see at github link that there are two files which go into two folders namely public and templates. Both are .php

However I got lost at the way to connect mysql with php. Thanks for making it clear that the one line and two line codes on page 133,135,136 are example code. The whole thing is confusing if one has not done this before and is not used to the coding vocabulary.
I looked online for a tutorial where it has just the steps to do that with docker and I was unable to find anything.

I am struggling to understand the relationship between docker and mysql work bench
and also how the whole thing is set up.
That is not obvious to a beginner.
Thanks for even responding to the post.

After using the files from git as listed near the bottom of 146 and
When I open the URL which is on page 146,
I get this:

?php> if(!isset($_POST['firstname'])) { include _DIR_ . '/../templates/form.html.php'; } else { $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; if ($firstname == 'Tom' && $lastname == 'Butler') { $output = 'Welcome o glorious leader! '; } else { $output = 'Welcome to our website, '. htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') . ' ' . htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8') . '!' ; } include _DIR_ . '/../templates/welcome.html.php';

So… for me, the URL at the bottom of page 146 is the one Sam linked (MySQL-Connect-Complete), which would not be possible of generating the blurb you have there. Those words do not exist in that example. Have you cloned the correct branch?

@m_hutley ,
Thanks for the response. I would first have to learn how to clone. So I am in a fix.
Knowing html and css is definitely not enough qualification for using the book.

So did you follow the instructions on page XXV, under “The Code Archive”? Or… what did you do when you say you “used the files from git”?

The page at github has this url

I copied one file from the public folder and one from the templates folder.

I put those in the corresponding folders in my laptop.

Then I tried to open the index file and go the text above.

Right now I deleted the files because of sheer frustration.

There is something I am not grasping.

I am not sure where page xxv is or the code archive.
Thanks for your interest in this matter, especially since you are not being paid to reply.

That looks like the raw PHP code appearing in the browser, as if PHP isn’t parsing.
That could mean that PHP isn’t running in Docker, but then the opening PHP tag is wrong, is should begin <?php not ?php> so it could just be a typo.
Though this still doesn’t explain the fact @m_hutley points out, that the code doesn’t exist in those files.

I will take a break and then begin the book from page 1 again.

The first thing I would try is correcting the PHP start tag.

It does look like you got ahead of yourself. That bit of code looks like the result of a “Sign up” script, which I think comes later in the book than the database setup. Since the DB setup is a prerequisite of the sign-up/log-in system.
It’s a linear tutorial, if you skip ahead you will get confused and things likely won’t work…

Now I look, that bit is from Chapter 2, earlier in the book.