mardi 24 mai 2016

Which method is optimized (compare with DB or use only If statement in PHP)

I have coded one idea in PHP using only If statement as below. This code compares one string which we had got from one webpage to predefined set of strings using only If statement. And then if any If statement is to be true then the respective value is assigned to some variable.

<?php
.....
//Here goes for loop to get so many values of $geta
.....
//below if statements are inside above for loop

if($geta == "some string"){$a="some1";$b="some2";}
if($geta == "some string2"){$a="some3";$b="some4";}
if($geta == "some string3"){$a="some5";$b="some6";}
if($geta == "some string4"){$a="some7";$b="some8";}
......

?>

Now if we could use MySQL database to get value of $a and $b instead of using If statements. Like "open db, search table where geta = $geta and fetch respective $a and $b". Take a look at below example:

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT a, b, FROM data_tbl WHERE geta=$geta';

mysql_select_db('TUTORIALS');
$retval = mysql_query( $sql, $conn );
.....

So which is optimized method for above stack of code.

Aucun commentaire:

Enregistrer un commentaire