Jump to content

[SOLVED] Good Programming Practice?


samona

Recommended Posts

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.