Passing an array to a query using a WHERE clause

Locked. Comments on this answer have been disabled, but it is still accepting other interactions. Learn more. BEWARE! This answer contains a severe SQL injection vulnerability. Do NOT use the code samples as presented here, without making sure that any external input is sanitized. $ids = join(“‘,'”,$galleries); $sql = “SELECT * FROM galleries WHERE id … Read more

How to use multiple databases in Laravel

Tested versions (Updated) Version Tested (Yes/No) 4.2 No 5.x Yes (5.5) 6.x No 7.x No 8.x Yes (8.4) 9.x Yes (9.2) Define Connections Using .env >= 5.0 (or higher) In .env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database1 DB_USERNAME=root DB_PASSWORD=secret DB_CONNECTION_SECOND=mysql DB_HOST_SECOND=127.0.0.1 DB_PORT_SECOND=3306 DB_DATABASE_SECOND=database2 DB_USERNAME_SECOND=root DB_PASSWORD_SECOND=secret In config/database.php ‘mysql’ => [ ‘driver’ => env(‘DB_CONNECTION’), ‘host’ => env(‘DB_HOST’), ‘port’ … Read more

Remove new lines from string and replace with one empty space

You have to be cautious of double line breaks, which would cause double spaces. Use this really efficient regular expression: $string = trim(preg_replace(‘/\s\s+/’, ‘ ‘, $string)); Multiple spaces and newlines are replaced with a single space. Edit: As others have pointed out, this solution has issues matching single newlines in between words. This is not … Read more

MySQL query to get column names?

The best way is to use the INFORMATION_SCHEMA metadata virtual database. Specifically the INFORMATION_SCHEMA.COLUMNS table… SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`=’yourdatabasename’ AND `TABLE_NAME`=’yourtablename’; It’s VERY powerful, and can give you TONS of information without need to parse text (Such as column type, whether the column is nullable, max column size, character set, etc)… Oh, and … Read more

How to get xdebug var_dump to show full object/array

These are configurable variables in php.ini: ; with sane limits xdebug.var_display_max_depth = 10 xdebug.var_display_max_children = 256 xdebug.var_display_max_data = 1024 ; with no limits ; (maximum nesting is 1023) xdebug.var_display_max_depth = -1 xdebug.var_display_max_children = -1 xdebug.var_display_max_data = -1 Of course, these may also be set at runtime via ini_set(), useful if you don’t want to modify … Read more

What’s the use of ob_start() in php?

Think of ob_start() as saying “Start remembering everything that would normally be outputted, but don’t quite do anything with it yet.” For example: ob_start(); echo(“Hello there!”); //would normally get printed to the screen/output to browser $output = ob_get_contents(); ob_end_clean(); There are two other functions you typically pair it with: ob_get_contents(), which basically gives you whatever … Read more

How do I compare two DateTime objects in PHP 5.2.8?

The following seems to confirm that there are comparison operators for the DateTime class: dev:~# php <?php date_default_timezone_set(‘Europe/London’); $d1 = new DateTime(‘2008-08-03 14:52:10’); $d2 = new DateTime(‘2008-01-03 11:11:10’); var_dump($d1 == $d2); var_dump($d1 > $d2); var_dump($d1 < $d2); ?> bool(false) bool(true) bool(false) dev:~# php -v PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 26 2009 20:09:03) … Read more