06.
Fetching Objects from the Database
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
24 Comments
For anyone stil having issues setting up the database even after attempting all the suggested comments. Try the following as it finally worked for me and hope it helps you too:
I had remove all installed previous versions as well.
Used brew's remove & cleanup commands, unloaded the launchctl script, then deleted the mysql directory in /usr/local/var, deleted my existing /etc/my.cnf (leave that one up to you, should it apply) and launchctl plist
Updated the string for the plist. Note also your alternate security script directory will be based on which version of MySQL you are installing.
Step-by-step:
I then started from scratch:
from stackoverflow, can't add the link, sorry, but the title is 'brew install mysql on macOS'
I had to run
mysql.server stopseveral times as it lept telling me that: A mysqld process already exists, but the page wouldn't work.Hey Didi,
Woh, you had a rough experience with running MySQL locally :/ This sounds like a user-specific problem, why did you re-install your MySQL? You had problems running it after the first installation? I can't 100% confirm your steps, but thanks for sharing with others, it will probably be useful for someone!
Also, another good way to spin MySQL server locally - use Docker. That way you can spin a specific version of MySQL server without having to install everything natively in your OS, you need only Docker app. Unfortunately, it requires some little knowledge of Docker, but there're a lot of instructions over the internet about it, so it should not be too hard, especially with some help of ChatGPT IMO.
I hope this helps too!
Cheers!
I use Postgres database, specifically Postgresql 15, and here is the things I had to do to get the init_db.php file to work.
Thank you for sharing it with others @JDCrain!
http://localhost:8000/resources/init_db.phpHello, after MANY tries, this code worked for me.
I use MAMP on a M1 macbook pro.
This is my MAMP config
You must check the password of course but Socket and Port too because only 'localhost:3306' worked for me, without the port I have a weird error.
Is there some setup required for the db outside of this course? When I move the init_db.php file into my project root and try to access it in the browser I get the following error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /Users/thingybob/code/oo/init_db.php:13 Stack trace: #0 /Users/thingybob/code/oo/init_db.php(13): PDO->__construct('mysql:host=loca...', 'root', '') #1 {main} thrown in /Users/thingybob/code/oo/init_db.php on line 13
And I can't just access localhost:306 to get to PMA like in the video
If you happen to be using a Mac and running Acquia DevDesktop for mysql, it uses a socket instead of a port. So the connection strings read (for my setup at least, you might need to locate your active socket if it is somewhere else):
Line 13:
$pdoDatabase = new PDO('mysql:unix_socket=/Applications/DevDesktop/mysql/data/mysql.sock', $databaseUser, $databasePassword);and line 20:
$pdo = new PDO('mysql:unix_socket=/Applications/DevDesktop/mysql/data/mysql.sock;dbname='.$databaseName, $databaseUser, $databasePassword);(But yes, it is a pretty giant assumption to make for this video that the viewer's machine is running mysql and accepting connections with no root password, without any prior configuration...)
Hey Steve,
Thanks for this hint about unix_socket MySQL setup!
Cheers!
Hey ThingyBob,
Hm, no, I don't see any required setup. Well, the only thing you need to check is your DB credentials. As you can see from the error, it failed on line 13. That's exactly where we create new PDO object with proper credentials. Please, check your credentials and probably tweak them to match yours and it should work.
Cheers!
Thanks for getting back to me. How is the mysqld service running if no set up is required and nothing has been installed prior? Are you saying this would work on a new Mac with no setup? Does the PHP built in server need to be running? Is it part of that? Doesn't the fact that I can't access localhost:306 like in the tutorial mean that the service isn't running?
Hey ThingyBob,
Are you on Mac? IIRC there's no MySQL installed on Mac out of the box, so if you didn't install it - you have to do it first :) I'm on Mac also, and installed MySQL via Homebrew, I'd recommend you to do the same.
Also, why are you saying 306 port? The default MySQL port should be 3306 :)
And nope, you don't need to run PHP built-in webserver in order to get access to MySQL = MySQL has its own server. You need to run a web server only when you want to open a PHP file in browser. If you work in console - you don't need that webserver at all, you will see the output in console in any case.
I hope it clarifies things for you :)
Cheers!
OK gotcha so, initial mysql setup required. That's what I meant in my original comment. Thanks for that, will get that installed. I was referring to port 306 because that's what used in the video (skip to 1:12) so I didn't know if the project ran somehow ran something to install mysql and set it to listen on that port, or if it somehow comes with the built in PHP web server.
Thanks for the assistance :)
Hey ThingyBob,
Ah, yeah, I see... that 306 is PhpMyAdmin port, that's another tool (server) that is separate, not the same as MySQL server, so those serves should have different ports. If you want PhpMyAdmin - you would need to install it too, it does not come with MySQL installation. But you should point PDO DB credentials to MySQL port, not PhpMyAdmin port.
Cheers!
On Ubuntu 22.04:
Xampp, place your project in folder
opt/lampp/htdocsRun xampp:
sudo /opt/lampp/xampp startThen in the browser start your project with
http://localhost/your_project_folderTo see you DB in the browser with PHPMyAdmin:
http://localhost/phpmyadmin/In ShipLoader class:
Hey DXS,
Thanks for this tip for Xampp users :)
Personally, I would recommend you - while you're on Ubuntu! not Windows :) - use a built-in Symfony web server we're using in the video. It should be enough for local development and it simplifies a lot of things :) But if you do want to continue using Xampp with a real web server - that's totally up to you ;)
Cheers!
Hi there - I've read through the comments below and am not able to resolve this error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /Users/ivana.abreu/sites/code-oo/oo/init_db.php:13 Stack trace: #0 /Users/ivana.abreu/sites/code-oo/oo/init_db.php(13): PDO->__construct('mysql:host=loca...', 'root', '') #1 {main} thrown in /Users/ivana.abreu/sites/code-oo/oo/init_db.php on line 13
The init_db.php is in the root folder so I don't know why I am getting this error.
I have mysql installed.
Please assist, thanks.
Hey @Deon-K
That error has nothing with this file, it's about mysql connection, as I see PHP can't find host to connect, try to change
localhostto127.0.0.1and check if you mysql server is runningcheers!
Thanks Sadikoff! I tried that but did not work. Turns out I was running mysql in a container and not locally. All fixed now after installing mysql locally.
woh containers can bring some headache with connections and it's pretty hard to debug, the most common issue is wrong port mapping. However I'm happy that you solved issue!
Cheers!
For some reason I am getting this error if I try using the Jedi Fighter. I am inputting a value for the number of ships for both/ I have filled in the entire form correctly.
~~~~~~~~~~~~~~~~
Don't forget to select some ships to battle!
~~~~~~~~~~~~~~~~
It works without error for all other ships and this problem did not show up until the database connection was added to the program.
All ships are showing up correctly in the ship selection area of the main page.
nevermind.... I saw that this is handled in the next video....
Awesome!
Lines 13 & 20 are explicitly using the string 'root' instead of referencing $databaseUser in the init_db.php file.
Great catch Tim! I've fixed this at sha: https://github.com/knpunive... and sha: https://github.com/knpunive... - and just deployed those updates.
Thanks!
"Houston: no signs of life"
Start the conversation!