Can I use mysql and mysqli

I’m wondering can I use connection to database to become with MYSQLi but all other queries to be with mysql?

no.

I need to rewrite all other queries to mysqli.Is there any converter to convert from mysql to mysqli?

You shouldn’t feel the need to mix-match deprecated functions with mysqli_*. Also, if you want to convert to mysqli_*, by appending an i at the end of mysql will not work. You have to actually understand how mysqli_* works. You have to understand that mysqli_* provides functions that mysql_* doesn’t. Also, some functions need more work if you want to use mysqli_*.

I’ve tried to connect db with mysqli and everything is broken.All queries with mysql_fetch_array broken.I need to learn more.Thanks!
I want to migrate to mysqli because I read it’s more secure against xss and more efficient than mysql also PDO is more complicated than Mysqli.

I would say otherwise. esp. when it comes to prepared statements nothing I know beats the complexity of mysqli.

1 Like

It also still exists whereas the mysql interface now only exists in old versions of PHP that are nearing their end of life (PHP 5.5 has security patch support until July and PHP 5.6 still has full support until July and then security patch support for another 12 months after that. Once those are no longer supported there will be no more support for that previously deprecated interface).

The main differences between mysqli and PDO are that PDO supports named entries in place of ? and PDO supports databases other than mySQL.

To convert from mysql_ to mysqli_ using the quickest possible fix is mostly a matter of reversing the order of the parameters and then replacing the few that don’t work with the equivalents that do work.

Implementing all the new features that make mysqli and PDO better than mysql will take a lot longer - but that’s why the new interfaces were introduced 11 years before the old one was removed with 2-1/2 years notice given that the old interface was going to be removed (which happened earlier this month).

1 Like

Actually, I agree with Dormilich on this. Although I’ve used mysqli_* for years. I think PDO is way easier. The only thing I find annoying and hard to do with PDO is the options part when you are trying to connect to the database. If you want stuff like

😶

To be submitted to the database, you have to do a work around with charset=UTF8 and the database. That’s the only annoying part. Everything else is actually way easier than mysqli_*. When you want OOP being printed or returned to you via PDO. You just use ->fetchAll() which will return an stdClass object. This will list everything you specified for your columns in your query via OOP style.

In mysqli_*, you actually have to manually pull columns then create a new stdClass. Then append the variables you assigned to the stdClass and then you can finally print or return that as OOP.

Same with just getting regular associative arrays. In PDO, associative arrays are default (same in mysqli_*). You just do while($row = $stmt->prepare(PDO::FETCH_ASSOC)) and you either return $row or print it and you get your array.

With mysqli_*, you actually have to use get_results to actually display associative arrays (AFAIK). And get_results is only available in certain PHP versions. So you’ll have to make an alternative way to return or print associative arrays.

Although you can just assign variables to ->bind_result() and reference them, this is actually going to take a lot of work if you want to display an entire row. You’ll need to create a manual array and assign them with keys.

Not to mention what Dormilich has also said. In mysqli_*, you HAVE to append the right data type to bind_param or your query will fail. If you are expecting let’s say a string, but for some reason, you specified integer, the query will fail.

s = string
i = integer
d = double (same as integer, but you can use decimals)
b = blob

Correct me if I’m wrong.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.