Read on to learn about how to connect PHP with a MySQL database using different methods, including mysqli, PDO, and dbForge for MySQL.
The post How to Connect PHP With MySQL appeared first on Devart Blog.
Read on to learn about how to connect PHP with a MySQL database using different methods, including mysqli, PDO, and dbForge for MySQL.
The post How to Connect PHP With MySQL appeared first on Devart Blog.
Help!
I am preparing a presentation for the Longhorn PHP conference titled PHP & MySQL -- How do PDO, MySQLi, and X DevAPI do what they do. This will be a comparison of using PHP with different MySQL Connectors. As far as I can tell there are no three way comparisons of mysqli, PDO, and the X DevAPI PECL extension. And much of the PDO versus myqli stuff looks to me like it has not aged well.
I have good material in the raw presentation about overall features, parameters for prepared queries. And a good section on the how's and whys on prepared queries.
But what else would you like to see in such a presentation? I have read some postings on the web about turning off buffering (fairly simple to do). But what else are would you like to see compared?
…
[Read more]
There was an option during the Fedora 30 Workstation installation
to add the Apache Web Server, but you need to set it to start
automatically. Unfortunately, there was no option to install PHP,
which I thought odd because of how many web developers learn the
trade first on PHP with a LAMP (Linux, Apache, MySQL,
Perl/PHP/Python) stack. You see how to fix that shortcoming in
this post and how to install and test PHP, mysqli
,
and pdo
to support MySQL 8.
Before you do that make sure you install MySQL 8. You can find my prior blog post on that here.
You set Apache to start automatically, on the next boot of the operating system, with the following command:
chkconfig httpd on
It creates a symbolic link:
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → …[Read more]
I have had some requests to write some blogs on the basics of
using PHP and MySQL together. This will not be a series for
the experienced as it will start at a level where I will go into
a lot of details but expect very few prerequisites from the
reader. If this is not you, please move on. If it is you
and you read something you do not understand, please contact me
to show me where I assumed too much.
PHP and MySQL are both in their mid twenties and
both vital in the worlds of developers. With the big
improvements in PHP 7 and MySQL 8, I have found a lot of
developers flocking to both but stymied by the examples they see
as their are many details not explained. So let's get to the
explaining!
1. Use the latest software
If you are not using PHP 7.2 or 7.3 (or maybe 7.1) then you are
missing out in features and performance. The PHP 5.x series
is deprecated, no longer support, and …
Most of the PHP I write runs on Bluemix – it’s IBM self-service cloud, and since I work there, they pay for my accounts :) There are a bunch of databases you can use there, mostly open source offerings, and of course with PHP I like to use MySQL. Someone asked me for my connection code since it’s a bit tricky to grab the credentials that you need, so here it is. Bluemix Environment Variables
In Bluemix, you can simply create a MySQL database (look for “compose-for-mysql” in the catalog), create a set of credentials, and then bind it to your app. I should blog a few more of my PHP-on-Bluemix tricks but you can run a selection of PHP versions and it’s also possible to add extensions that you need, I have found it does have what I need once I figure out how to configure it!
Once the database is bound to the application, then your PHP code running on Bluemix will have an environment variable called …
[Read more]Since PHP 7.0 has been released there's more attention on scalar types. Keeping types for data from within your application is relatively simple. But when talking to external systems, like a database things aren't always as one eventually might initially expect.
For MySQL the type we see -- in the first approximation -- is defined by the network protocol. The MySQL network protocol by default converts all data into strings. So if we fetch an integer from the database and use PHP 7's typing feature we get an error:
<?php declare(strict_types=1); function getInteger() : int { $mysqli = new mysqli(...); return $mysqli->query("SELECT 1")->fetch_row()[0]; } var_dump(getInteger()); ?> Fatal error: Uncaught TypeError: Return value of getInteger() must be of the type integer, string returned in t.php:6
Of course the solution is easy: Either we cast ourselves or we disable the strict mode and PHP will …
[Read more]I love PHP's PDO (PHP Data Objects) extension; it gives a consistent, object-oriented interface to handling all kinds of relational database backends. One thing that annoys me is that the MySQL driver for PDO defaults to a silent error mode which can make SQL errors tricky to spot!
To give you an example, consider the query below (the correct
tablename is country
, so this SQL will fail):
$db = new PDO('mysql:host=localhost;dbname=sakila', 'user', 'pass'); $sql = 'select * from countrt'; $stmt = $db->query($sql); while(($row = $stmt->fetch()) != false) { echo $row['country'] . "\n"; }
The script will output an error because $stmt
is not
an object.
You have a few options here - you can check that you got an
object back before you try to do anything with it, for example.
Alternatively you can prepare()
and then …
I love PHP's PDO (PHP Data Objects) extension; it gives a consistent, object-oriented interface to handling all kinds of relational database backends. One thing that annoys me is that the MySQL driver for PDO defaults to a silent error mode which can make SQL errors tricky to spot!
To give you an example, consider the query below (the correct
tablename is country
, so this SQL will fail):
$db = new PDO('mysql:host=localhost;dbname=sakila', 'user', 'pass'); $sql = 'select * from countrt'; $stmt = $db->query($sql); while(($row = $stmt->fetch()) != false) { echo $row['country'] . "\n"; }
The script will output an error because $stmt
is not
an object.
You have a few options here - you can check that you got an
object back before you try to do anything with it, for example.
Alternatively you can prepare()
and then …
So in my blog series I try to cover all additions to PHP trunk so I have to mention scalar type hints.
<?php function print_float(float $f) { echo $f."\n"; } for ($i = 1; $i < 5; $i++) { print_float( $i / 3 ); } ?>
0.33333333333333
0.66666666666667
Catchable fatal error: Argument 1 passed to
print_float() must be of the type double, integer given, called
in typehints.php on line 7 and defined in typehints.php on line 2
Is expected behavior in PHP's trunk. If you want such a thing to work please use the numeric type hint.
In case that wasn't enought fun: There's more!
<?php function handle_result(int $i) { echo $i."\n"; } $pdo = new PDO("mysql:host=localhost;dbname=test", "user", "pass"); …[Read more]
Lukas is making another attempt at jumpstarting PDO development. I welcome this effort, and will do what I can to help fill in details and make suggestions. Unfortunately, I'm just way too busy with work to be able to commit to more than that.
I also wanted to share some of my thoughts on why PDO has been in a holding pattern for a while, so that more people are aware of it and can work to avoid repeating the same mistakes.
The first thing to note is that the guts of PDO were hard to develop. The PHP script facing API sounds simple enough, but the underlying libraries for each different databases work in different ways, and it was and is a challenge to build PDO in such a way that it can work in the most efficient way.
The second thing, which is really a follow-on from the first, is that the database libraries are complex and nuanced. …
[Read more]