Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, i am a PHP newbie but have some experience with other programming languages. I am trying to enter simple values into the database using the connection settings defined in a different file:


//DBConnect.php
PHP
$serverName="localhost";
$userName="root";
$passWord="";
$db="inventorymgmtdb";


$conn=mysqli_connect($serverName,$userName,$passWord,$db);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  
  exit();
  }


I have included this file in my entercategory.php file:

PHP
<?php
include("DBConnect.php");

function insertCategory($categoryName,$categoryDescription)
{
	global $conn;

		$insertQuery="insert into tbl_categories(CategoryName,CategoryDescription) 				values('$categoryName','$categoryDescription')";
		
		
		$retVal=mysqli_query($conn,$insertQuery);
		
		if(!$retVal)
			die('Error entering to database'.mysqli_error($conn));
		
		echo "Entered Successfully!";
			
		mysqli_close($conn);

}
?>


Now, the system is complaining that the $conn is null ("mysqli_query() expects parameter 1 to be mysqli,null is given...").

What am i doing wrong here? Can someone please explain?

EDIT: The function is called as follows:

PHP
if(isset($_POST["btnCreateCategory"]))
{
     insertCategory($_POST["txtCategoryName"],$_POST["txtCategoryDescription"]);

}


EDIT:
I have put the code of DBConnect.php directly in entercategory.php. The $conn variable is not null and working just fine. However, i am not able to understand why, when the code in placed in a separate file (DBConnect.php), the $conn variable becomes null. Can someone please assist?
Posted
Updated 19-Jul-15 2:23am
v3
Comments
W Balboos, GHB 14-Jul-15 8:36am    
First thing you should do is use your DBConnect.php directly in entercategory.php (that is, test if you connection is made). Then, assure yourself that the value of $conn is not null in DBConnect.php. Finally, my personal way of doing this would be for your DBConnection.php to contain a function which I'd call to get my connection in the body of where I wished to use it.

Unrelated:
On other thing - this isn't a text-message on a cell phone. Capitalization and spelling out words is much more polite. (for example, Please not pls).
Minghang 14-Jul-15 9:42am    
Thank you for taking your time to reply. I will try out the things that you suggested. I did not get your final reference though -the unrelated section.

1 solution

When include executed by the PHP parser the parsing starts in HTML mode (regardless of what mode the parser was until now) and switches back to the current mode when the included file has been finished...
So you HAVE to enclose your code in <?php ?> to make it truly PHP!!!
PHP
<?php
$serverName="localhost";
$userName="root";
$passWord="";
$db="inventorymgmtdb";

$conn=mysqli_connect($serverName,$userName,$passWord,$db);
 
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  
  exit();
}
?>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900