unexpected '{' in bind_parm statement

Some help needed (again!)
unexpected ‘{’ in bind_param statement.

[php] include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/cgi-bin/cnnct2mysqli.php’);

$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName); //this sequence req'd

if ($mysqli_connect_errno) {
	echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
	}

If(!file_exists(‘test5’))

$sql="CREATE TABLE test5(
	 `id` INTEGER(6) UNSIGNED NOT NULL AUTO_INCREMENT,
	 `amount` char(6) DEFAULT NULL,
	 `item_name` char(10) DEFAULT NULL,
	 `email` char(40) DEFAULT NULL,
	 `first_name` char(20),
	 `last_name` char(40),
	 `address1` char(100) DEFAULT NULL,
	 `address2` char(100) DEFAULT NULL,
	 `city`  char(50) DEFAULT NULL,
	 `state` char(6) DEFAULT NULL,
	 `country` char(6) DEFAULT NULL,	
	 `zip` char(12) DEFAULT NULL,
	 `mc_gross` char(6) DEFAULT NULL,
	 `mc_fee` char(6) DEFAULT NULL,
	 `tax` char(6) DEFAULT NULL,
	 `invoiceNo` char(20) DEFAULT NULL,
	 `payment_date` char(20) DEFAULT NULL,
	 `s_h` char(6) DEFAULT NULL,
	 `affiliation` char(40) DEFAULT NULL,
	 `e_anncmnt` char(10) DEFAULT NULL,
	 `hit` varchar(100) DEFAULT NULL,
	 `USIT_txt` char(5) DEFAULT FALSE,
	 `quantity` char(5) DEFAULT FALSE,
	 `updated` DATE,
	 `created` DATE,
	 PRIMARY KEY (invoiceNO),
	 INDEX (id)
)";

$amount		= 0.50;			//d
$item_name	= 'bk';			//s
$email 		= '[email protected]';      //s
$first_name	= 'Fname';		//s
$last_name  = 'LongerLastNames';	//s
$address1 	= 'Addr01';		//s
$address2 	= 'Addr02';		//s
$city		= 'cty';				//s
$state 		= 'MI';			//s
$country 	= 'US';				//s
$zip		= 48138;				//i
$mc_gross	= 0.00;			//d
$mc_fee		= 0.00;			//d
$tax		= 0.00;				//d
$invoiceNo	= 140630;		//i
$payment_date	= 'yyyy-mm-dd';//s
$s_h		= 0.00;				//d
$affiliation	= 'retired';		//s
$e_anncmnt	= true;			//s
$hit		= 123;				//i
$USIT_txt	= 'true';				//s
$quantity 	= 1;				//i  22 vars to here
$updated	= '';					//s
$created	= '';					//s

$stmt = $mysqli->prepare(“INSERT INTO test5 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)”)
{ //L. 70 <-- syntax error, unexpected ‘{’ … line 70
$stmt->bind_param(“dsssssssssidddisdssisiss”,$amount,$item_name,$email,$first_name,$last_name,$address1,$address2,$city,$state,$country,$zip,$mc_gross,$mc_fee,$tax,$invoiceNo,$payment_date,$s_h,$affiliation,$e_anncmnt,$hit,$USIT_txt,$quantity,updated,created);
$stmt->execute();
$stmt->bind_result(test5);
$stmt->close();
}

exit;[/php]

Any insights appreciated
usit

Ummm, what the heck are you trying to do with that bindParam()? You can’t just place all the vars in one bind statement. The bind only takes one param at a time.
RTFM! http://php.net/manual/en/pdostatement.bindparam.php

I’m trying to follow thd mysqli manual; in particular the bind_param() function at …
http://php.net/manual/en/mysqli-stmt.bind_param.php
It gives an example of bind_param() with 4 parameters, as shown here …

$stmt->bind_param(‘sssd’, $code, $language, $official, $percent);

I have not run across any limitations on the number of parameters prmitted.

usit

The limitation IS what the manual shows. Plus the params are not just random things, they are very specific things based on what you want the bind to do. You have 25 params in your statement. If you need to bind each var that is being sent to the db then you can use a foreach() to do that, just like the user provided examples show.

Got through the unexpected ‘{’ and then started over. (Should this be a new topic?)

What am I trying to do? Trying to create a db, insert into it, then select from it using mysqli – and learn a little mysqli in the process.
Here, I’m using an example oo script from http://php.net/manual/en/mysqli-stmt.bind-param.php.

At this point, I’m confused by the meaning of ‘bind_param()’. I have yet to find an explanation of what ‘bind’ means, the limits on the number of parameters, and why a limit.

The latest script throws the error: ‘Call to a member function bind_param() on a non-object’.

[php]// creaTbl_test5b.php
// Refs: http://php.net/manual/en/mysqli-stmt.bind-param.php

include_once ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/cnnct2mysqli.php');

$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName); //this sequence req'd

if ($mysqli_connect_errno) {
	echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
	}

If(!file_exists(test5))

$sql="CREATE TABLE test5(
	 `id` INTEGER(6) UNSIGNED NOT NULL AUTO_INCREMENT,
	 `amount` char(6) DEFAULT NULL,
	 `item_name` char(10) DEFAULT NULL,
	 `email` char(40) DEFAULT NULL,
	 `first_name` char(20),
	 PRIMARY KEY (invoiceNO),
	 INDEX (id)
)";

$amount		= 1.00;					//d
$item_name	= 'book';				//s
$email 		= '[email protected]'; //s
$first_name	= 'Fname';				//s

$stmt=$mysqli->prepare(“INSERT INTO test5 VALUES (?,?,?,?)”);

$stmt->bind_param(“dsss”,$amount,$item_name,$email,$first_name);

$stmt->execute();

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

$mysqli->close();
exit;[/php]

The last time I encountered this error mssg I think there was a discrepancy between the number of '?'s in the prepare statement and the number of params being passed in bind_param(). Here I have 4 and 4 as in the example.

Is there any free software debugger program that can test this script?

Any suggestions appreciated
usit

Ahhh, I see, I was thinking of the PDO version bind_param. The mysqli does allow for more params. The reason yours is failing is most likely cause you have backticks around the vars in the bind_param(), you don’t need anything around them if you are passing vars and not a direct string. If you did a direct string then you would use ‘single’ quotes around those values.

The bind basically means that the system is binding the value supplied to a certain argument in the prepare query and then making it safe to put in the db by sanitizing it with the type you specified.

Also, this $mysqli_connect_errno won’t ever exist cause you are using the OOP method.

FOUND errors and corrected them. Then decided to start over. Here’s the script I’m beginning with
[php]
//Example #1 Object oriented style found at http://php.net/manual/en/mysqli-stmt.bind-param.php

$mysqli = new mysqli(‘localhost’, ‘my_user’, ‘my_password’, ‘world’);

/* check connection */
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}

$stmt = $mysqli->prepare(“INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)”);
$stmt->bind_param(‘sssd’, $code, $language, $official, $percent);

$code = ‘DEU’;
$language = ‘Bavarian’;
$official = “F”;
$percent = 11.2;

/* execute prepared statement */
$stmt->execute();

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

/* close statement and connection */
$stmt->close();

/* Clean up table CountryLanguage */
$mysqli->query(“DELETE FROM CountryLanguage WHERE Language=‘Bavarian’”);
printf("%d Row deleted.\n", $mysqli->affected_rows);

/* close connection */
$mysqli->close();[/php]
"The above example will output:

1 Row inserted.
1 Row deleted.

=========

To this I added my DB credentials and a snipped to create the needed table. Here’s my new start:
[php] include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/cgi-bin/cnnct2mysqli.php’);

$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

/* check connection */
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}

If(!file_exists(test5))
$sql=“CREATE TABLE test5(
id INTEGER(6) UNSIGNED NOT NULL AUTO_INCREMENT,
code char(6) DEFAULT NULL,
language char(10) DEFAULT NULL,
official char(40) DEFAULT NULL,
percent integer(20),
PRIMARY KEY (code),
INDEX (id)
)”;

$stmt = $mysqli->prepare(“INSERT INTO test5 VALUES (?, ?, ?, ?)”);
$stmt->bind_param(‘sssd’, $code, $language, $official, $percent);

$code = ‘DEU’;
$language = ‘Bavarian’;
$official = “F”;
$percent = 11.2;

/* execute prepared statement */
$stmt->execute($stmt);

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

/* close statement and connection */
$stmnt->close();

/* Clean up table test5 */
$mysqli_query(“DELETE FROM test5 WHERE Language=‘Bavarian’”);
printf("%d Row deleted.\n", mysqli_affected_rows($link));

/* close connection */
$mysqli->close();[/php]

The compiler doesn’t like this script:
“Call to a member function bind_param() on a non-object”

Stuck again! I’ve tried to find the error. It’s possibly a type, but I can’t find it.

Help appreciated.

usit

OK, fount it! (after 4 days of now power then a false virus attack). Code now modified to the following …

[php]//testCreatInsrtBindExec.php

include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/cgi-bin/cnnct2mysqli.php’);
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName );

if ($mysqli->connect_errno) {
echo “Failed to connect to MySQL: (” . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$FistName = “Bavarian”; //s
$LastName = “F”; //s
$Age = 13; //d

$stmt = $mysqli->prepare(“INSERT INTO test5 VALUES (?, ?, ?)”);

$stmt->bind_param(‘ssd’, $FirstName, $LastName, $Age);

$stmt->execute();

if ($result = $mysqli->query(“SELECT * FROM test5”, MYSQLI_USE_RESULT)) {
printf(“Select returned %d rows.\n”, $result->num_rows);
}//Select returned 0 rows. (Why?, test5 contains 5 col: one row has | NULL | F |11 |, others are similar)

$result->close();

$mysqli->close();
exit;[/php]

The code runs w/o error, but I don’t under its output (see line 24). Two questions. 1) why the ‘NULL’ in col. 1, and 2) why are 0 rows returned?

Trying to get back to work.
Ideas appreciated.

usit

Sponsor our Newsletter | Privacy Policy | Terms of Service