Pergunta de entrevista da empresa TrustCommerce

Review the following code block and suggest improvements when necessary. $_con = false; // Assume this code attempts to return a DB handle to master DB function getMasterConnection() { ... } // Assume this code attempts to return a DB handle to slave DB function getSlaveConnection() { ... } // Function to get a DB connection handle function get_con($master = false) { global $_con; if ( !empty($_con) ) return $_con; if ($master == true) $_con = getMasterConnection(); else $_con = getSlaveConnection(); return $_con; } // Table 'users' definition // - user_id (SERIAL/LONG) // - login_id (TEXT) // - creation_ts (Unix timestamp) // - locked (BOOLEAN) // - locked_ts (Unix timestamp) // Function attempts to read and return data stored for the userid in an array function retrieveData($loginID) { $userData = array(); $dbconn = get_con(); $result = pg_query($dbconn, "SELECT * FROM users WHERE login_id = '$loginID'"); $userData = pg_fetch_all($result,0); return $userData; } // Table 'orders' definition // - order_id (SERIAL) // - user_id (LONG) // - order_details (TEXT) // Function attempts to insert order information function storeOrder($userid, $orderDetails) { $dbconn = get_con(true); $result = pg_query_params($dbconn, "INSERT INTO orders(userid, order_details) VALUES ($1, $2)", array($userid, $orderDetails)); return false; } // Main code execution begins here // This code helps store new order details under 'orders' table if ( !empty($_POST['login_id']) ) { $userData = retrieveData($_POST['login_id']); if ($_POST['action'] == 'create') { if ( storeOrder($userData[0]['user_id']) ) echo "Order creation successful"; else die("Failed to store order information due to run time exceptions."); } }