Why Mysqli Database Connection not Working Inside a Method or a Function in PHP
Sometimes, I have faced some issues when calling to function inside PHP script, The database connection not working inside the method/function, In case , it will work in MySQL but In the case of Mysqli Connection, we need to pass the connection object as a parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "dynamic_field"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } |
Suppose I have a function below example you can understand easily.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php function CheckEmail($link,$emailId){ //Do Some Code } // Calling the function CheckEmail($conn, $email); ?> |