8 Replies Latest reply: Apr 3, 2015 12:30 AM by robertoc64397989 RSS

    Converting to MYSQLI

    HelpPW Community Member

      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

        • 1. Re: Converting to MYSQLI
          rhecker3

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

          • 2. Re: Converting to MYSQLI
            HelpPW Community Member

            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

            • 3. Re: Converting to MYSQLI
              Lakeside_Lou Community Member

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

              • 4. Re: Converting to MYSQLI
                mancorn

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

                • 5. Re: Converting to MYSQLI
                  brpcdesigner_2010

                  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.

                  • 6. Re: Converting to MYSQLI
                    osgood_ MVP

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

                    • 7. Re: Converting to MYSQLI
                      tim norton

                      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;

                      }

                      ?>

                      • 8. Re: Converting to MYSQLI
                        robertoc64397989

                        Perfect my friend !!!

                        It Works !!!!