PDO get the last ID inserted

That’s because that’s an SQL function, not PHP. You can use PDO::lastInsertId(). Like: $stmt = $db->prepare(“…”); $stmt->execute(); $id = $db->lastInsertId(); If you want to do it with SQL instead of the PDO API, you would do it like a normal select query: $stmt = $db->query(“SELECT LAST_INSERT_ID()”); $lastId = $stmt->fetchColumn();

PHP PDO: charset, set names?

You’ll have it in your connection string like: “mysql:host=$host;dbname=$db;charset=utf8mb4” HOWEVER, prior to PHP 5.3.6, the charset option was ignored. If you’re running an older version of PHP, you must do it like this: $dbh = new PDO(“mysql:host=$host;dbname=$db”, $user, $password); $dbh->exec(“set names utf8mb4”);

Row count with PDO

$sql = “SELECT count(*) FROM `table` WHERE foo = ?”; $result = $con->prepare($sql); $result->execute([$bar]); $number_of_rows = $result->fetchColumn(); Not the most elegant way to do it, plus it involves an extra query. PDO has PDOStatement::rowCount(), which apparently does not work in MySql. What a pain. From the PDO Doc: For most databases, PDOStatement::rowCount() does not return … Read more

Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

According to the official Laravel 7.x documentation, you can solve this quite easily. Update your /app/Providers/AppServiceProvider.php to contain: use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); } Alternatively, you may enable the innodb_large_prefix option for your database. Refer to your database’s documentation for instructions on … Read more

What is the difference between bindParam and bindValue?

From the manual entry for PDOStatement::bindParam: [With bindParam] Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called. So, for example: $sex = ‘male’; $s = $dbh->prepare(‘SELECT name FROM students WHERE sex = :sex’); $s->bindParam(‘:sex’, $sex); // use bindParam to bind the variable $sex … Read more

PDOException SQLSTATE[HY000] [2002] No such file or directory

Laravel 4: Change “host” in the app/config/database.php file from “localhost” to “127.0.0.1” Laravel 5+: Change “DB_HOST” in the .env file from “localhost” to “127.0.0.1” I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the “host” in the /app/config/database.php file from “localhost” to “127.0.0.1”. Not … 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)