samona Posted October 23, 2008 Share Posted October 23, 2008 I have a script that executes a query 3 times on the same page. 1.One query finds out which template to use. 2.The other query on the page is used to update the template to a different one, 3. A loop which searches to find the active template and inactivate it and activate the new one. Is it bad programming practice to have these queries all after one another on the same script? Link to comment https://forums.phpfreaks.com/topic/129691-solved-good-programming-practice/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 23, 2008 Share Posted October 23, 2008 It depends on exactly what queries are doing. It may be that what you are doing is already the best solution. You would need to provide the actual code to get a more specific answer about what could be done to optimize what is being done. Link to comment https://forums.phpfreaks.com/topic/129691-solved-good-programming-practice/#findComment-672423 Share on other sites More sharing options...
samona Posted October 23, 2008 Author Share Posted October 23, 2008 <?php require_once "inc/mysql_connect.php"; $new_template = $_POST['templates']; $connection = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: '.mysql_error()); if (!$connection) { die('Not connected : ' . mysql_error()); } if (!mysql_select_db(DB_NAME, $connection)) { die ('Could not select a database: ' . mysql_error()); } $query = 'SELECT * FROM styles_template WHERE active = 1 LIMIT 1'; if (!($result = @mysql_query($query, $connection))) { die ('Could not query the database: ' . mysql_error()); } if (($row = mysql_fetch_array($result))) { if ($row['form_name'] != $new_template) { $query = 'Update styles_template SET active=0 where active = 1'; if (!($result = @mysql_query($query, $connection))) { die ('Could not update the database: ' . mysql_error()); } } $query = "Update styles_template SET active = 1 WHERE form_name = '$new_template'"; if (!($result = @mysql_query($query, $connection))) { die ('Could not query the databases: ' . mysql_error()); } } header('Location: ./update_index.php'); ?> That's the code. Do you think it's optimized? Link to comment https://forums.phpfreaks.com/topic/129691-solved-good-programming-practice/#findComment-672437 Share on other sites More sharing options...
awpti Posted October 23, 2008 Share Posted October 23, 2008 Not much else you can do there, but avoid the usage of error suppression through the @ symbol. This makes PHP take approximately 6 times as long to process the script. I'd cite that, but I can't recall the URL offhand. @ = bad, regardless. Proper error handling will boost performance big time. Link to comment https://forums.phpfreaks.com/topic/129691-solved-good-programming-practice/#findComment-672442 Share on other sites More sharing options...
samona Posted October 23, 2008 Author Share Posted October 23, 2008 ok cool! Thanks for the tip. Link to comment https://forums.phpfreaks.com/topic/129691-solved-good-programming-practice/#findComment-672451 Share on other sites More sharing options...
sasa Posted October 23, 2008 Share Posted October 23, 2008 you can do all this job eith just one query $query = "UPDATE styles_template SET active = IF(form_name='$new_template',1,0)"; Link to comment https://forums.phpfreaks.com/topic/129691-solved-good-programming-practice/#findComment-672491 Share on other sites More sharing options...
samona Posted October 23, 2008 Author Share Posted October 23, 2008 awesome!!!!!!!!!!!! Link to comment https://forums.phpfreaks.com/topic/129691-solved-good-programming-practice/#findComment-672872 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.