Post by Cue4cheap » Tue Oct 06, 2015 5:01 am

OC version 1.5.6.4.
I have upgraded to mqsqli and everything but a contribution is working.

It has a line in it like:
$cat = trim(mysql_real_escape_string($product['cname']) );

Since I have moved to mysqli I figured it would be as simple as changing mysql to mysqli on that line BUT when I change that and run the script it errors out with mysqli_real_escape_string() expects exactly 2 parameters, 1 given...

Searching online I see mysqli requires a link to the database. An example is:
$city = mysqli_real_escape_string($link, $city);

Where $link is declared above that line like:
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

SOOOOOOOO since the contrib has this above in it:
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

I thought I should be able to then further change that line above to
$cat = trim(mysqli_real_escape_string($db,($product['cname'])) );
But it doesn't work. Am I close or am I really missing something?

Thank you,
Mike

cue4cheap not cheap quality


Expert Member

Posts

Joined
Fri Sep 20, 2013 4:45 am

Post by Cue4cheap » Tue Oct 06, 2015 10:46 am

Cue4cheap wrote:OC version 1.5.6.4.
I have upgraded to mqsqli and everything but a contribution is working.

It has a line in it like:
$cat = trim(mysql_real_escape_string($product['cname']) );

Since I have moved to mysqli I figured it would be as simple as changing mysql to mysqli on that line BUT when I change that and run the script it errors out with mysqli_real_escape_string() expects exactly 2 parameters, 1 given...

Searching online I see mysqli requires a link to the database. An example is:
$city = mysqli_real_escape_string($link, $city);

Where $link is declared above that line like:
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

SOOOOOOOO since the contrib has this above in it:
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

I thought I should be able to then further change that line above to
$cat = trim(mysqli_real_escape_string($db,($product['cname'])) );
But it doesn't work. Am I close or am I really missing something?

Thank you,
Mike

Well I resolved it by doing exactly that: I created
$link = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

and put it under:
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
and changed the line to:
$cat = trim(mysqli_real_escape_string($link,($product['cname'])) );


It works BUT I would love to know the best way and IF I can use the original $db variable like
$cat = trim(mysqli_real_escape_string($db,($product['cname'])) );

instead of creating the other variable: $link = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

Thank you,
Mike

cue4cheap not cheap quality


Expert Member

Posts

Joined
Fri Sep 20, 2013 4:45 am

Post by Cue4cheap » Thu Oct 08, 2015 5:37 am

Cue4cheap wrote: and put it under:
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
and changed the line to:
$cat = trim(mysqli_real_escape_string($link,($product['cname'])) );


It works BUT I would love to know the best way and IF I can use the original $db variable like
$cat = trim(mysqli_real_escape_string($db,($product['cname'])) );

instead of creating the other variable: $link = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

Thank you,
Mike
Could someone explain the $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); line to me and why it doesn't work the same as $link = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);?

It may be a novice PHP question but that is what I am. :)

Mike

cue4cheap not cheap quality


Expert Member

Posts

Joined
Fri Sep 20, 2013 4:45 am

Post by Cue4cheap » Sun Oct 11, 2015 10:24 pm

Still looking for someone who will be able to help me understand that line and why I can't use it in the mysqli_real_escape_string line.

Anyone? :)

Thank you,

Mike

cue4cheap not cheap quality


Expert Member

Posts

Joined
Fri Sep 20, 2013 4:45 am

Post by artcore » Sun Oct 11, 2015 11:29 pm

$db is an instantiation of the Opencart db class
$link is plain old procedural using the php mysql function

try $cat = $this->db->escape($product['cname']); (Shouldn't work in the tpl when using OC2)

I wonder why people don't use the framework, it's bound to break stuff ;)

Basically in MVC PHP those php functions are wrapped in a class for convenience

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by Cue4cheap » Tue Oct 13, 2015 12:45 am

artcore wrote:$db is an instantiation of the Opencart db class
$link is plain old procedural using the php mysql function

try $cat = $this->db->escape($product['cname']); (Shouldn't work in the tpl when using OC2)

I wonder why people don't use the framework, it's bound to break stuff ;)

Basically in MVC PHP those php functions are wrapped in a class for convenience
I see the two are basically the same but I guess I need to do more reading because I'll have to say it is still over my head.

Thank you for the reply.

cue4cheap not cheap quality


Expert Member

Posts

Joined
Fri Sep 20, 2013 4:45 am
Who is online

Users browsing this forum: No registered users and 4 guests