Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform UPDATE with mysqli->prepare?

As I know there is a way to input data into a mysql database with mysqli, where you do not have to use mysql_real_escape_string. I mean like this:

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");  
$stmt->bind_param('sssd', "something", "something2", "something3", "123");

Now my question: Can you do the same with UPDATE instead of INSERT? What would the expression look like? Would it look like the following:

$stmt = $mysqli->prepare("UPDATE CountryLanguage SET some = ?, some2 = ?, some3 = ?, some4 = ?"); 
$stmt->bind_param('sssd', "something", "something2", "something3", "123");`

Thanks for your help.

like image 504
phpheini Avatar asked Mar 19 '11 12:03

phpheini


People also ask

What is MySQLi -> prepare?

The mysqli_prepare() function prepares an SQL statement for execution, you can use parameter markers ("?") in this query, specify values for them, and execute it later.

What is the use of prepare in PHP?

A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements basically work like this: Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?").


1 Answers

It would look the same, but don't forget the WHERE. Your example is correct.

like image 186
Jon Avatar answered Oct 01 '22 21:10

Jon