URL and Link repository

Thursday, June 20, 2013

PHP/MySQL ending in JSON that Needs Optimization

I am testing this code versus "Insert ... where Select ..." and testing MySQL return codes.

function sql_safe($s) { if (get_magic_quotes_gpc()) $s = stripslashes($s); return mysql_real_escape_string($s); }

$flag = "0"; // set flag to good at the start of program
// The user is coming in as the integer id
$user = trim(sql_safe($_POST["User"]));

if ($user == '') $user = 0;
// The member is coming as the text input
$member = trim(sql_safe($_POST["Member"]));
// could do a sub select query, but broke out for clarity.

if ($member != '') {
$sel=mysql_query("SELECT `idmembers` from `a5035822_mya55`.`members` where `handle` = '$member';") or trigger_error ('Error on select handle'.mysql_error(),E_USER_ERROR);

$r1 = mysql_fetch_array($sel);
//echo "r1 is".$r1['idmembers'];
// make sure not trying to delete the owner userid.
// will perform check on the device first with next version of app

if (($r1['idmembers'] != '') && ($r1['idmembers'] != $user)) {
// No composite key on SETS table so no checking for MySQL ret code 1062.
// Separate check to see if row exists first, then delete if it does.
$memberid = $r1['idmembers'];
$checkSet = MySQL_query("select `member` from sets where owner = $user and member = $memberid;") or trigger_error('Failed on check'. mysql_error(), E_USER_ERROR);
$r2 = MySQL_fetch_array($checkSet);

if ($r2['member'] != '') { $del = mysql_query("delete from sets where owner = $user and member = $memberid;") or trigger_error('Failed on delete'. mysql_error(), E_USER_ERROR);

if (mysql_error() == 0) {
$flag = 0;

} else {
$flag = 3; // no row to delete
}

} else { $flag = 3; }

} else {
$flag = 3;
$memberid = $user;
}

} else { $flag = 5; };
$data = array (
'Flag' => $flag,
);

print(json_encode($data));

No comments:

Post a Comment