Using “like” wildcard in prepared statement

You need to set it in the value itself, not in the prepared statement SQL string. So, this should do for a prefix-match: notes = notes .replace(“!”, “!!”) .replace(“%”, “!%”) .replace(“_”, “!_”) .replace(“[“, “![“); PreparedStatement pstmt = con.prepareStatement( “SELECT * FROM analysis WHERE notes LIKE ? ESCAPE ‘!'”); pstmt.setString(1, notes + “%”); or a suffix-match: … Read more

How to bind mysqli bind_param arguments dynamically in PHP?

Using PHP 5.6 you can do this easy with help of unpacking operator(…$var) and use get_result() insted of bind_result() public function get_result($sql,$types = null,$params = null) { $stmt = $this->mysqli->prepare($sql); $stmt->bind_param($types, …$params); if(!$stmt->execute()) return false; return $stmt->get_result(); } Example: $mysqli = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME); $output = new search($mysqli); $sql = “SELECT * FROM root_contacts_cfm WHERE root_contacts_cfm.cnt_id … Read more

Java: Insert multiple rows into MySQL with PreparedStatement

You can create a batch by PreparedStatement#addBatch() and execute it by PreparedStatement#executeBatch(). Here’s a kickoff example: public void save(List<Entity> entities) throws SQLException { try ( Connection connection = database.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_INSERT); ) { int i = 0; for (Entity entity : entities) { statement.setString(1, entity.getSomeProperty()); // … statement.addBatch(); i++; if (i % 1000 … Read more

How to use mysqli prepared statements?

From the mysqli::prepare docs: The parameter markers must be bound to application variables using mysqli_stmt_bind_param() and/or mysqli_stmt_bind_result() before executing the statement or fetching rows. bind_param docs. i.e.: $name=”one”; $age = 1; $stmt = $mysqli->prepare(“INSERT INTO users (name, age) VALUES (?,?)”); // bind parameters. I’m guessing ‘string’ & ‘integer’, but read documentation. $stmt->bind_param(‘si’, $name, $age); // … Read more

mysqli_stmt::bind_result(): Number of bind variables doesn’t match number of fields in prepared statement

$mysqli->prepare(“SELECT username, password FROM users WHERE username = ?”); $username = $_POST[‘name’]; $stmt->bind_param(‘s’ ,$username); $stmt->execute(); $stmt->bind_result($username, $password); Your select syntax was wrong, the correct syntax is SELECT field1, field2, field3 FROM TABLE WHERE field1 = ? AND field2 = ? To select more fields simply separate them by a comma and not an AND