1

I'm getting the above error in regards to my statement below, tried multiple fixes from other answers in stack but continue to get the same error :(

this is part of a php code in a user registration form, would like their info to insert in the table

if($conn->query( "INSERT INTO users (FIRST_NAME, LAST_NAME, USERNAME, PASSWORD)
VALUES ('$fname', '$lname', '$uname', '$pword')")==TRUE){
 echo 'Inserted';
 }else{
 echo 'Not Inserted';
 }

i have this other code in a separate file for the $conn

function dbConnect(){
 $servername = "x";
 $database = "activity2"; 
 $username  = "root"; 
 $password = "root";
// Create connection
 $conn = new 
mysqli($servername, $username, $password, $database); 
// Check connection 
if  ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); 
} //echo "Connection successful"; //make variable global to access in other 
functions global $conn; return $conn;} 
7
  • the error is with the creation of $conn Commented Mar 10, 2019 at 23:40
  • i have this code in a separate function file: function dbConnect(){ $servername = "x"; $database = "activity2"; $username = "root"; $password = "root"; // Create connection $conn = new mysqli($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //echo "Connection successful"; //make variable global to access in other functions global $conn; return $conn;} Commented Mar 10, 2019 at 23:40
  • then in the file with the code above, I have global $conn at the top? Commented Mar 10, 2019 at 23:41
  • you need $conn=dbConnect(); before the above Commented Mar 10, 2019 at 23:43
  • Do you call dbConnect() at any point? Commented Mar 10, 2019 at 23:44

1 Answer 1

1

First thing to do is to create the class that will return a connection:

<?php
//filename: dbConnect.php
class dbConnect
{
	
		public function connect() {
			 global $conn;
				
			 $servername = "localhost";
			 $username  = "root"; 
			 $password = "";
			 $database = "test";  
			// Create connection
			 $conn = new mysqli($servername, $username, $password, $database); 
			// Check connection 
			if  ($conn->connect_error) { 
				die("Connection failed: " . $conn->connect_error); 
			} //echo "Connection successful"; //make variable global to access in other 
			
			return $conn;			
		}
}

?>

Now you can execute your sql command in another file:

<?php
require 'dbConnect.php';

	$db = new dbConnect();
	$conn = $db->connect();

	$nome = "Anailton";
	$email = "[email protected]";
	if($conn->query( "INSERT INTO clientes (NOME, EMAIL) VALUES ('$nome', '$email')")==TRUE){
		echo 'Inserted';
	}else{
		echo 'Not Inserted';
	}

?>

Sign up to request clarification or add additional context in comments.

5 Comments

i get the error that $db cannot be resolved to a type
when i run it, i get Fatal error: Class 'dbConnect' not found in C:\xampp\htdocs\activity3\registerHandler.php on line 55
It is possible to copy the content of the answer and save it as a file dbConnect.php inside the folder C:\xampp\htdocs\activity3 With this I believe that your registerHandler.php will find the dbConnect class. Inside the registerHandler.php (before line 55), use the line require 'dbConnect.php';
Hi Junior, sorry but weird. It works for my registerHandler file, but on my loginHandler file, when i require dbConnect.php it gives me an error that i "cannot redeclare class dbConnect"
It's possible to use require_once instead of require.Use it in every PHP file you need to establish a connection to the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.