Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Define $title before using it. You should include this line

$title = 'test 123';

before

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

Full code :

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

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

$title = 'test 123';

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

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

$stmt->execute();

$stmt->close();

$mysqli->close();

Instead of

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

try this:

$stmt->bindParam(1, $title, PDO::PARAM_STR);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow