prepared parameterized query with PDO

To create the connection try { $db = new PDO(“mysql:dbname=”.DB_NAME.”;host=”.DB_HOST,DB_USER,DB_PWD); } catch (PDOException $e) { die(“Database Connection Failed: ” . $e->getMessage()); } Then to prepare a statement $prep = $db->prepare(“SELECT * FROM `users` WHERE userid = ‘:id'”); As you can see, you label each parameter you’d like by prefixing any string with ‘:’. Then all … Read more

How to get a list of databases?

It doesn’t appear as though there’s a function available to do this, but you can execute a show databases; query and the rows returned will be the databases available. EXAMPLE: Replace this: $db_list = mysql_list_dbs($link); //mysql With this: $db_list = mysqli_query($link, “SHOW DATABASES”); //mysqli

Cannot figure out how to run a mysqli_multi_query and use the results from the last query

From the manual: mysqli_multi_query() returns a bool indicating success. To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result(). Here is a function that returns the last result of a multi-query: function mysqli_last_result($link) { while (mysqli_more_results($link)) { mysqli_use_result($link); mysqli_next_result($link); } … Read more

Mysqli multiple row insert, simple multi insert query [duplicate]

The mysqli class provides a number of different ways of accomplishing your inserts, each with its own benefits. Certainly, one of them should fit your needs. The following examples assume that your unspecified “extracted data” is stored in an array of arrays: $bigArray[0…datasetsize][0…2]. The mysqli database is assumed to be $db. Method 1 – As … Read more

Create mysql trigger via PHP?

While the mysqli doesn’t to anything with DELIMITER in multi-query statements, it actually doesn’t do anything with any delimiters at all in normal queries, so just shove your triggers in one by one: $ cat i.php <?php $mysqli = new mysqli(‘localhost’, ‘test’, ”, ‘test’); $sql = ” CREATE TRIGGER `_foo_fulltext_update` AFTER UPDATE ON `foo` FOR … Read more

When should I close a database connection in PHP?

Never create a new connection for every query; You don’t even have to close it manually. Just create the connection at the beginning of you page have a look at: How do you efficiently connect to mysql in php without reconnecting on every query you can use this: public function __destruct() { mysql_close($this->connection); } it … Read more

Warning: mysqli_query() expects at least 2 parameters, 1 given. What? [duplicate]

The issue is that you’re not saving the mysqli connection. Change your connect to: $aVar = mysqli_connect(‘localhost’,’tdoylex1_dork’,’dorkk’,’tdoylex1_dork’); And then include it in your query: $query1 = mysqli_query($aVar, “SELECT name1 FROM users ORDER BY RAND() LIMIT 1”); $aName1 = mysqli_fetch_assoc($query1); $name1 = $aName1[‘name1′]; Also don’t forget to enclose your connections variables as strings as I have … Read more