I have a function to connect with a database. It seems to work when I seed the correct password -- it prints out the line that says
"Connection worked! <br>"
But In my file where I called the function - I wanted to test for $conn so I knew for sure that it was returned by the function. Even though it is showing me IN FUNCTION that the connection worked, when I try to test for it IN FILE it throws an undefined variable error. Why is this not working? Can you NOT test for the database connection that is returned this way?
Here is the connecting function code that is called in the file:
function db_connect($pword)
{
$conn = new mysqli("localhost", "omgma_omgmauser", "$pword", "omgma_members");
if (mysqli_connect_errno() > 0)
{
$errmsg = "Failure - Could not connect to database with db_connect.<br />mysqli_connect_error message: ".mysqli_connect_error();
throw (new Exception($errmsg));
}
else
{
echo "Connection worked! <br>";
return $conn;
}
}
And here is the portion of the file where I call the function and try to test to see if $conn is returned:
$pword = "xxxx213";
db_connect($pword);
if (!$conn) echo "Object failed at line: " .__LINE__ ;
else if ($conn) echo "Object succeeded at line: " .__LINE__ ;
The messages to my screen when I use the correct password are:
Connection worked!
Encountered error: 8 in /home/omgma/public_html/Fresh_start/member.php5, line 67: Undefined variable: conn
Object failed at line: 67
Any ideas on what I have screwed up? 😕