Skip to main content
Participant
Jan 21, 2013 10:51 PM UTC
Answered

Converting to MYSQLI

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

This topic is closed to new replies. Start a new post to keep the conversation going.
Tim Norton
Tim NortonCorrect answer
Participant
Aug 22, 2014 9:45 PM UTC

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;

}

?>

12 replies

Tim Norton
Tim NortonCorrect answer
Participant
Aug 22, 2014 9:45 PM UTC

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;

}

?>

Translate
Participant
Apr 3, 2015 7:30 AM UTC

Perfect my friend !!!

It Works !!!!

Translate
Participant
Jan 5, 2014 11:32 AM UTC

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.

Translate
Legend
Jan 6, 2014 10:58 AM UTC

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');

Translate
Participant
Aug 17, 2013 4:13 AM UTC

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);

Translate
Inspiring
Jan 22, 2013 11:29 AM UTC

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().

Translate
HelpPWAuthor
Participant
Jan 22, 2013 1:22 PM UTC

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

Translate
Participant
Mar 25, 2013 5:58 PM UTC

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!!!

Translate