Array Function issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sean Harding
Forum Newbie
Posts: 2
Joined: Fri Jul 22, 2011 5:07 am

Array Function issue

Post by Sean Harding »

I'm looking for a better way to convert my array into a MySQL string.
My array will be

Code: Select all

$my_array = name => full_name,
                   email => email@email.com,
                   birthdate => my/bi/thday
and here is my function

Code: Select all

function db_input($user_details) {
	$string_quer = 'INSERT INTO emails (';
	foreach($user_details as $user_key => $user_val) {
		$string_quer .= ''. $user_key .', ';
	}
	substr($string_quer, 0, -2); //remove extra ', '
	$string_quer .= ') VALUES (';
	foreach($user_details as $user_key => $user_val) {
		$string_quer .= ''. $user_val .', ';
	}
	substr($string_quer, 0, -2); //remove extra ', '
	$string_quer .= ')';
	
	$mysql_result = mysql_query($string_quer);
	if ($mysql_result) {
		return TRUE;
	} else {
		return FALSE;
	}
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Array Function issue

Post by social_experiment »

Code: Select all

<?php
 function db_input($user_details) 
 {
  if (is_array($user_details))
  {
    foreach($user_details as $key => $value)
    {
      $fields = array($key);
      $values = array($value);
    }
    
      $fieldString = implode(', ', $fields);
      $valueString = implode(', ', $values);
      
      $string_quer = "INSERT INTO emails (" $fieldString ") VALUES (" $valueString ")";
      $mysql_result = mysql_query($string_quer);

     if ($mysql_result)
     {
      return TRUE;
     }
     else
     {
       return FALSE;
     }
  }  
 } 
?>
You could try something like this, it's not been tested but it will simplify things a bit
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Sean Harding
Forum Newbie
Posts: 2
Joined: Fri Jul 22, 2011 5:07 am

Re: Array Function issue

Post by Sean Harding »

Got some working code:

Code: Select all

function db_input($user_details) {
	if (is_array($user_details)) {
		$fields = array();
		$values = array();
		foreach($user_details as $key => $value) {
			$fields[] = $key;
			$values[] = $value;
		}
		$fieldString = implode(", ", $fields);
		$valueString = "'";
		$valueString .= implode("', '", $values);
		substr($valueString, 0, -3);
		substr($fieldString, 0, -2);
		
		$string_quer = "INSERT INTO emails ($fieldString) VALUES ($valueString')";
		$mysql_result = mysql_query($string_quer);

		if ($mysql_result) {
			return $string_quer;
		} else {
			return $string_quer;
		}
	}  
} 
I dont know which way is better or if there is a much easier way without having to substr
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Array Function issue

Post by social_experiment »

Code: Select all

<?php
function db_input($user_details) 
 {	
  if (is_array($user_details))
  {		
    foreach($user_details as $key => $value){
	  $fields[] = $key;	  
	  $values[] = "'" . $value . "'";
	}
     
    $fieldString = implode(', ', $fields);
    $valueString = implode(', ', $values);
      
    $string_quer = "INSERT INTO emails (". $fieldString .") VALUES (". $valueString .")";
    $mysql_result = mysql_query($string_quer);	  
		
     if ($mysql_result)
     {
      return TRUE;
     }
     else
     {
      return FALSE;
     }	 
  }  
 }
?>
Nice, after i posted the code i reviewed it and made some modifications. The only issue is still have with it is adding some sort of checking to the data before it is written to the database.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Array Function issue

Post by pickle »

I think the easiest solution will be to just hard code the query & fields. There are only 3 of them.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply