-1

I'm getting this error on my site after I tried to convert to mysqli on line 26 ($rs = mysqli_query($link,MySQLi) or die(mysqli_connect_error());)

My code is like this

//Get configuration control record from database

$MySQLi = "SELECT configValLong 
              FROM   storeadmin 
              WHERE  configVar = 'controlRec' 
              AND    adminType = 'C' ";
    $rs = mysqli_query($link,MySQLi) or die(mysqli_connect_error());
    $totalRows = mysqli_num_rows($rs);
    if ($totalRows!=0){
        $rows = mysqli_fetch_array($rs);
        $configArr = trim($rows["configValLong"]);
        if (strlen($configArr) > 0 ){
            $configArr = explode("*|*",$configArr);
        }
ndm
  • 59,784
  • 9
  • 71
  • 110
Trixx
  • 13
  • 3
  • Are you using `$rs = mysqli_query($link,$MySQLi) or die(mysqli_connect_error());` inside a function? Plus you need to change `MySQLi` to `$MySQLi` like Edward mentioned in his answer. – Mathews Mathai Mar 18 '18 at 06:02
  • What is your `$link` code? – Edward Mar 18 '18 at 06:13
  • yes, I'm using $rs = mysqli_query($link,$MySQLi) or die(mysqli_connect_error()); inside a function – Trixx Mar 18 '18 at 06:15

2 Answers2

0

On this line: $rs = mysqli_query($link,MySQLi) or die(mysqli_connect_error());, your variable MySQLi needs the variable sign $.

So change that to this:

$rs = mysqli_query($link,$MySQLi) or die(mysqli_connect_error());
Edward
  • 2,291
  • 2
  • 19
  • 33
  • 1
    What you pointed out is important but I don't think that's the issue here though. This seems to be an issue with the scope of variable `$link`. OP is probably trying to use `$link` inside a function. `Warning is about parameter 1 and not 2`. – Mathews Mathai Mar 18 '18 at 05:59
  • The error is still coming up after I changed line 26 as suggested by Edward – Trixx Mar 18 '18 at 06:12
  • $link=mysqli_connect($dbserver,$dbusername,$dbpassword) or die(mysqli_connect_error()); – Trixx Mar 18 '18 at 06:25
0

If you have defined, $link=mysqli_connect() outside the function, you need to pass it as a parameter to the function or declare it global inside the function.

1. Passing as parameter

yourFunction($link); //function call, passing the variable to the function

function yourFunction($link) //you can use any name for parameter
{ //now it can be accessed here
}

2. Declaring as Global inside the function

function yourFunction()
    { 
    global $link;
//now it can be accessed here
    }

By using the keyword global, you are specifying that you want to use the $link which you have defined in global scope.

You may have to do the same with $MySQLi and other variables if you have defined them outside the function.

Mathews Mathai
  • 1,707
  • 13
  • 31
  • It worked after passing the parameter to the function – Trixx Mar 18 '18 at 06:29
  • Great. Happy Coding :D Also, to learn more about variable scope in php, http://php.net/manual/en/language.variables.scope.php – Mathews Mathai Mar 18 '18 at 06:31
  • That's great. Thank you – Trixx Mar 18 '18 at 06:32
  • If this answer helped you, please indicate so by clicking the `check/tick` mark on the left. And you are welcome :D – Mathews Mathai Mar 18 '18 at 06:48
  • Ideally, you are supposed to start a new question but I think I will try to explain it here instead. You are getting a `deprecated warning` because all these functions with `mysql` extension like `mysql_connect()`, `mysql_pconnect()` are not supported anymore. They have been officially discontinued. You should use `mysqli_` or `PDO_MySQL` instead. For more details on deprecated warning, https://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli – Mathews Mathai Mar 18 '18 at 08:03
  • 1
    noted. I'm unable to create a new question at this time. Thanks for your help – Trixx Mar 18 '18 at 09:37