Warning: mysqli_query() expects at least 2 parameters

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts:282
Location:Holland Rotterdam

Warning: mysqli_query() expects at least 2 parameters

I did turn a php script to mysqli but getting two errors:

Code: Select all

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/u790012824/public_html/Message/comments/db.php on line 26
Code:

Code: Select all

$db_hostname = "mysql.hostinger.nl";  //usually "localhost be default"
$db_username = "u790012824_gebru";  //your user name
$db_pass = "123456";  //the password for your user
$db_name = "u790012824_datab";  //the name of the database

/*MYSQL DATABASE CONNECTION/ TRACKING FUNCTIONS
--------------------------------------*/
// connect to database
$dbh = mysqli_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysqli_select_db ($db_name);
?>
line 26 is

Code: Select all

mysqli_select_db ($db_name);
my second error is the same

Code: Select all

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/u790012824/public_html/Message/comments/inc_rate.php on line 178
Code:

Code: Select all

function getComments($tutid){
//fetch all comments from database where the tutorial number is the one you are asking for
	$results = mysqli_query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC") or die(mysql_error());
//find the number of comments
	$commentNum = mysqli_num_rows($results);
line 178 is

Code: Select all

	$results = mysqli_query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC") or die(mysql_error());

Admin Posts:805
Hy,
Replace the "mysqli_" prefix from the function name with the object that store the connection.
For example:

Code: Select all

// connect to database
$dbh = mysqli_connect($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: '. mysqli_connect_error());
$dbh->select_db($db_name);
Or, add the object with mysqli as first parameter to the "mysqli_...()" function.

Code: Select all

// connect to database
$dbh = mysqli_connect($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: '. mysqli_connect_error());
mysqli_select_db($dbh, $db_name);
- The same for mysqli_query() and other "mysqli_.." functions.

If the "mysqli_" is into a function, you must add in that function scope (with the GLOBAL keyword) the object with mysqli connection.

Code: Select all

function getComments($tutid){
  GLOBAL $dbh;  //to can use $dbh in this function
//fetch all comments from database where the tutorial number is the one you are asking for
   $results = $dbh->query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC");
//find the number of comments
  $commentNum = $results->num_rows;
  //...
}