AJinNYC Posted December 10, 2013 Share Posted December 10, 2013 (edited) 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); } } Edited December 10, 2013 by AJinNYC Quote Link to comment https://forums.phpfreaks.com/topic/284668-prepared-statement-in-if-conditional-not-working-as-expected/ Share on other sites More sharing options...
Ch0cu3r Posted December 10, 2013 Share Posted December 10, 2013 (edited) Your conn function is not returning the mysqli connection. Change it to //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); } return $dbConnection; // return the connection } Edited December 10, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284668-prepared-statement-in-if-conditional-not-working-as-expected/#findComment-1461938 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.