MySQL advice needed

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,413
Reaction score
7,522
Location
Bellville
Hello.

Okey these of you that followed progress are being made and the journey to PHP Ninja is going good, however I am stuck and would appreciate some guidance

So I am working with SQL to manipulate data etc, but one website uses this and another website uses that and stackoverflow says dont use this use that.

So now I just want to know if someone can point out what I will have to look out for to see what is depreciated and what is still active.

For instance I should not use this:

Code:
mysql_*
mysql_connect(localhost,$username,$password);

But instead use this:

Code:
$conn = new mysqli($servername, $username, $password, $dbname);

And here is where I get confused: Because the way I understand
Code:
mysql_*
means stuff like this is also wrong?;

Code:
mysql_query
mysql_fetch_array
mysql_num_rows
mysql_numrows
mysql_result
etc?

I know this is a bit of a mess I am just trying to ask some guidance as to what I must and musnt use and if I understood the above section correctly regarding this:
Code:
mysql_query
mysql_fetch_array
mysql_num_rows
mysql_numrows
mysql_result
etc?

What are the correct ways to do that?

Also does that mean this tutorial is incorrect?;
https://www.siteground.com/tutorials/php-mysql/display_table_data.htm

I really appreciate the replies in advance.
 
Basically mysqli allows a few more than standard mysql.

I didn't follow your last posts but If you're just learning, then I would say learn mysqli.
It's not that your understanding is wrong, it's just the older way of doing things but it will still works.

Unfortunately, a lot of the apps I started just before mysqli was properly introduced and I can't be bothered to change over.

New apps I am starting, there I am beginning to use it because the additional interfacing and prepared statements will most likely come in handy down the line.
 
Even mysqli isn't really recommended these days. PDO is better because it supports multiple database types (you can interchange between MySQL and SQLite, for example) without having to change as much code.

You could try keep up with the latest PHP patch notes, but there isn't really all that much that changes. PHP has been about the same for years now, the main additions being security and performance updates. There are sometimes nice additions which help the programmer by allowing them to do things in an easier way, such as how arrays were changed in PHP 5.4:
Code:
<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>
(code from www.php.net)
 
As per above post, use PDO.. Apparently mysql_query and the likes will be dropped at some point..
 
Last edited:
Even mysqli isn't really recommended these days. PDO is better because it supports multiple database types (you can interchange between MySQL and SQLite, for example) without having to change as much code.

You could try keep up with the latest PHP patch notes, but there isn't really all that much that changes. PHP has been about the same for years now, the main additions being security and performance updates. There are sometimes nice additions which help the programmer by allowing them to do things in an easier way, such as how arrays were changed in PHP 5.4:
Code:
<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>
(code from www.php.net)

I looked at this:

http://php.net/manual/en/mysqlinfo.api.choosing.php

And decided mysqli is easier for me and since it's my VM and Cpanel I will always use Mysql db's for now.

This is my issue:
#1
Code:
mysql_query
mysql_fetch_array
mysql_num_rows
mysql_numrows
mysql_result

How would I do that using mysqli in the correct way

#2
and is this website incorrect:?
https://www.siteground.com/tutorials/php-mysql/display_table_data.htm
 
As per above post, use PDO.. Apparently mysql_query and the likes will be dropped at some point..

This is correct. the MySQL procedural library is already deprecated and MySQLi is actively used, however, PDO seems to be joining the PSR-4 standard. PDO Also offers easier escaping and general security methods that you wish MySQLI had and has much easier prepared statements. Another nice thing about PDO is that you don't need to write a lot to do a little (like you have to do in MySQLi) and it's pretty usable out-of-the-box.

PHP:
$handler = new PDO('...');
$query = $handler->prepare("SELECT * FROM XYZ WHERE a = :b");
$query->execute(array(":b" => $b));

That is pretty general use for PDO (but we can take additional steps)

I have a class that I made that applies to any project that relies on PDO with the ability to execute a query and specify the escaped values in one method with two parameters.
 
Top
Sign up to the MyBroadband newsletter
X