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

PreparedStatement IN clause alternatives?

An analysis of the various options available, and the pros and cons of each is available in Jeanne Boyarsky’s Batching Select Statements in JDBC entry on JavaRanch Journal. The suggested options are: Prepare SELECT my_column FROM my_table WHERE search_column = ?, execute it for each value and UNION the results client-side. Requires only one prepared … Read more

Can I bind an array to an IN() condition in a PDO query?

You’ll have to construct the query-string. <?php $ids = array(1, 2, 3, 7, 8, 9); $inQuery = implode(‘,’, array_fill(0, count($ids), ‘?’)); $db = new PDO(…); $stmt = $db->prepare( ‘SELECT * FROM table WHERE id IN(‘ . $inQuery . ‘)’ ); // bindvalue is 1-indexed, so $k+1 foreach ($ids as $k => $id) $stmt->bindValue(($k+1), $id); $stmt->execute(); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)