0

Here is the database.php code every time I get this error:

Notice: Undefined variable: dbConn in C:\xampp\htdocs\couriermanagement\database.php on line 15

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\couriermanagement\database.php on line 15

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\couriermanagement\database.php on line 15

<?php


// database connection config
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'courier_db';

 $dbConn = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName) or die ('MySQL 
 connect failed. ' . mysqli_error($dbConn));
//mysqli_select_db($dbConn ,$dbName) or die('Cannot select database. ' . 
  mysqli_error($dbConn));//

 function dbQuery($sql)
{
$result = mysqli_query($dbConn,$sql) or die(mysqli_error());

return $result;
}

function dbAffectedRows()
{
global $dbConn;

return mysqli_affected_rows($dbConn);
 }

 function dbFetchArray($result, $resultType = MYSQL_NUM) {
return mysqli_fetch_array($result, $resultType);
 }

 function dbFetchAssoc($result)
{
return mysqli_fetch_assoc($result);
}

 function dbFetchRow($result) 
{
return mysqli_fetch_row($result);
}

 function dbFreeResult($result)
 {
return mysqli_free_result($result);
 }

 function dbNumRows($result)
 {
return mysqli_num_rows($result);
 }

 function dbSelect($dbName)
 {
    return mysqli_select_db($dbName);
 }

 function dbInsertId()
  {
  return mysqli_insert_id();
  }
  ?>
abpatil
  • 916
  • 16
  • 20
Raj Singh
  • 1
  • 1
  • 4
  • You have to supply two parameters , one for query and another for connection in mysqli_query($query,$conn) – Suraj Khanal Oct 14 '17 at 06:54
  • Either way you can call the query $result = mysqli_query($dbConn, $sql) Add connection to query or $result = $dbConn->query($sql); [Check the doc](http://php.net/manual/en/mysqli.query.php) – Sivaraj S Oct 14 '17 at 07:01
  • $result = $dbConn->query($sql); used this one but still getting error Notice: Undefined variable: dbConn in C:\xampp\htdocs\couriermanagement\database.php on line 15 Fatal error: Uncaught Error: Call to a member function mysqli_query() on null in C:\xampp\htdocs\couriermanagement\database.php:15 Stack trace: #0 C:\xampp\htdocs\couriermanagement\login.php(13): dbQuery('SELECT DISTINCT...') #1 {main} thrown in C:\xampp\htdocs\couriermanagement\database.php on line 15 – Raj Singh Oct 14 '17 at 07:16

2 Answers2

1

The "link" is missing i think you upgrade from mysql to mysqli

it should look like this:

$dbConn = mysqli_connect("localhost", "my_user", "my_password", "world");
mysqli_query($dbConn, "SELECT * FROM test");

read:

http://php.net/manual/en/mysqli.query.php

under Procedural style

Peter Haberkorn
  • 259
  • 1
  • 9
  • i have used $dbconn still getting error undefined variable – Raj Singh Oct 14 '17 at 07:08
  • 1
    function dbQuery($sql) { global $dbconn; $result = mysqli_query($dbConn,$sql); } because try to access the variable which not defined in the function – Sivaraj S Oct 14 '17 at 07:15
0

You should add

$con=mysqli_connect("hostmane","userename","password","db_name"); 

before the line

$result = mysqli_query($dbConn,$sql) or die(mysqli_error());
Robert
  • 5,278
  • 43
  • 65
  • 115
salim
  • 1
  • You have an error. `mysqli_error()` needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 30 '19 at 22:00