PDO select all fields with 'date' field formattig - php

I have some prepared statement:
$statement = $dbh->prepare("SELECT * FROM `". $table ."` WHERE id = ?");
$statement->execute([$id])
I need to rebuild my query to select all fields with date formatting, something like:
$statement = $dbh->prepare("SELECT *, DATE_FORMAT(date_field, 'M/D/YYYY') FROM `". $table ."` WHERE id = ?");
but this solution doesn't work...
P.S I can't select by fields list because it's a dynamic query with different tables and structures.

I have been a little remiss:
$dbh->prepare("SELECT *, DATE_FORMAT(date , '%d-%m-%Y') AS date FROM `". $table ."` WHERE id = ?");

Related

Preparing and executing two queries

If i have 2 queries like this
$query1 = "SELECT title, content, image FROM table1 WHERE id = :id";
$query2 = "SELECT title, content FROM table2 WHERE id = :id";
Can i prepare two of them at once like something like this
$stmt = $conn->prepare($query1, $query2);
$stmt->execute([':id' => $id]);
You can do this with MySQL UNION.

Sum multiuple columns

I have numbers stored in my MySQL (paid). I need to SUM the columns.
$sql= "SELECT SUM(furniture) FROM paid";
$stmt = $connect->prepare($sql);
$stmt->execute();
$furniture = (int) $stmt->fetchColumn();
$sql= "SELECT SUM(groceries) FROM paid";
$stmt = $connect->prepare($sql);
$stmt->execute();
$groceries = (int) $stmt->fetchColumn();
//so on....
There are morthan 50 columns in the database. My question is, Is there a shorter way to write this so I can get the SUM for each column and assign it to a variable?
Try with single query
$sql = "SELECT SUM(`furniture`) AS sumFurniture,
SUM(`groceries`) AS sumGroceries ,
...
FROM `paid` ";
result can be get with
$sth = $connect->prepare($sql);
$sth->execute();
$result = $sth->fetch();
$sumFurniture = $result['sumFurniture'];
$sumGroceries = $result['sumGroceries'];
....
You can combine them as a Single SQL Query
SELECT SUM(furniture) AS furniture, SUM(groceries) AS groceries....... FROM paid
If your where clause is same you can combine the query like this:
select sum(<column_name1>) column_name1,sum(<column_name2>) column_name2 from tablename where <where>
From php, you can fetch it: using array index "column_name1","column_name2"

PHP, Mysqli, how to bind parameters from column and use their

I am trying to select num1 and num2 from different users:
$stmt = $mysqli->prepare('SELECT `num1`, `num2` FROM `Mytable1` WHERE `user` in (SELECT DISTINCT `user` from `Mytable1` where type=2)');
$stmt->execute();
$stmt->bind_result($num1, $num2);
$stmt->store_result();
$stmt->fetch();
$stmt->close();
And I need get results $result = $num1 * $num2;
Then enter the result in Mytable2
$stmt = $mysqli->prepare('UPDATE `Mytable2` SET `num`=? WHERE `name` in (SELECT DISTINCT `user` FROM bookmaker_bets WHERE type=2)');
$stmt->bind_param('i', $result);
$stmt->execute();
$stmt->close();
How to make that num1 and num2 selected from each user, and not first user?
You can perform this update with one SQL statement:
UPDATE table2
INNER JOIN users ON users.userId = table2.userId
INNER JOIN table1 ON table1.userId = users.userId
SET table2.result = table1.num1 * table1.num2
WHERE users.type = 2;
Please, refer to this fiddle

SQLite: last_insert_rowid() in same INSERT statement

I running this query in PHP/SQLite 3 (PDO)
Scenario: a new driver is inserted into drivers table and an existing car is immediately linked to him:
DRIVERS
driver_id [PK]
driver_name
CARS
car_id [PK]
fk_driver_id [FK]
$qr = "INSERT INTO drivers (driver_name) VALUES ('{$_GET['driver_name']}'); COMMIT; UPDATE cars SET fk_driver_id=( SELECT last_insert_rowid() ) WHERE car_id={$_GET['car_id']};";
$stmt = $dbh->prepare($qr);
$result = $stmt->execute();
It inserts the driver but does not UPDATE the cars table and produces no error either.
It works if I use the same query using SQLite Spy.
In PHP it will only if I break it in two parts:
$qr = "INSERT INTO drivers (driver_name) VALUES ('{$_GET['driver_name']}'); COMMIT; ";
$stmt = $dbh->prepare($qr);
$result = $stmt->execute();
$qr = "UPDATE cars SET fk_driver_id=( SELECT last_insert_rowid() ) WHERE car_id={$_GET['car_id']};";
$stmt = $dbh->prepare($qr);
$result = $stmt->execute();
What is wrong in the PHP code if it won't work in one single statement?
Try this way :
$qr = "INSERT INTO drivers (driver_name) VALUES ('{$_GET['driver_name']}'); ";
$stmt = $dbh->prepare($qr);
$result = $stmt->execute();
$lastId = $dbh->lastInsertId();
$dbh->commit();
$qr = "UPDATE cars SET fk_driver_id=? WHERE car_id={$_GET['car_id']};";
$stmt = $dbh->prepare($qr);
$result = $stmt->execute(array($lastId));

PHP PDO search for a value in two or more columns using one string

When I want to find a value from a row using PDO I use the following method:
//Search whether user exists
$sqlQueryEmailLogin = $dbh->prepare("SELECT vendor_id, first_name, last_name, email_login, user_password, passport_id, login_attempts, login_last_attempt FROM $tableVendorDetails WHERE email_login = ?");
$sqlQueryEmailLogin->bindValue(1, $emailLogin);
$sqlQueryEmailLogin->execute();
and the following PHP code for the search field
$emailLogin = 'xyz#abc.com'
Now I'd like to search two columns or more and use the following code
$sql = "SELECT * FROM articles WHERE id = ? AND status = ?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1, $id);
$stmt->bindValue(2, $status);
$stmt->execute();
I'd like to search the two columns from a string. How should I go about it, please?
The string value i go is from a html form with one input box
I'd like a string that is capable of searching two values from a MySQL table e.g.
$search = $id; and
$seach = $status;
in this case both cancel each other
You could simplify it by using the method described by #gbestard. But you should also do this:
$search = 'asdf'; // fill this with your form input
$sql = "SELECT * FROM articles WHERE id = :id OR status = :status";
$stmt = $conn->prepare($sql);
$stmt->execute(array(
':id' => $search,
':status' => $search,
));
Notice the change to OR in the query, and supplying the $search multiple times...
That's what I'm using
$sql = "SELECT * FROM articles WHERE id = :id AND status = :status";
$stmt = $conn->prepare($sql);
$stmt->execute(array(':id' => $id , ':status' => $status));
Try the following
$sql = "SELECT * FROM articles WHERE id = :id AND status = :status";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':id', $id);
$stmt->bindValue(':status', $status);
$stmt->execute();
See docs http://php.net/manual/en/pdostatement.bindvalue.php
You should use OR instead of AND. That way, you will get all rows that match either by id or by status.
SELECT * FROM articles WHERE id = ? OR status = ?

Categories