Jump to content

this is due in a couple hours and im blinded by the code!? (dynamic prep. stmt)


raccer

Recommended Posts

This code is probably ugly, its my first PHP class; and I can't figure why my binded params aren't equal!?!?

 

Thank you in advance for mucking through this code and for any advice you have!

 

Here is my ghetto dynamic stmt;

	if (!empty($rstArr)) {	

		require self::cnxDetails;
		$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbname);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		} 


		$prepString = $rstArr[0];
		echo "\n\n"; var_dump($prepString);
		$bindString = $rstArr[1];
		echo "\n\n"; var_dump($bindString);
		$evalString = $rstArr[2];
		echo "\n\n"; var_dump($evalString);
		eval($evalString);


		echo "\n val0==$val0 \n";
		echo "\n val1==$val1 \n";
		echo "\n val2==$val2 \n";
		echo "\n val3==$val3 \n";
		echo "\n val4==$val4 \n";
		echo "\n val5==$val5 \n";		

		$stmt = $mysqli->stmt_init();

		if($stmt->prepare($prepString)) {
			//if($stmt->error) echo $stmt->error; 

			$stmt->bind_param($bindString);
			//if($stmt->error) echo $stmt->error; 

			$stmt->execute();
			if($stmt->error) echo $stmt->error; 
			printf("%d Row inserted.\n", $stmt->affected_rows);

			$stmt->close();
		}
		$mysqli->close();    

		echo "\n val0==$val0 \n";
		echo "\n val1==$val1 \n";
		echo "\n val2==$val2 \n";
		echo "\n val3==$val3 \n";
		echo "\n val4==$val4 \n";
		echo "\n val5==$val5 \n";
	}

 

And here is the output & error it's incessantly producing; (I left a field blank on this submission (street2), but the error persists regardless)

 

 

array(5) {
  ["$val0"]=>
  string(14) "'Some Road Dr'"
  ["$val1"]=>
  string(5) "'12a'"
  ["$val2"]=>
  string(9) "'SanFran'"
  ["$val3"]=>
  string(4) "'CA'"
  ["$val4"]=>
  string(7) "'65056'"
}


string(128) "INSERT INTO Physical_Contact(Street1_editable, Unit_editable, City_editable, State_editable, Zip_editable) VALUES(?, ?, ?, ?, ?)"


string(42) "'ssssi', $val0, $val1, $val2, $val3, $val4"


string(99) "$val0 = "'Some Road Dr'"; $val1 = "'12a'"; $val2 = "'SanFran'"; $val3 = "'CA'"; $val4 = "'65056'"; "

val0=='Some Road Dr' 

val1=='12a' 

val2=='SanFran' 

val3=='CA' 

val4=='65056' 

val5== 
<br />
<b>Warning</b>:  Wrong parameter count for mysqli_stmt::bind_param() in <b>C:\xampp\htdocs\donorware\includes\myclass.php</b> on line <b>874</b><br />
No data supplied for parameters in prepared statement0 Row inserted.

val0=='Some Road Dr' 

val1=='12a' 

val2=='SanFran' 

val3=='CA' 

val4=='65056' 

val5== 
array(1) {
  ["$val0"]=>
  string( "'PayPal'"
}


string(51) "INSERT INTO Donationmeta(Method_editable) VALUES(?)"


string(10) "'s', $val0"


string(20) "$val0 = "'PayPal'"; "

val0=='PayPal' 

val1== 

val2== 

val3== 

val4== 

val5== 
<br />
<b>Warning</b>:  Wrong parameter count for mysqli_stmt::bind_param() in <b>C:\xampp\htdocs\donorware\includes\myclass.php</b> on line <b>874</b><br />

No data supplied for parameters in prepared statement0 Row inserted.

val0=='PayPal' 

val1== 

val2== 

val3== 

val4== 

val5== 

 

I hope you see the glaring mistake and point me in the right direction!

$stmt->bind_param($bindString);

 

You can't do this. First argument needs to be a string denoting parameter types, and next parameters need to be actual variables.

If you want to stick to this ugly solution you could do something like:

 

eval('$stmt->bind_param('.$bindString.');')

Hey Mchl, thanks for the reply!

 

(didn't work though, not saying it couldn't be a million other things wrong w my code lol)

 

I found a topic elsewhere where someone suggested using a call_user_func_array(),  [ in lieu of eval() ], I'm going to try and patch one in and see what happens. This assignment has been both a learning experience and a case study in top-down design.....

Mysqli's prepared statement API is a bit half baked I must admit. If you want to create a general purpose function you need to use dirty tricks like this, or create variables on-the-fly. call_user_func_array() might also work indeed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.