prepared-statement

A few questions about PDO and prepared statements

微笑、不失礼 提交于 2019-12-12 17:22:20
问题 I'm starting to use PDO and prepared statements in my applications, but i have some questions to the pros out there. Hope you can help me! :) When should i use prepared statements? Every query in my entire application? Can i use prepared statements with INSERT's? Can i use prepared statements with variable columns in my INSERT? How much faster prepared statements are (when using SELECT or INSERT) Can i use prepared statements with UPDATE? Why should i use them, other than speed improvement

SELECT * from SQL table using prepared statement

核能气质少年 提交于 2019-12-12 14:18:25
问题 I'm using a prepared statement to SELECT * from a MySQL table and I'm not sure how to use while($row = mysqli_fetch_array($stmt)) to loop through and select items from the result array. This is my code, what am I doing wrong? $link = mysqli_connect($host, $username, $password, $db); $query = "SELECT * from `wp_posts` WHERE ID=? "; //$result = mysqli_query($link, $query); $stmt = mysqli_prepare($link, $query); if($stmt){ mysqli_stmt_bind_param($stmt, "i", $pid); mysqli_stmt_bind_result($stmt,

prevent error when target database in restore mode (sql preparation)

空扰寡人 提交于 2019-12-12 12:06:14
问题 I have a stored procedure that runs nightly. It pulls some data from a linked server and inserts it into a table on the server where the sql agent job runs. Before the INSERT statement is run, the procedure checks if the database on the linked server is online (STATE = 0). If not the INSERT statement is not run. IF EXISTS( SELECT * FROM OPENQUERY(_LINKEDSERVER,' SELECT name, state FROM sys.databases WHERE name = ''_DATABASENAME'' AND state = 0') ) BEGIN INSERT INTO _LOCALTABLE (A, B) SELECT A

De-allocating prepared queries

泄露秘密 提交于 2019-12-12 11:07:15
问题 EDIT: My thanks to both Daniel and Dennis. The problem is resolved now, and as they tactfully pointed out, the problem in this case was the programmer (specifically not thinking it all the way through) I wish I could accept both as answers. NOTE: To say I am a newbie to postgresql is to insult the newbies! I am writing a web app which will utilize a PostgreSQL database for it's data storage. In what I have done so far, I have managed a good grasp of the syntax for creating queries, and

MYSQLI prepared statement proceeds no output

依然范特西╮ 提交于 2019-12-12 10:25:31
问题 I've just started learning MYSQLI prepared statements technique and faced really annoying issue. My code stops at bind_result(). And I just don't get it what could be wrong, so please, guys, help me. Here is my config.php: <?php $db_name = 'site'; $db_user = 'root'; $db_pass = 'root'; $db_host = 'localhost'; $db_port = 8889; $db = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port); if($db->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } ?>

How to create a dynamic prepared statement in Java?

江枫思渺然 提交于 2019-12-12 10:13:06
问题 I know using prepared statements helps to avoid sql-injection. My problem is, that a prepared statement is usually very static. I have a problem, where I build the where-clause of the sql-query at runtime, depending on user-input. Depending on which input-fields are filled I have to add the correspending statements to the where-clause. How can this be realized with prepared statements? 回答1: I guess you could dynamically build your prepared statements based on what columns they want to query,

How do I pass a []slice to an IN-condition in a prepared SQL statement with non-IN-conditions as well?

心不动则不痛 提交于 2019-12-12 10:10:45
问题 Imagine you have the following SQL query: SELECT * FROM foo WHERE type = ? AND subtype IN (?) And you have the following possible data (we imagine that a user interface can set these data): var Type int var SubTypes []int In the case of SubTypes , we are talking about a multiple choice selection. Now, the following code won't work: rows, err := sqldb.Query(`SELECT * FROM foo WHERE type = ? AND subtype IN (?)`, Type, SubTypes) Because the driver (at least the mysql driver used in this example)

Why do I need a connection to create PreparedStatements?

五迷三道 提交于 2019-12-12 09:37:31
问题 I would like to use prepared statements, for many reasons. But, I would like to create a method that looks like this: /* This opens a connection, executes the query, and closes the connection */ public static void executeNonQuery(String queryString); In other words, I want my application logic to only have to formulate the queries and feed in parameters, but not deal with connections & statements. However, PreparedStatements are created from a connection object, so I am currently forced into

Wildcards in Java PreparedStatements

老子叫甜甜 提交于 2019-12-12 09:37:16
问题 Here's my current SQL statement: SEARCH_ALBUMS_SQL = "SELECT * FROM albums WHERE title LIKE ? OR artist LIKE ?;"; It's returning exact matches to the album or artist names, but not anything else. I can't use a '%' in the statement or I get errors. How do I add wildcards to a prepared statement? (I'm using Java5 and MySQL) Thanks! 回答1: You put the % in the bound variable. So you do stmt.setString(1, "%" + likeSanitize(title) + "%"); stmt.setString(2, "%" + likeSanitize(artist) + "%"); You

warning:mysql_fetch_array() expects parameter 1 to be resource, object given

冷暖自知 提交于 2019-12-12 08:23:16
问题 Hey guys, I'm getting the above warning when I try to run this code: $mysqli=new mysqli("localhost", "***", "***","***") or die(mysql_error()); function checklogin($username, $password){ global $mysqli; $result = $mysqli->prepare("SELECT * FROM users WHERE username = ?"); $result->bind_param("s", $username); $result->execute(); if($result != false){ $dbArray=mysql_fetch_array($result); 回答1: You are mixing mysql and mysqli calls in your code. Use mysqli_fetch_array instead of mysql_fetch_array