p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Warning: Unable to save MySQL query result in /home/www/topekatinman/rec.php on line 312... huh?? Unable to save query??


Message #1 by "Dan Ostrowski" <dan@t...> on Tue, 23 Apr 2002 19:23:21
has anyone ever seen anything like this?

here is the function i am running:

function insertIntoClientDatabase($array) {
	global $PHP_SELF;
	global $list_tables, $default_dbname, $HTTP_POST_VARS, $mandatory;

	db_connect();

	reset($array);
	reset($HTTP_POST_VARS);
	/*if there is no team name, then the team name is "individual" */
	if($HTTP_POST_VARS["iswim_opt_team_name"] == "" || !isset
($HTTP_POST_VARS["iswim_opt_team_name"])) {
		$the_team_name = "individual";
	} else {
		$the_team_name = $HTTP_POST_VARS["iswim_opt_team_name"];
	}
	
	/* the main loop. runs through and sets the table name to be used, 
basically. */
	while(list($table_id, $table_name) = each($list_tables)) {

		/* lets set some variables for the loop. mandatory or no?*/
		if($table_id == $mandatory_form_id) { $mandatory = "1"; } 
else { $mandatory = ""; }
		/* now lets unset the variables we will have to reuse */
		unset($query2, $result1, $result2, $insert_string, 
$query_string_array, $counter, $columns);
		$counter = 0;

		/* now we do a query and get a result for the field names. 
*/

		$result1 = mysql_listfields($default_dbname, $table_name);
		$columns = mysql_num_fields($result1);

		/******** now we go through and use the result1 to loop 
through and add to query string ******/

		for ($i = 0; $i <= $columns; $i++) {
			$field_name = mysql_fieldname($result1, $i);

			if($array[$field_name] != "" && isset($array
[$field_name])) {

				/* at this point, we have a field name who 
has a corresponding key */
				$query_string_array[] = "'" . $array
[$field_name] . "'";
				$counter++;

			} else {

				$query_string_array[] = fieldNameMagic
($field_name);

			}

		}

		$query_string_array = "'$the_team_name'";

		$insert_string = implode(",", $query_string_array);

		if($counter < 1) {
		$query2 = "INSERT INTO $table_name VALUES($insert_string)";
		$result2 = @mysql_query($query_2);
		}

	}

}

the error is happening on the line that says:

$field_name = mysql_fieldname($result1, $i);

(at present i am trying the deprecated mysql_fieldname, just in case the 
PHP module is older than mysql_field_name.

thoughts?

thanks,
dan
Message #2 by "Dan Ostrowski" <dan@t...> on Tue, 23 Apr 2002 19:25:30
grrr.....

i hate the way code turns out here...
Message #3 by "Nikolai Devereaux" <yomama@u...> on Tue, 23 Apr 2002 12:08:58 -0700
Hey Dan,


The for loop should probably use $i < $columns, not <=.  It seems you're
going one iteration too many.  That means that you're passing an index to
mysql_fieldname that doesn't exist, which is probably why you're generating
the error.

In true Nik fashion, here's a couple of unasked-for comments:

1)  What is $mandatory?  You set it or reset it ever iteration through the
loop, but don't use it within the loop.  It's modifying the value of a
global variable, but only the last iteration of the while() loop has any
effect on the variable's final value.

Instead of your way, set it like this:

$mandatory = ($table_id == $mandatory_form_id);

It's shorter and more consise.

If you would rathere have mandatory be integer 1 instead of boolean true,
you can do this:

$mandatory = ($table_id == $mandatory_form_id)? 1 : '';


2)  using isset and != ""
You test $array[$field_name] == "" before testing isset().  If
$array[$field_name] doesn't exist yet, you're forcing PHP to create it and
initialize it to "" so it can perform the == "" comparison.  Isset is
redundant here because it will ALWAYS return true, which technically isn't
correct.  Put all isset() calls before the == "" comparison, so that if the
variable is NOT set, you don't inadvertently create it before you check that
it exists.  Also, this will protect you from any "uninitialized variable"
warnings with E_ALL error reporting.


3)  $query_string_array
You set query_string_array to be a string on this line:
    $query_string_array = "'$the_team_name'";
and pass it to implode(), which expects an array.  That's why you're getting
the last error on the page -- be consistent with your datatypes.


Hope this helps,

Nik


  Return to Index