• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Converting to MYSQLI

New Here ,
Jan 21, 2013 Jan 21, 2013

Copy link to clipboard

Copied

I am trying to convert an old site to use mysqli rather than mysql.

Hit a bit of a stumberling block with this section of code

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($theValue) : mysqli_escape_string($theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

I keep getting the errors

Warning:  mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampplite\htdocs\games\test.php on line 15

Warning:  mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampplite\htdocs\games\test.php on line 15

If I add a connection like this

  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($games,$theValue) : mysqli_escape_string($games,$theValue);

get the error

Warning:  mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampplite\htdocs\games\test.php on line 12

Warning:  mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampplite\htdocs\games\test.php on line 12

Could someone please tell me what I am doing wrong

Many thanks

TOPICS
Server side applications

Views

16.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

New Here , Aug 22, 2014 Aug 22, 2014

I solved this by putting my connection include in a function called dbconnect()

Then I injected the function for the connection handle..

Like so

$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string(dbconnect(), $theValue) : mysqli_escape_string(dbconnect(), $theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "''";

      break;   

*****************

Connection php file

<?php

function dbconnect()

{

  $hostname_PrintSc

...

Votes

Translate

Translate
Explorer ,
Jan 22, 2013 Jan 22, 2013

Copy link to clipboard

Copied

From the PHP manual:

For those accustomed to using mysql_real_escape_string(), note that the arguments of mysqli_real_escape_string() differ from what mysql_real_escape_string() expects. The link identifier comes first in mysqli_real_escape_string(), whereas the string to be escaped comes first in mysql_real_escape_string().

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2013 Jan 22, 2013

Copy link to clipboard

Copied

I have tried putting the the string in both locations.

This is standard dreamwearver code tha tI am trying to convert from mysql to mysqli.

I have read quite a lot around this and just do not understnad.

Having looked at it a little more it might be to do with the variable $theValue but cannto see where this get populated.

Many thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 25, 2013 Mar 25, 2013

Copy link to clipboard

Copied

Did you ever get this solved?  I'm doing the same conversion and am having this exact same problem.  I would really appreciate  example code showing the correct arguments for mysqli_real_escape_string().

Dreamweaver should really upgrade to mysqli and give users examples of how to convert existing code!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 16, 2013 Aug 16, 2013

Copy link to clipboard

Copied

Old thread, but here we are 8 months later STILL without mysqli. This worked for me;

As rhecker3 suggested swap the link identifier with the string In HelpPW example above.

$theValue = function_exists("mysqli_real_escape_string") ? mysql_escape_string($theValue) : mysql_real_escape_string($theValue) ;

as oppoed to the mysql version of

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 05, 2014 Jan 05, 2014

Copy link to clipboard

Copied

After starting to convert to mysqli with dreamweaver cs5.5 I had the same problem as mentioned above with the function GetSQLValueString. Finally I use this code that works fine with mysqli-extension of php:

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

/*Global variable $con is necessary, because it is not known inside the function and you need it for mysqli_real_escape_string($con, $theValue); the Variable $con ist defined as mysqli_connect("localhost","user","password", "database") with an include-script.

*/

  Global $con;

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

  $theValue = mysqli_real_escape_string($con, $theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

   return $theValue;

}

}

END OF CODE

It's a pity that Adobe does not implement the new mysqli-functions in dreamweaver cc. Even the server behavier extension is OLD mysql. But mysql is deprecated.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 06, 2014 Jan 06, 2014

Copy link to clipboard

Copied

Or you could just use:

$formName = $conn->real_escape_string(trim($_POST['formName']));

$conn is the new mysqli connection variable:

$conn = new mysqli('server' , 'username' , 'password' , 'database');

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 29, 2016 Jan 29, 2016

Copy link to clipboard

Copied

that solved my problem

Thank u

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 22, 2014 Aug 22, 2014

Copy link to clipboard

Copied

I solved this by putting my connection include in a function called dbconnect()

Then I injected the function for the connection handle..

Like so

$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string(dbconnect(), $theValue) : mysqli_escape_string(dbconnect(), $theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "''";

      break;   

*****************

Connection php file

<?php

function dbconnect()

{

  $hostname_PrintSchedDataCn = 'localhost';

  $database_PrintSchedDataCn = 'nothing';

  $username_PrintSchedDataCn = 'nothing';

  $password_PrintSchedDataCn = 'nothing';

  $PrintSchedDataCn = mysqli_connect($hostname_PrintSchedDataCn, $username_PrintSchedDataCn, $password_PrintSchedDataCn, $database_PrintSchedDataCn) or trigger_error(mysqli_error(),E_USER_ERROR);

  return $PrintSchedDataCn;

}

?>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 03, 2015 Apr 03, 2015

Copy link to clipboard

Copied

Perfect my friend !!!

It Works !!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 10, 2016 Oct 10, 2016

Copy link to clipboard

Copied

Thanks so much, this worked for me!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 29, 2020 Jun 29, 2020

Copy link to clipboard

Copied

Thanks bro.it worked

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

LATEST

thank you so very much its working perfectly

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines