tags:

views:

151

answers:

2

Hi there i'm playing with stmt for the first time (looking at converting my code over after previously using standard mysqli functions through an extension class.

However I can't seem to get it working. I'm using PHP 5.2.11 and mysql 4.1.22.

I've got a table structure like so:

CREATE TABLE IF NOT EXISTS `tags` (
  `tag_id` smallint(3) unsigned NOT NULL auto_increment,
  `title` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`tag_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

and here's my code, i've been using http://devzone.zend.com/article/686 and php.net as references.

$mysqli = new mysqli('***','***','***','***');

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $mysqli->prepare("INSERT INTO tags (title) VALUES (?)");

$stmt->bind_param('s', $title);

$title = 'test 123';

$stmt->execute();

$stmt->close();

$mysqli->close();

It seems simple enough and i'd hope that it would insert "test 123" into title column with the tag_id auto incrementing. But instead title is blank (tag_id does incremenet as one would assume).

Anyone have any ideas where i'm going wrong?

=================================================

As atli pointed out and tested, it works on MYSQL version of 5 and above. So it's probably a problem with using an old version of sql although the php.net documentation says that mysqli "allows you to access the functionality provided by MySQL 4.1 and above".

If anyone can suggest another reason why it may not be working or how to get it working in my environment that would great. If not I suppose i'll keep with my own extension of the mysqli class to prepare and clean queries.

A: 

Instead of

$stmt->bind_param('s', $title);

try this:

$stmt->bindParam(1, $title, PDO::PARAM_STR);
daviweb
He's using mysqli, not PDO. You can't just thrown in a PDO method when using the mysqli class.
Atli
As Atli said, PDO does help for reference it returns: "Fatal error: Cannot pass parameter 3 by reference"
Rob
+1  A: 

I've tested your code on my test server and it works fine. (Using PHP 5.3 and MySQL 5.1.37)
So it is not the code itself, or the database design.

My first guess would be some incompatibility with your MySQL version.
You could try using the mysqli_stmt::get_warnings function to see if there are any warnings issued.

If it is at all possible, I would recommend upgrading MySQL. The version you are using is getting pretty ancient. - I was going to install 4.1 and test this on it (just for fun :P) but they don't even have the downloads available anymore.

Atli
Upgrading isn't possible in my current environment. But i'll see if I can find another server that I can run a test on. get_warnings doesn't return anything.
Rob
Tested it on a 5.0.83 and it worked fine. Which is annoying for me. The introduction to mysqli on php.net says that it "allows you to access the functionality provided by MySQL 4.1 and above". So I would have thought it should work, obviously not.
Rob