This via r/programming today: Recommended Pagination for APIs. Keyset pagination is an approach I’ve seen recommended a fair bit lately…
Tag Archives: sql
Keyset pagination
A case for keyset pagination in preference to OFFSET.
What ORMs have taught me: just learn SQL
This old chestnut What ORMs have taught me: just learn SQL popped up on HN today.
Null
Stumbled upon the Null (SQL) Wikipedia article. Looks interesting. Will read.
systemd RandomizedDelaySec
Today I was reading Some SQL Tricks of an Application DBA and I learned about the RandomizedDelaySec systemd option.
PDO: Execute a prepared statement using array for IN clause
See Example #5 here.
/* Execute a prepared statement using an array of values for an IN clause */ $params = array(1, 21, 63, 171); /* Create a string for the parameter placeholders filled to the number of params */ $place_holders = implode(',', array_fill(0, count($params), '?')); /* This prepares the statement with enough unnamed placeholders for every value in our $params array. The values of the $params array are then bound to the placeholders in the prepared statement when the statement is executed. This is not the same thing as using PDOStatement::bindParam() since this requires a reference to the variable. PDOStatement::execute() only binds by value instead. */ $sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)"); $sth->execute($params);
Running a SQL text file in mysql CLI
Use the ‘source’ command:
mysql> source path/to/file.sql
Escaping SQL values in PHP without using mysql_real_escape_string
Found this article which has a handy ‘escape’ function:
// replace any non-ascii character with its hex code. function escape($value) { $return = ''; for($i = 0; $i < strlen($value); ++$i) { $char = $value[$i]; $ord = ord($char); if($char !== "'" && $char !== "\"" && $char !== '\\' && $ord >= 32 && $ord <= 126) $return .= $char; else $return .= '\\x' . dechex($ord); } return $return; }
Execute Multiple MySQL Queries from One String in PHP
Found this article today, Execute Multiple MySQL Queries from One String in PHP. It’s a work around for processing MySQL batch queries separated by semicolons.
MySQL: Error in SQL: Commands out of sync; you can’t run this command now
Today I was programming MySQL via PHP and I received an error “Error in SQL: Commands out of sync; you can’t run this command now”. It turned out the problem was that a previous mulit_query had mysqli_results that hadn’t been freed. This article helped me solve the problem, and now my batch mode SQL processor looks like this:
public function execute_batch( $sql ) { $this->write_count++; if ( ! $this->link->multi_query( $sql ) ) { $this->throw_error( $sql ); } if ( $this->link->more_results() ) { do { $result = $this->link->use_result(); if ( $result instanceof mysqli_result ) { $result->free(); } } while ( $this->link->more_results() && $this->link->next_result() ); } }
This code executes the SQL batch and then frees any mysqli_results that result from the query batch.