prepared-statement

PDO: Cost of Calling prepare() in a loop?

南笙酒味 提交于 2019-12-01 04:54:39
问题 I'm writing a database class for my website with functions such as fetchOne , fetchAll which prepare, execute (+ bind), and fetch the query all in one so I don't have to individually call those functions every time. Some cron jobs on my site execute thousands, or even millions of queries inside of a loop. Would using my class cause the statement to be re-prepared each iteration of the loop or would PDO "remember" the query has already been prepared? Would this significantly impact performance

MySQL PHP PDO prepared statements - performance issues vs security

﹥>﹥吖頭↗ 提交于 2019-12-01 04:20:21
I am thinking of rewriting some open-source application for my purposes to PDO and transactions using InnoDB (mysql_query and MyISAM now). My question is: Which cases are reasonable for using prepared statements? Because everywhere I am reading is stated (even in many posts here) that I should use prepared statements every time and everywhere because of the 1. security and 2. performance. Even PHP manual recommends using prepared statements and not mentioning the escape-thing. You can't deny the security mechanism. But thinking it over and over it comes to mind that having to prepare the

PHP bind_params with null

旧巷老猫 提交于 2019-12-01 04:11:38
I am trying to bind params to a INSERT INTO MySQLi prepared statement if that variable exists, otherwise insert null. This is what I have, but it is not working: if (!empty($name)) { $result->bind_param('ss', $name, $url_friendly_name); } else { $result->bind_param('ss', null, null); } if (!empty($description)) { $result->bind_param('s', $description); } else { $result->bind_param('s', null); } Does anyone know of a better way of doing this or is there just a minor issue with my code. I am doing the above for each variable in the prepared statement. bind_param works by reference. That means

PHP bind_params with null

落爺英雄遲暮 提交于 2019-12-01 02:27:03
问题 I am trying to bind params to a INSERT INTO MySQLi prepared statement if that variable exists, otherwise insert null. This is what I have, but it is not working: if (!empty($name)) { $result->bind_param('ss', $name, $url_friendly_name); } else { $result->bind_param('ss', null, null); } if (!empty($description)) { $result->bind_param('s', $description); } else { $result->bind_param('s', null); } Does anyone know of a better way of doing this or is there just a minor issue with my code. I am

How to insert Date into MySQL Database table in Java?

北城以北 提交于 2019-12-01 01:46:15
How do I insert date, without time, to MySQL database table? I tried these codes but I get the following exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Mar 05 00:00:00 GMT-08:00 2014,1,1)' at line 1 NOTE: In MySQL table, data type of this column I chose date datatype String Date = "\\d\\d\\d\\d\\D[0-1][0-9]\\D[0-3][0-9]"; while (DateMatcher.find()) { String date = DateMatcher.group().trim(); DateFormat formatter = new SimpleDateFormat(

MySQL PHP PDO prepared statements - performance issues vs security

人盡茶涼 提交于 2019-12-01 01:37:28
问题 I am thinking of rewriting some open-source application for my purposes to PDO and transactions using InnoDB (mysql_query and MyISAM now). My question is: Which cases are reasonable for using prepared statements? Because everywhere I am reading is stated (even in many posts here) that I should use prepared statements every time and everywhere because of the 1. security and 2. performance. Even PHP manual recommends using prepared statements and not mentioning the escape-thing. You can't deny

Why is the PostgreSQL JDBC prepared statement threshold defaulted to 5?

♀尐吖头ヾ 提交于 2019-11-30 23:03:18
By default, the parameter statement treshold is set to 5, instead of 1. That is, ((PGStatement) my_statement).getPrepareThreshold() always returns 5 by default. What would be the reason for that? Why would I want not to have to use the server-side prepared statement for the first 4 times the query is executed? I fail to understand why I would ever set this to another value than 1 and why this isn't by default set to 1. Can you explain? Thanks a lot. Server side prepared statements consume server side resources to store the execution plan for the statement. The threshold provides a heuristic

PHP/PostgreSQL: check if a prepared statement already exists

柔情痞子 提交于 2019-11-30 22:14:18
I create my prepared statement as: pg_prepare('stm_name', 'SELECT ...'); Today, I had a problem (calling twice a function for mistake) when declaring a prepared statement with the same name twice: Warning: pg_prepare() [function.pg-prepare]: Query failed: ERROR: prepared statement "insert_av" already exists in xxx on line 221 So, as the question title, there is a way to check if a prepare statement with the same label already exists, and in case, overwrite it? I know this error is from my mistake and will be solved by simply declaring the prepared statements at the begin of my code, but I'm

Should I be using PreparedStatements for all my database inserts in Java?

試著忘記壹切 提交于 2019-11-30 20:56:47
What is the recommended method for escaping variables before inserting them into the database in Java? As I understand, I can use PreparedStatement.setString() to escape the data, but PreparedStatement seems somewhat impractical if I don't plan to run the same query ever again.. Is there a better way to do it without preparing every query? Yes, use prepared statements for everything. They're parsed once. They're immune from SQL injection attacks. They're a better design because you have to think about your SQL and how it's used. If you think they're only used once, you aren't looking at the

Confusion with setFetchSize method of Statement Object

半城伤御伤魂 提交于 2019-11-30 20:45:49
问题 Initially, I asked this question I solve this by setting fetchSize to Integer.MIN_VALUE , but I have some questions about this When I set fetchSize to 10 or another positive integer then it does not work, after setting it to Integer.MIN_VALUE it works, why is this? If we set negative value then it gives illegal value error but Integer.MIN_VALUE is -2147483648 so why is it not giving errors? This table contains 6 million records and I closed resultset after fetching 100 or 200 records then it