0

At the beginning I will add that I am just starting to write in PHP + mysqli. I was working on mysql all the time .

I created something like a CMS-based website , but everything was / is done in old, decrypted language . I have created two management files "driver_mysql and engine." Everything works as it should on older versions, but I have to move it to mysqli.

SO :

There are functions saved in driver_mysql.php, which I later use in the engine.php itself or in a smaller amount as I make a query.

How i should write 2 functions in mysqli . You probably write that I should learn first and then write . If there was a nice person and could write me how to do these two functions, I would be grateful. I would correct the rest based on this, because I don't know how to connect by myself, because you need 2 arguments

I have set in config.php : $db_host , $user_login , $user_password , $db_name

CODE + FUNCTIONS :

driver_mysql.php

function db_connect(
$db_host,
$user_login,
$user_password
)
    {
        if (
        mysql_connect (
        $db_host,
        $user_login,
        $user_password
                        )
            )
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

function SelectDB($db_name)
    {
        if(mysql_select_db($db_name))
            {
                return TRUE;
            }
            else
            {
                return FALSE
            }
    }

Should I do smth like that ? :

function db_connect($mysqli = mysqli_connect($db_host,$user_login,$user_password)
 {
        if (
        mysqli_connect (
        $db_host,
        $user_login,
        $user_password
                        )
            )
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

AND

function SELECTDB($db_name)
 { 
   $link = mysqli_connect($db_host,$user_login,$user_password)
   if(mysqli_select_db($link, $db_name)
 { return TRUE; } else { return FALSE; } }

I'll add if in engine.php I have

$connect = db_connect(
$db_host,
$user_login,
$user_password);
if($connect == FALSE) { echo 'ERROR MESSAGE'; exit(); }

AND FOR DB SELECT

$db_selecting = selectDB($db_name);
if($db_selecting == FALSE) { echo 'ERROR MESSAGE'; exit(); }

Thank you for any help !!!!

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Is there any reason why you are considering mysqli instead of migrating to PDO? – Dharman Sep 10 '21 at 17:39
  • I linked you to my answer. You should be able to understand from my answer what needs to be changed. However, I would strongly encourage you to forget about mysqli and learn PDO. PDO is easier. Either way, you will have to rewrite the whole code. What you tried does not look like valid PHP code – Dharman Sep 10 '21 at 17:42
  • For example, instead of `db_connect()` use `new mysqli()` like it is described in the manual https://www.php.net/manual/en/mysqli.construct.php#refsect1-mysqli.construct-examples You don't need `SelectDB()` at all – Dharman Sep 10 '21 at 17:43

0 Answers0