this is my query:
$query = $db->prepare("
EXPLAIN SELECT transaction.id, transaction.dateCreated, transaction.dateUpdated, transaction.brandId, transaction.clientId, transaction.serviceId, transaction.currencyCode, transaction.type, transaction.token, transaction.amount, transaction.reason, transaction.referenceOrderId, transaction.serviceTransactionId, transaction.chainMode, transaction.bonusCode, transaction.codeId, transaction.codeMessage, su.name AS user_name, su.surname AS user_surname, b.name AS brand_name, su.email_address, su.player_id, sandbox, integrationId
FROM transaction_AstroPay transaction
LEFT JOIN tokens t ON transaction.token = t.token
LEFT JOIN sys_users su ON su.id=t.user_id
LEFT JOIN brands b ON b.api_brand_code = transaction.brandId
WHERE transaction.clientId = :clientId
");
$query->bindParam(':clientId',$clientId,PDO::PARAM_INT);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
The query is performed well and the data is fetched from the DB.
My question is how can I get detailed information about how much time it took. In mysqli the EXPLAIN SELECT was working.
We took the information in mysqli like this:
$sql = 'EXPLAIN SELECT * FROM table WHERE condition = "condition"';
$result = $mysqli->query($sql);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];
How can i do it here in PDO ?
Related
My main goal is to get certain columns (student: last, first, middle names; section name; and current grade level) from multiple tables by joining them, mostly getting data on the 'students' table and getting other necessary data through foreign keys on the other tables ('g7/8/9/10_performance', and 'sections'). They will then be stored in an array and such.
MySQL Query:
SET #grade_table = (SELECT CONCAT('g', student_currentGrade, '_performance') FROM students);
SET #stmt = CONCAT('SELECT students.student_last, students.student_first, students.student_middle, students.student_currentGrade, sections.section_name
FROM ((students LEFT JOIN (', #grade_table ,' INNER JOIN sections ON ', #grade_table,'.section_id = sections.section_id) ON ',#grade_table,'.student_id = students.student_id))');
PREPARE stmt1 FROM #stmt;
EXECUTE stmt1;
I used CONCAT() function to utilize the variable #grade_table as a student database will most likely contain students of differing grade levels.
Attempted PDO query inside a class function:
protected function getRecords() {
$sql =
"SET #grade_table = (SELECT CONCAT(\'g\', student_currentGrade, \'_performance\') FROM students);"
;
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql =
"SET #stmt = CONCAT(\'SELECT students.student_last, students.student_first, students.student_middle, students.student_currentGrade,
sections.section_name FROM ((students LEFT JOIN (\', #grade_table ,\' INNER JOIN sections ON \', #grade_table,\'.section_id = sections.section_id)
ON \',#grade_table,\'.student_id = students.student_id))\');"
;
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql = "PREPARE stmt1 FROM #stmt;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql = "EXECUTE stmt1;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
if($stmt->rowCount() == 0){
$stmt = null;
header('location: ../performances.php?error=not_found');
exit();
}
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
I tried using the "create PHP" option in the XAMPP phpmyadmin interface to maybe help things hence the slashes on the $sql declarations. But, the function returns nothing (in this case it changes the header to "../performances.php?error=not_found").
Controller Class function that calls the above function:
public function loadRecords(): array
{
$records = $this->getRecords();
$data = [];
while($row = $records){
$data[] = $row;
}
return $data;
}
I do understand that I made the code like how Frankenstein made his monster, so I'll understand if I need better organization of data in the database.
I would rewrite your sql to (but I also agree on most of the comments on your question 😉):
SELECT
students.student_last,
students.student_first,
students.student_middle,
students.student_currentGrade,
sections.section_name
FROM students
LEFT JOIN g7_performance g7 ON g7.student_id = students.student_id
AND students.student_currentGrade=7
LEFT JOIN g8_performance g8 ON g8.student_id = students.student_id
AND students.student_currentGrade=8
LEFT JOIN g9_performance g9 ON g9.student_id = students.student_id
AND students.student_currentGrade=9
LEFT JOIN g10_performance g10 ON g10.student_id = students.student_id
AND students.student_currentGrade=10
INNER JOIN sections ON sections.section_id = COALESCE(g10.section_id, g9.section_id, g8.section_id, g7.section_id) ;
This assumes that a student is only in one of the 'g7/8/9/10_performance' tables.
I'm starting to learn php. I'm able to extract the list of CD from the SQL query in [in this table][1] but there's another query which contains the Category which is linked via ID in this table.
How do I get all the columns in the same result set using one query?
<?php
include 'database_conn.php';
$sql = "SELECT id, title, year, price FROM table_cd";
$queryresult = mysqli_query($conn, $sql)
or die (mysqli_error($conn));
while($row = mysqli_fetch_assoc($queryresult)) {
$iid = $row['id'];
$title = $row['title'];
$year = $row['year'];
$price = $row['price'];
echo "<div>
$title
</div>\n";
echo $row['year'];
echo $row['price'];
}
?>
editCDForm.php:
<?php
$code = $_GET['itemCode'];
$sql = "SELECT * FROM table_cd WHERE table_category.ID = $code
JOIN table_category ON (table_category.Desc = table_cd.ID)";
?>
What you're looking for is called a JOIN which allows you to merge the rows from two or more tables into the same result set.
You can modify your query as illustrated below, by using a LEFT JOIN of the nmc_cd table and the nmc_category table on the catID column, as the common primary attribute between them, giving you the desired result set...
$sql = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear,
nmc_cd.CDPrice, nmc_category.catDesc
FROM nmc_cd
JOIN nmc_category on nmc_cd.catID = nmc_cd.catID";
Here's a nice article that may help you visualize what a JOIN looks like in your SQL.
Syntax for joins:
SELECT * FROM table1
JOIN table2 ON (table2.colunmname = table1.columnname)
use can get the data from both tables when joining them:
$sql = "SELECT FT.CDID, FT.CDTitle, FT.CDYear, FT.CDPrice,
ST.catDesc FROM nmc_cd AS FT LEFT JOIN nmc_category as ST ON
FT.catID=ST.catID";
soo i have 2 tables.
train_information
user_train_information
When somebody submits something in a form. it get put in the train_information table and looks like this:
Now, when people are logged in and select the train from a selector. his happens in the database:
On a other page, i want the users to see a whole list of things they selected over the time. So i run a query: SELECT * FROM user_train_information WHERE user_id=user_id;
This shows me the table user_train_information
But is it posible to show the train_information where user_id = user_id ? because i want the user to show the trains he added.
EDIT:
What i have now:
function testingggg() {
$sql = "SELECT *
FROM train_information
INNER JOIN user_train_information
ON train_information.train_id = user_train_information.train_id
WHERE user_train_information.user_id = user_id";
$sth = $this->pdo->prepare($sql);
$sth->bindParam("user_id", $_GET["user_id"], PDO::PARAM_INT);
$sth->execute();
return $sth->fetchAll();
}
And i call i here:
<?php
$summary = $database->testingggg();
?>
<?php
foreach ($summary as $result){
echo $result['train_name'];
}
?>
I get the error:
This is how i use query in such case and it works. In your case there ambiguity somewhere. Also make sure you have proper value in $_GET["user_id"]. Please check
function testingggg() {
$sql = "SELECT ti.train_name, ti.train_id, uti.user_id
FROM train_information as ti
JOIN user_train_information as uti
ON ti.train_id = uti.train_id
WHERE uti.user_id = :user_id";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":user_id", $_GET["user_id"], PDO::PARAM_INT);
$sth->execute();
return $sth->fetchAll();
}
SELECT t.train_id
, t.country_id train_country_id
, t.train_name
, t.number_of_bogies
, t.number_of_axles
, t.wheel_diameter_min
, t.wheel_diameter_max
, u.user_row_id
, u.user_id
, u.country_id user_country_id
FROM user_train_information u
JOIN train_information t
ON t.train_id = u.train_id
WHERE u.user_id = :user_id;
You try this one.
SELECT * FROM train_information INNER JOIN user_train_information
ON train_information.train_id = user_train_information.train_id
WHERE user_train_information.user_id = $user_id;
SELECT *
FROM train_information
INNER JOIN user_train_information
ON train_information.train_id = user_train_information.train_id
WHERE user_train_information.user_id = user_id;
I am new to both mysql and php.
I have two tables which are 'members' and 'points'. Both of them including the column 'username'. I want to select all the values from these two tables where username= $POST[username].
So I wrote this but this is not working.
$username = $_POST['username'];
$sql = $con->prepare("SELECT *.members, *.points FROM members, points WHERE
username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And I tried this :
$sql = $con->prepare("SELECT * FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And this:
$sql = $con->prepare("SELECT *.points, *.members FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
I can't use UNION because the number of columbs are not equel in these tables.
So, Please help me what is wrong with the code? What is the proper way to select all from multiple tables.
Alias are meant to be used to specify to which table those column belong, so you need to prepend table name to your columns
SELECT * FROM members
INNER JOIN points
ON points.username = members.username
WHERE points.username = ?
You can otherwise assign an alias to your table while selecting and use them
SELECT * FROM members a
INNER JOIN points b
ON a.username = b.username
WHERE a.username = ?
You were close with this:
SELECT *.points, *.members
FROM members
INNER JOIN points ON username.points = username.members
WHERE username=?
Try this instead:
SELECT *
FROM members
INNER JOIN points ON members.username = points.username
WHERE members.username=?
check this
SELECT * FROM points,members WHERE points.username="'.$_POST['username'].'" AND members.username="'.$_POST['username'].'";
you can check this query it is very simple.
I'm trying to display a list of status updates from artists that a logged in user is following.
So far I have this:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
}
But i'm not sure how to loop through and display the returned status updates?
This isn't a strong point of mine, so any pointers would be greatly appreciated!
What prevented you from doing similar to what you'd already done for the first query? Something like follows:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
while($status_result_row = mysqli_fetch_assoc($status_result)) {
echo $status_result_row['mycol']; // This is where you know better than us
}
}
Or if those two tables artist_likes and status_updates have artist_id in common then you could just use one query with a join. (But don't know if you are asking for that).
Just for avoiding multiple query, you can use one query like this:
SELECT l.*, s.*
from artist_likes l, status_updates s
WHERE
l.artist_id = s.artist_id and
l.user_id = '1'
or
SELECT l.*, s.*
from artist_likes l
JOIN status_updates s on (l.artist_id = s.artist_id)
WHERE
l.user_id = '1'