php, mysql – Too many connections to database error

There are a bunch of different reasons for the “Too Many Connections” error. Check out this FAQ page on MySQL.com: http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html Check your my.cnf file for “max_connections”. If none exist try: [mysqld] set-variable=max_connections=250 However the default is 151, so you should be okay. If you are on a shared host, it might be that other … Read more

How to escape single-quote (apostrophe) in string using php

The answer is that you don’t need to. The proper way to use PDO’s prepare is like this: $stmt = $pdo->prepare( “SELECT * FROM `products_keywords` WHERE `product_type` = ?”); This is the whole point of using a prepared statement. Then you bind the parameter as follows: $stmt->bindParam(1, $product_type) Proof, Schema: create table `products_keywords` ( `id` … Read more

PHP/SQL Insert Error when using Named Placeholders

Your $userData must have exactly the same placeholders bound by your statement, no more and no fewer. See PDOStatement::execute documentation, the part that says “You cannot bind more values than specified”. You need to prepare your argument to execute() to match your binds exactly. This is easy with array_intersect_key() if you arrange your arrays correctly. … Read more

PHP PDO how to run a multiple query request?

There are three queries in this request and therefore you have to run them in three calls, not one: $objPdo->query(“CREATE TEMPORARY TABLE r1 SELECT CONCAT(MONTH(Heure_deb),”https://stackoverflow.com/”,DAY(Heure_deb)) as ‘Date’, Heure_deb, Operateur.Id_op , Nom_op , Prenom_op, Nom_act , TIME(Heure_deb) as heure, Commentaire, Type FROM Operateur, Pointage, Activite WHERE Operateur.Id_op = Pointage.Id_op AND Activite.Id_act = Pointage.Id_act ORDER BY date, … Read more

I’m getting this error code: Invalid argument supplied for foreach() [duplicate]

Try to increase error mode: <?php $hostname=”localhost”; $username=”root”; $password=”; try { $dbh = new PDO(“mysql:host=$hostname;dbname=stickercollections”,$username,$password); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line echo ‘Connected to Database<br/>’; $sql = “SELECT * FROM stickercollections”; foreach ($dbh->query($sql) as $row) { echo $row[“collection_brand”] .” – “. $row[“collection_year”] .”<br/>”; } $dbh = null; } catch(PDOException $e) { echo $e->getMessage(); } … Read more

PDO and MySQL Fulltext searches

This is unfortunately a weird exception to the use of query parameters (edit: but apparently not in the most recent point-release of each MySQL branch, see below). The pattern in AGAINST() must be a constant string, not a query parameter. Unlike other constant strings in SQL queries, you cannot use a query parameter here, simply … Read more

How can I pass an array of PDO parameters yet still specify their types?

If you turn off the default setting of PDO::ATTR_EMULATE_PREPARES, then it will work. I just found out that that setting is on by default for mysql, which means you never actually use prepared statements, php internally creates dynamic sql for you, quoting the values for you and replacing the placeholders. Ya, a major wtf. $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, … Read more