Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert the record with MySQL NOW() function in MySQLi with bind_param?

I need to execute this mysql query in MySQLi PDO with bind parametr in PHP:

mysql_query("INSERT INTO `posts` (post_name,publish_date) VALUES ($post_name,NOW()) ")

I use the script like this, but it doesn't insert publish_date correctly.

$publish_date = 'NOW()';
$insert = $mysqli->prepare("INSERT INTO posts (post_name,publish_date) VALUES (?,?)");
$insert->bind_param("ss", $post_name $publish_date);
$insert->execute();

It inserts the record into the publish_date column like this: 0000-00-00 00:00:00

How can I do this ? Thanks in advance.

P.S: The type of date column is datatime.

like image 369
JohnUS Avatar asked Feb 29 '12 10:02

JohnUS


People also ask

What is MySQLi Bind_param () function in PHP?

The PHP bind_param() function is used to bind variables to a prepared statement, as parameters, in PHP MySQLi object-oriented style.

Can we use MySQL and MySQLi together?

Using the PHP MySQL to MySQLi Migration PackagePHP MySQL to MySQLi is package that emulates the mysql extension functions using the mysqli extension. It uses these replacement code solutions and can act as a stop-gap while you work on migrating your code.

What is the use of the function Mysqli_num_rows?

The mysqli_num_rows() function returns the number of rows in a result set.


1 Answers

Probably you should try using the date function not NOW()

$publish_date =date("Y-m-d H:i:s");
$insert = $mysqli->prepare("INSERT INTO posts (post_name,publish_date) VALUES (?,?)");
$insert->bind_param("ss", $post_name $publish_date);
$insert->execute();
like image 124
smilezjim Avatar answered Sep 29 '22 03:09

smilezjim