
Originally Posted by
keyboard1333
Something really weird is going on o.O
PHP Code:
<?php
function sqli_connect($username, $password) {
$sqli_connection = mysqli_connect('localhost', $username, $password);
if(!$sqli_connection) {
return 'false'; //Could not connect, return false.
} else {
return 'true'; //Connection fine, return true.
}
}
?>
(for the moment I pulled out the globals bit)
If the connection is valid, it returns true.
If I put in an empty password, no matter the username, I get a true return though.. no clue why. But it's not a valid connection because I ran an sql command and nothing happened.
If I put in an invalid username and a password that isn't empty, or I put in the right username and a password that is wrong (and not empty) I get a php error, it doesn't return false..
So two questions -
1. Any clue why this is happening?
2. How can I stop the php error for just this event?
1. Is there a reason you're returning true/false as strings? 
2. What is the php error you're getting?

Originally Posted by
keyboard1333
Edit: EDIT -
Actually, just figured out how to stop the php error.. just used an @ on the mysql connect.
ugh, please don't do that.
There are some valid uses for the error suppression operator ( @
), but this isn't one of them.
This is like putting a fancy rug over the spot where the dog barfed so you don't have to clean it up.
NOW, to your original question:

Originally Posted by
keyboard1333
Say I've got this function that connects to the mysql database -
PHP Code:
function sqli_connect($username, $password) {
$connection = mysqli_connect("localhost", $username, $password);
//Test connection to database
if(mysqli_connect_errno()) {
echo "Installation error: Could not connect to database.";
exit;
}
}
Then I call it from here -
Code:
<?php
sqli_connect('****', '****');
//Create the databases
if(mysqli_query($connection,"CREATE DATABASE my_db")) { //Executes mysqli query and tests for a return of true at the same time
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($con);
}
?>
How can I give it the connection variable from the function?
Thanks all

keebs
Two methods pop to mind. #1 (preferred): return
the connection from the function.
PHP Code:
<?php
function sqli_connect($username, $password) {
$connection = mysqli_connect("localhost", $username, $password);
//Test connection to database
if(mysqli_connect_errno()) {
return false; // connection failed
}
return $connection; // connection successful
}
// $link will be a mysqli connection on success; or FALSE on failure.
$link = sqli_connect( 'username','password' );
#2: If the function *must* return true/false for some reason, you can pass a var for the connection by reference.
PHP Code:
<?php
function sqli_connect( $username,$password,$link=null ) {
$link = mysqli_connect("localhost", $username, $password);
//Test connection to database
if(mysqli_connect_errno()) {
return false; // connection failed
}
return true; // connection successful
}
// $link will be a mysqli connection on success; or FALSE on failure.
// $success will be TRUE on success; or FALSE on failure.
$success = sqli_connect( 'username','password',$link );
I don't recommend using $_GLOBALS, for various reasons.
Bookmarks