Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Converting plugins to mysqli
There are a lot of threads about this at the moment so I’m consolidating the info here as a catch-all for how to update any plugin that uses mysql_*
functions. You’ll know if a plugin does this because, depending on your PHP version, it’ll spit out errors or warnings such as:
access denied for user 'abc'@'localhost'
or:
Warning mysql_get_server_info(): A link to the server could not be established
or in PHP 7:
Fatal error: Uncaught Error: Call to undefined function mysql_connect()
The problem stems from the fact that in Txp 4.6.0 we moved from using mysql_*()
to mysqli_*()
(note the ‘i’) functions as a stop-gap on our way towards PDO.
Any plugins that exclusively use the core’s safe_*()
or get*()
calls are immune to any issues. There are, however, a few PHP functions that plugin authors use directly, due to gaps in our API: mainly mysql_real_escape_string
, mysql_get_server_info
and mysql_error
. To fix all three — and others — just do this:
- In the plugin function that surrounds the
mysql_*()
call, ensure there is aglobal $DB;
line somewhere near the top of the function. - Change the call name by adding an
i
after themysql
. - Add
$DB->link
as the first parameter to that call. - Repeat for all places where
mysql_*()
functions are used.
tl;dr example: convert:
mysql_real_escape_string($content);
to:
global $DB;
...
mysqli_real_escape_string($DB->link, $content);
and you’ll be on your way.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
#2 2016-12-15 15:56:50
- marisa1981
- New Member
- Registered: 2016-12-15
- Posts: 1
Re: Converting plugins to mysqli
Hi! it is not working for my instalation (hak_tinymce), can you recomend to me some other similar plugin? I get kuo_tinymce_cdn but I can not add images directly.
Thank you!
Offline
#3 2016-12-16 14:17:45
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,310
Re: Converting plugins to mysqli
Hi, marisa1981, and welcome to the forum. hak_tinyMCE is the only WYSIWYG plugin with image connection to the textpattern DB. Sorry.
But can you tell us exactly what isn’t working, do you get errors, aren’t there no formatting buttons any longer displayed in the Write pane etc.?
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: Converting plugins to mysqli
What, if a plugin only contains calls like
mysqli_affected_rows()
or mysqli_error()
- do we still have to fill in $DB->link
resulting in mysqli_affected_rows($DB->link)
or (EDIT) mysqli_error($DB->link)
,
and given that
global $DB;
is involved in any of the respective functions, near the top of the respective function call?
Example from smd_user_manager v0.21.
Last edited by jayrope (2018-04-17 19:04:09)
A hole turned upside down is a dome, when there’s also gravity.
Offline
Re: Converting plugins to mysqli
Yes, same rules apply to those two functions: import $DB
global once into the top of each function where the call is used, add the ‘i’ then stick the link in the brackets.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
Re: Converting plugins to mysqli
Perfekto, thanx again.
A hole turned upside down is a dome, when there’s also gravity.
Offline
#7 2019-03-04 14:46:51
- neptho
- Member
- From: A cold, dark place.
- Registered: 2006-02-01
- Posts: 48
Re: Converting plugins to mysqli
Is there any specific reason we don’t go PDO, or some other functional wrapper to build our queries?
I know I’m an old cranky person these days, but I still see some things shipping with addslashes(), or worse, mysql_real_escape_string() (with no current DB connection), and I have a personal issue with granting a global $DB to functions.
Offline
Re: Converting plugins to mysqli
neptho wrote #316853:
Is there any specific reason we don’t go PDO, or some other functional wrapper to build our queries?
Offline
#9 2020-10-15 09:36:48
- ibadullah
- Member
- From: Kabul, Afghanistan
- Registered: 2017-09-16
- Posts: 49
Re: Converting plugins to mysqli
Hello Everyone!
i need a textpattern plugin for newsletter, can anyone help me.
Offline
Re: Converting plugins to mysqli
ibadullah wrote #326400:
i need a textpattern plugin for newsletter, can anyone help me.
Yes! I’m in the process of writing a proper newsletter plugin/framework from scratch, but in the meantime I’ve patched mem_postmaster (formerly bab_postmaster) to work on Textpattern 4.8.
I’ve not quite finished it yet but it’s nearly there. You’re welcome to try it out when I get it into a suitable state of working.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
#11 2020-10-18 08:41:02
- ibadullah
- Member
- From: Kabul, Afghanistan
- Registered: 2017-09-16
- Posts: 49
Re: Converting plugins to mysqli
Thank you very much Bloke Developer for the assistance, i hope you will finish it as soon as passable.
it will be a great help for the textpattern community.
i will wait for the plugin impatiently. thanks again
Offline
Re: Converting plugins to mysqli
I’m happy it’s working as well as can be expected given the ancient code. I just have to test the PHPMailer module I added to it yesterday. I haven’t looked at the ‘direct send’ feature yet though. Not sure if anyone uses that.
Last edited by Bloke (2020-10-18 09:29:50)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online