nappyhead Posted July 11, 2013 Share Posted July 11, 2013 I was given this function question for an interview. I don't think I'm interested in the job and therefore will not submit this, but I am curious how to answer this. Once I start something like this, I can't just quit without knowing the answer. Questions:1. Does the function have any errors?2. What is the purpose of the function?3. What can be done to improve it?4. Why is isset() used instead of a simple conditional used above it?5. What assumptions can you make about the database structure? function auto_query( $table, $vars ) { $_result = $this->query( "EXPLAIN $_table_name" ); $_fields = array(); // Empty array assigned to variable while( $_row = $_result->fetchRow() ) { array_push( $_fields, $_row['Field'] ); // Push one or more elements onto the end of array } if( $vars[$table . 'id'] ) { $_query = "UPDATE $_table SET "; } else { $_query = "INSERT INTO $_table SET " . $_table . '_date_created = NOW(), '; } $_query_params = array(); // Empty array assigned to variable foreach( $_fields as $_field ) { if( isset( $_vars[$_field] ) ) { $_query_params[] = "$_field = " . $this->dbh->quoteSmart( $_vars[$_field] ); } } $_query .= implode( ',', $_query_params ); // Join array elements with a string if( $_vars[$_table . '_id'] ) { $_query .= " WHERE {$_table}_id = " . $this->dbh->quoteSmart($_vars[$_table . '_id']); } return $_query; } Quote Link to comment https://forums.phpfreaks.com/topic/280085-help-with-function/ Share on other sites More sharing options...
KevinM1 Posted July 11, 2013 Share Posted July 11, 2013 ...why are you asking us your interview question? Not to be a dick, but these are questions that you should actually investigate yourself before asking others. Have you tried running the code? Do you get any errors or warnings? Can you trace what it's actually doing with the database? Quote Link to comment https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440390 Share on other sites More sharing options...
Maq Posted July 11, 2013 Share Posted July 11, 2013 Post your answers and we'll critique them, otherwise, you won't get any positive replies. Quote Link to comment https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440393 Share on other sites More sharing options...
AbraCadaver Posted July 11, 2013 Share Posted July 11, 2013 ...why are you asking us your interview question? Not to be a dick, but these are questions that you should actually investigate yourself before asking others. Have you tried running the code? Do you get any errors or warnings? Can you trace what it's actually doing with the database? Haha! You're not being a dick. I thought the exact same things, except I questioned the " I don't think I'm interested in the job and therefore will not submit this". Quote Link to comment https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440395 Share on other sites More sharing options...
web_craftsman Posted July 12, 2013 Share Posted July 12, 2013 1. $this exists only inside object methods. But you have just some function. quoteSmart ??? I know about only a quote() in PDO not $_vars but $vars. $table and $_table. $vars[$table . 'id'] <-----> $_vars[$_table . '_id'] 2. it returns the query for saving some data in database table 3. use Yii's Active record instead. :-) 4. maybe because we have no ELSE logic here Quote Link to comment https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440420 Share on other sites More sharing options...
.josh Posted July 12, 2013 Share Posted July 12, 2013 Questions:1. Does the function have any errors? yes2. What is the purpose of the function? to be executed3. What can be done to improve it? lots of stuff4. Why is isset() used instead of a simple conditional used above it? demonstration of power to oppress5. What assumptions can you make about the database structure? it has columns and stuff. Also it has those other things. Quote Link to comment https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.