Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Prepared statement in if conditional not working as expected

$
0
0
EDIT: Nevermind, my issue was that the function wasn't returning anything. The DB connection wasn't being returned out of the function.
 
I can't for the life of me see why this prepared statement inside of the if conditional is not working:

$dbConn=conn($host, $user, $password, $dbname, $_SESSION['websitename']);
/* set autocommit to off */
mysqli_autocommit($dbConn, false);
$executed_ok=null;
if($prepareuserdetails=mysqli_prepare($dbConn, "INSERT INTO `userdetails` (`fname`, `email`) VALUES (?,?)")){
die("Test");
if($binduserdetails=mysqli_stmt_bind_param($prepareuserdetails, 'ss', $regfname, $regemail)){
if($executeuserdetails=mysqli_stmt_execute($binduserdetails)){
$executed_ok1=true;
$lastID=mysqli_insert_id($dbConn);
var_dump($lastID);
}
else{
$executed_ok1=false;
}
}
else{
$dberrorshow=true;
}*/
}
else{
$dberrorshow=true;
}
if($prepareuser=mysqli_prepare($dbConn, "INSERT INTO `users` (`username`, `pass`, `id`) VALUES (?,?,?)")){
if($binduser=mysqli_stmt_bind_param($prepareuser, 'ssi', $regusername, $regpass, $lastID)){
if($executeuser=mysqli_stmt_execute($binduser)){
$executed_ok2=true;
}
else{
$executed_ok2=false;
}
}
else{
$dberrorshow=true;
}
}
else{
$dberrorshow=true;
}
//If there is a DB error, show message.
if($dberrorshow===true){
die('Database Error: We couldn\'t connect to the database. Please return to the <a href="//'.getDomain($_SESSION['websitename']).'">homepage</a>.');
}
else{
//If all queries executed okay, commit them; if not, rollback.
if($executed_ok1===true && $executed_ok2===true){mysqli_commit($dbConn);}
elseif($executed_ok1===false || $executed_ok2===false){mysqli_rollback($dbConn);}
}

I placed the die("Test") in there to see where it was failing, and it's failing at the prepare point... it immediately fails and heads to the error message. The query runs fine in phpMyAdmin.


For reference:

conndetails.php just has the connection details, dbname, username, password, and host.

DBFuncs.php

//Database Connection 
    function conn($host, $user, $password, $dbname, $websiteName){ 
    //MySQLi Connection 
    $dbConnection=mysqli_connect($host, $user, $password, $dbname); 

        /* check connection */ 
        if(!$dbConnection){ 
            if($_SESSION['siteisactive']=="no"){ 
            $errormsg='('.mysqli_connect_errno().') '.mysqli_connect_error(); 
            } 
            else{ 
            $domainname=getDomain($websiteName); 
            $errormsg='We couldn\'t connect to the database. Please return to the <a href="//'.$domainname.'">homepage</a>.'; 
            } 
        die('Connection Error: '.$errormsg); 
        } 
    }  

Viewing all articles
Browse latest Browse all 13200

Trending Articles