Jump to content

Help with Function


nappyhead
 Share

Recommended Posts

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;
}
Link to comment
Share on other sites

...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?

Link to comment
Share on other sites

...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".   :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Questions:
1. Does the function have any errors? yes
2. What is the purpose of the function? to be executed
3. What can be done to improve it? lots of stuff
4. Why is isset() used instead of a simple conditional used above it? demonstration of power to oppress
5. What assumptions can you make about the database structure? it has columns and stuff. Also it has those other things.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.