PhdJB Posted June 6, 2007 Share Posted June 6, 2007 Im new at PHP, and i need someone to check my code for me: <? // connect to database // $dbh = mysqli_connect($host, $username, $password, $db); // turn off auto-commit mysqli_autocommit($dbh, FALSE); // end connection // // run query 1 $result = mysqli_query($dbh, $query1); if ($result !== TRUE) { mysqli_rollback($dbh); // if error, roll back transaction } // run query 2 $result = mysqli_query($dbh, $query2); if ($result !== TRUE) { mysqli_rollback($dbh); // if error, roll back transaction } // end query // echo "Submission Completed!"; // close transaction // mysqli_close(); ?> and than i have a bunch of variables Quote Link to comment https://forums.phpfreaks.com/topic/54447-solved-i-need-someone-to-check-my-code/ Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 Wow dude...wow you got patience typing all that out. You know you can compress this into a lot less writing out and make it dynamic using a for loop www.php.net/for Just wow. Quote Link to comment https://forums.phpfreaks.com/topic/54447-solved-i-need-someone-to-check-my-code/#findComment-269290 Share on other sites More sharing options...
obsidian Posted June 6, 2007 Share Posted June 6, 2007 Well, my first recommendation would be to define your queries within an array and simply loop over the array instead of trying to write each query individually: <?php $queries = array( '-- do some SQL stuff --', '-- do some SQL stuff --', '-- do some SQL stuff --' ); foreach ($queries as $q) { if (mysqli_query($dbh, $q) === FALSE) { // Error occurred } } ?> I really don't think that rollback is what you're after, though, because if there is an error, the transaction was not completed. mysqli_rollback() rolls back the current transaction, but if it was not completed, there's no need to roll it back. Quote Link to comment https://forums.phpfreaks.com/topic/54447-solved-i-need-someone-to-check-my-code/#findComment-269295 Share on other sites More sharing options...
Psycho Posted June 6, 2007 Share Posted June 6, 2007 Am I missing something? The sequence you have the code in will not work. You first try to run a series of queries beofre you connect to the database and before you define your queries. Also, if all your variables really are in the form: Location01, Location02, Location03, etc. and all your queries are the same (for 1 - 17) I would really suggest putting everything into arrays and then you could do everything in a few lines of code by utilizing a loop: for ($i=1; $i<17; $i++) { $query= "INSERT INTO $table[$i](Location[$i], Model[$i], Lens[$i], FPS[$i], Quality[$i], RRes[$i], REC[$i], Power[$i]) VALUES ("$Location[$i]", "$Model[$i]", "$Lens[$i]", "$FPS[$i]", "$Quality[$i]", "$RRes[$i]", "$REC[$i]", "$Power[$i]")"; $result = mysqli_query($dbh, $query23); if ($result !== TRUE) { mysqli_rollback($dbh); // if error, roll back transaction } } Quote Link to comment https://forums.phpfreaks.com/topic/54447-solved-i-need-someone-to-check-my-code/#findComment-269297 Share on other sites More sharing options...
PhdJB Posted June 6, 2007 Author Share Posted June 6, 2007 I've redone the code like you guys have suggested but everytime i try to open the file in my browser it says: call to undefined function: mysqli_query() Quote Link to comment https://forums.phpfreaks.com/topic/54447-solved-i-need-someone-to-check-my-code/#findComment-269517 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 mysqli is for php5 only I believe, you must not have that functionality. http://us.php.net/manual/en/function.mysqli-query.php Quote Link to comment https://forums.phpfreaks.com/topic/54447-solved-i-need-someone-to-check-my-code/#findComment-269521 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.