Page 1 of 1

Array Function issue

Posted: Fri Jul 22, 2011 5:13 am
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;
	}
}

Re: Array Function issue

Posted: Fri Jul 22, 2011 7:07 am
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

Re: Array Function issue

Posted: Fri Jul 22, 2011 8:52 am
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

Re: Array Function issue

Posted: Fri Jul 22, 2011 12:10 pm
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.

Re: Array Function issue

Posted: Fri Jul 22, 2011 12:55 pm
by pickle
I think the easiest solution will be to just hard code the query & fields. There are only 3 of them.