0

Let's get directly to this weird confusing problem, WHAT IS WRONG WITH THIS LINE OF CODE:

<?php 
global $con = mysqli_connect("localhost","root","","myshop");
?>

The error I get from loading this is this:

Parse error: syntax error, unexpected '=', expecting ',' or ';'

2 Answers2

0

Remove the global keyword. If you need to pass the database parameter to a function, rather pass as arguments in the function. The global keyword use is bad practice .

Alternatively, you can put the database connection in a file and include it where you need it. Doing so, you'll only need to use the same variable $con. Do not use global keyword

Rotimi
  • 4,783
  • 4
  • 18
  • 27
0

You can reach your global variables this way:

<?php 
  $GLOBALS['con'] = mysqli_connect("localhost","root","","myshop");
  function a_function($query){
     mysqli_query($GLOBALS['con'],$query);
  }
?>

This is not the best practice to store your database link, but in your case, this is the solution.

Please take care with SQL Injection leaks also!

Peter
  • 748
  • 6
  • 20
  • Then it says: syntax error, unexpected ';' on line 4 LOL –  Sep 02 '17 at 17:56
  • This is only an example how to use GLOBAL variables as mysql connection :) I made a mistake, I've corrected it, check it again, but you have to work on your furter code – Peter Sep 02 '17 at 18:01
  • @ciomaejobaep: If it worked please accept my answer, thx.. – Peter Sep 02 '17 at 18:06