mysqli: can it prepare multiple queries in one statement?

A prepared statement can only execute one MySQL query. You can prepare as many statements as you want in different variables: $stmtUser = $sql->prepare(“INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)”); $stmtProc = $sql->prepare(“INSERT INTO process (id_user, idp) VALUES (?,?);”); And then execute them later. If you want to ensure that neither one is … Read more

LIMIT keyword on MySQL with prepared statement [duplicate]

Here’s the problem: $comments = $db->prepare($query); /* where $db is the PDO object */ $comments->execute(array($post, $min, $max)); The manual page for PDOStatement::execute() says (emphasis mine): Parameters input_parameters An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR. Thus your parameters … Read more

Reusing a PreparedStatement multiple times

The second way is a tad more efficient, but a much better way is to execute them in batches: public void executeBatch(List<Entity> entities) throws SQLException { try ( Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL); ) { for (Entity entity : entities) { statement.setObject(1, entity.getSomeProperty()); // … statement.addBatch(); } statement.executeBatch(); } } You’re however … Read more

How does a PreparedStatement avoid or prevent SQL injection?

Consider two ways of doing the same thing: PreparedStatement stmt = conn.createStatement(“INSERT INTO students VALUES(‘” + user + “‘)”); stmt.execute(); Or PreparedStatement stmt = conn.prepareStatement(“INSERT INTO student VALUES(?)”); stmt.setString(1, user); stmt.execute(); If “user” came from user input and the user input was Robert’); DROP TABLE students; — Then in the first instance, you’d be hosed. … Read more

PDO Prepared Inserts multiple rows in single query

Multiple Values Insert with PDO Prepared Statements Inserting multiple values in one execute statement. Why because according to this page it is faster than regular inserts. $datafields = array(‘fielda’, ‘fieldb’, … ); $data[] = array(‘fielda’ => ‘value’, ‘fieldb’ => ‘value’ ….); $data[] = array(‘fielda’ => ‘value’, ‘fieldb’ => ‘value’ ….); more data values or you … Read more