-1

I have a global [global $host,$user,$passwd,$dbname;] for connecting to database in my script and then I use mysqli_query to perform a simple update query. There is a connection, but I keep getting this error:

mysqli_query() expects at least 2 parameters,

What am I doing wrong with handling that global with the mysqli_query? Or is this error given when there is a problem with the query itself?

Dharman
  • 30,962
  • 25
  • 85
  • 135
mql45
  • 1

2 Answers2

1

It means that mysqli expects this:

 $result = mysqli_query($dbh, "SELECT ...");

and you're probably doing this:

 $result = mysqli_query("SELECT ...");

The mysqli library is not a direct drop-in replacement for the older mysql one (note the lack of an i).


ok. simple short pseudo-code sample:

$dbh = mysqli_connect(....) or die(mysqli_connect_error());

function do_something($blah) {
   global $dbh;
   $result = mysqli_query($dbh, "SELECT ... $blah") or die(mysqli_error($dbh));
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I see... can I assign a variable to that global that connects to the db? Thanks. – mql45 Aug 29 '11 at 02:51
  • Sure, $dbh is just the handle returned by `mysql_connect()`. You can do that in the global scope and use `mysqli_query($GLOBALS['dbh']` or use the `global` keyword. Whatever works best for you. Most people here who've drank the OOP koolaid would suggest using a singleton, but for such things a global is perfectly acceptable in your own code. – Marc B Aug 29 '11 at 02:53
  • Opp, I got a "Parse error: syntax error, unexpected T_GLOBAL" when I assigned a variable to the global that connects the db... – mql45 Aug 29 '11 at 02:58
  • `global $dbh = mysql_connect(...)` is not valid php syntax. `global $dbh; $dbh = mysql_connect(...)` would work. – Marc B Aug 29 '11 at 02:59
  • I'm a bit confused... this is how I have it now and it is shooting me errors: "$dbh = global $host,$user,$passwd,$dbname;" it doesn't shoot errors when it is solely "global $host,$user,$passwd,$dbname;" ... can you give me something to try with that in mind? I'm new to mysqli, I've always used the old one. Thanks for your help. – mql45 Aug 29 '11 at 03:01
  • You should read the man page: http://php.net/manual/en/language.variables.scope.php – Marc B Aug 29 '11 at 03:02
  • Okay, I just read through it and better understand the nature of variables in the mysqli environment.. but I still don't know how to assign that variable to the globals properly for this database. Am I missing something completely obvious? – mql45 Aug 29 '11 at 03:09
  • Unless you're in a function or in a class, every variable you create is in the global namespace. – mikesir87 Aug 29 '11 at 03:23
0

PHP Documentation: http://us2.php.net/mysqli_query

mysqli_query requires two parameters:

  1. The database link
  2. The query itself

Example:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");
mikesir87
  • 1,785
  • 1
  • 20
  • 25