PHP/Mysql Connector TroubleShooting And Setup
In this quick review tutorial we go over establishing a back-end access to a mysql server.
Not many people write raw PHP handlers any more, and even less code to connect to a mysql 8.0. Here are some basic tips, we will start with this basic code block:
<?php
if (session_status() === PHP_SESSION_NONE)
{
$servername = "199.200.100.50";
$serverusername = "php";
$serverpassword = "password!";
session_start();
$user = $_POST['username'];
$pass = $_POST['password'];
$pass = crypt($pass, $pass);
$conn = new mysqli($servername, $serverusername, $serverpassword, "test");
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
print("dkdkd");
}
- Make sure you have your modules installed and that you restart your php apache2, as in:
sudo apt-get install php-mysql -y
sudo service apache2 restart
One can make sure that the module is enabled in the /etc/php directory with:
grep -rni "mysql.so"
Now we want to make sure that we have the user created - locked down naturally to only the database that we are connecting to.
- Note! $serverusername adds your ip address invisibly on the connection. So in the above code block mysql will not see 'php' as the connecting user - it actually sees 'php'@'199.200.100.50' - so we need to make our permissions match:
CREATE USER 'php'@'199.200.100.50';
GRANT ALL PRIVILEGES ON *.* TO 'php'@'199.200.100.50';
ALTER USER 'php'@'199.200.100.50' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
After this point it is a matter of testing for connection by stepping the code block:
Note if you need a primer on getting PHPStorm to work with HTML Sessions - for your benefit!
You can step your php code and it will show up as :
With the $conn fully inspectable:
Summary: Without PHPStorm this would be impossible or take endless amounts of time. If you are doing full-stack where you are dealing with CSS / HTML / Javascript / PHP / Mysql PLUS the debugging environment it can be quite overwhelming so we really recommend this for the cost per month.
Yes it is an expensive product - but what is the value of your own dollar-cost if your productivity increases in many cases 6-800%?? This has been my actual resulting experience. It becomes insignificant. You get stuff done. You can also really tell that many 'answers' on fullstack have a summary 'throw spaghetti' till I get function. From this people will come along and 'try this and that' until they get a result.
Hope this helps you!