Question:
Proper code format (PHP and mySQL) for 10 points!?
Chris Perk
2011-12-20 14:16:25 UTC
Part 1 -Hi, I have 8 if statements nested inside one another because I need one action (submit) to do a lot of things in my database. Is there a way that I'm supposed to do it that's cleaner than nesting 8 if statements in one another with 32 database queries in them?

// reference up first tier
$query = mysql_query("SELECT reference FROM members WHERE username='$username'");
$numrows9 = mysql_num_rows($query);
if ($numrows9 == 1){
$sqlcommand ="SELECT * FROM members WHERE username = '$username' LIMIT 1";
$query = mysql_query($sqlcommand) or die (mysql_error());
while ($row = mysql_fetch_array($query)) {
$ref9 = $row ["ref"];
}
$sqlcommand ="SELECT * FROM members WHERE username = '$ref9' LIMIT 1";
$query = mysql_query($sqlcommand) or die (mysql_error());
while ($row = mysql_fetch_array($query)) {
$money9 = $row["money"];
$income9 = 5 * 0.2;
$newmoney9 = $money9 + $income9;
}
mysql_query("UPDATE members SET money='$newmoney9', tier1= '$income9' WHERE username = '$ref9' LIMIT 1");

//reference up second tier
...

Part 2 - I can separated the nested if statements into 8 separate files on my database and simply have one master script that requests them. Is this faster for the server than simply accessing one large file?

BEST ANSWER GETS 10 POINTS!
Three answers:
anonymous
2011-12-20 14:31:24 UTC
you have it proper, although there is other ways to do it. it really comes down to what works best for you. like you could also use switch:



switch ($i) {

case 0:

echo "i equals 0";

break;

case 1:

echo "i equals 1";

break;

case 2:

echo "i equals 2";

break;

}



and yes you can separate into different files. this will help speed it up a little bit, although probably not noticeably.
Liz
2011-12-20 14:42:52 UTC
Well... maybe?

Its hard to say. You should try to minimize the calls to the database, and preserve data if you have it.

What is reference and why does it need to be called separately from the rest of the database until we know if($numrows9 == 1) for instance?

Can you just pull * first, and not process? or is that to see if they exist before moving on?..



...

penny falling... still falling...

...

wait.. You are building a MLM system aren't you? :)

If you are.. I would recommend keeping track of everybody's chain of reference in a single text field upon signup, as long as you have a non-infinite reference chain. That way even if you DO have to use all this mass nesting, you only have to do it once.
burchette
2016-11-12 01:22:22 UTC
you on the on the spot at the on the spot are not checking to make constructive if the assistance is already there. i could want to point you spot if the $about is already there. no undergo in recommendations even if that's then project a caution. and modify stripslashes($about); to mysql_real_escape_string($about)...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...