Page 1 of 1

Learning Alone. Help Please!

Posted: Tue Sep 04, 2007 10:18 pm
by wikkideclipse
I've been trying to write a function that inserts data to a mysql db. I want it completely automated so it can be used with multiple db's and even use it on other sites i design. I've got it to grab the fieldnames from the table and format them for sql syntax, when echoed they display properly to the page. however when i change the echo to a return it only returns the index fieldname. Same thing with the values function the code is as follows:

PLEASE HELP!!!

Code: Select all

//--Function To Insert Data to a MySql Database
function Insert2_Db($iPick_Db, $table, $value_arr){
	//--Gets Field Names For SQL Query
	function getFields($iPick_Db, $table){
		$sql="SELECT * FROM `".$table."`";
		$result = mysql_query($sql) or die(mysql_error());
		$rowcount=mysql_num_rows($result);
		$y=mysql_num_fields($result);
		for($x=0;$x<$y;$x++){
		$fields =  array("`".mysql_field_name($result, $x)."`,");
		//CONTINUE HERE
		}
		return $return;
	}
	//--Formats The Values for SQL Syntax
	function cleanValues($value_arr){
		$y = count($value_arr);
		ob_start();
		for($x=0;$x<$y;$x++){
			return "`".$value_arr[$x]."`,";
		$values = ob_end_clean();
		}
		return $values;
	}
	//--Executes
	$fields=getFields($iPick_Db, $table);
	$values=cleanValues($value_arr);
	$sql="INSERT INTO `".$iPick_Db."`,`".$table."` (".$fields.")
	VALUES (
	NULL , ".$values.");";
	$insert = mysql_query($sql);
	if(!insert){
		die("Could Not <u>Insert</u> Information");
	}else{echo $sql;}
}

Re: Learning Alone. Help Please!

Posted: Tue Sep 04, 2007 11:55 pm
by Christopher
I would not recommend nesting functions unless you have a really good reason to do so. You should verify that your functions actually return something valid and that you want. The function cleanValues() is too clever for its own good. Simple concatenation would be fine. And mysql_error() may help you find out if the queries are even working.

Posted: Wed Sep 05, 2007 1:11 pm
by califdon
You are to be congratulated for getting as far as you have without the benefit of formal instruction, but the risk you take is getting too far out on a limb before realizing that it's going to break. I agree with arborint that nesting functions is not a good structure, at least for 99.99% of programming.

I think you may be overreaching for reusability. This would be better suited to OOP (Object Oriented PHP). You may want to read up on PHP classes.

But hang in there and show us any further ideas you have and I'm sure you will receive helpful analyses of your work.

Posted: Wed Sep 05, 2007 3:47 pm
by josa
The point in using field names (or column names) is to make sure the data goes into the right column. When you fetch the field names from the database like you do in your function, there is no mapping between field names and the values in $value_arr. The lazy approach would be to simply skip the field names in the query and just focus on the data inside VALUES(). But if you want to build a stable solution you have to map field names to data before you build the query. My suggestion is that you continue to improve on your functions (but avoid nested ones) and at the same time look into some existing Database Abstraction Layers.

/josa