PHP, How to display the 2nd table - php

DBOperations.php
public function getByQRID($id){
$stmt = $this->conn->prepare("SELECT students.*, courses.* FROM students INNER JOIN courses ON courses.id = students.course_id WHERE students.id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
Update the code

If you are you using MySql, then try this:
select * from Student
inner join Course on Student.course_id = Course.id
where Student.id = 1
(1 it's an example, and I'm guessing things, since your question wasn't entirely clear).
I hope it helps!

Related

php like concat get userid with username

I am creating PHP user details display script I have added userid to Like Conact method but i want to add userid and username to it's two parameters
Here is my code
if(isset($_GET["user_id"]))
{
$uid = $_GET["user_id"];
}
if ($stmt = $con->prepare("SELECT p.*, c.cat_title,u.username,u.profile_pic,u.
content_details,u.web_link,u.fb_page,
u.github,u.twit_link,title,desc1,time,img,
views,id FROM products AS p
JOIN users AS u ON u.user_id= p.user_id
JOIN category AS c ON c.cat_id = p.cat_id
WHERE u.user_id LIKE CONCAT('%',?,'%') ")){
$stmt->bind_param("s", $uid);
$stmt->execute();
}
WHERE u.user_id LIKE CONCAT('%',?,?,'%')")){
$stmt->bind_param("ss", $uid, $username);
Is this what you were asking for? Based on your question this would do it. If you're trying to do LIKE by either or then you'll have to use OR and do another LIKE comparison.

Combine MySQL SELECT queries with IF statement

I want to combine my two SELECT statements with an if statement. Currently I am just getting the row and then doing if($row['activated'] === 1) { ... run 2nd select statement }.
There has to be a better and much faster way of doing it (performance-wise). This is what I'm trying to do:
For example: (1st SELECT statement)
$db = $stmt->("SELECT * FROM users WHERE id = :id AND activated = 1");
$db->bindParam(':id', $id);
$db->execute();
If that above SELECT statement is true, then do this: (2nd SELECT statement)
$db = $stmt->("SELECT * FROM user_settings WHERE user_id = :user_id");
$db->bindParam(':user_id', $id);
$db->execute();
How can I combine those two SELECT statements? If it is else or if activated = 0, then only do the first select.
Bonus question:
How can I make it three If statements? For example, the 2nd SELECT statement will be like this:
$db = $stmt->("SELECT * FROM user_settings WHERE user_id = :user_id AND color = :color");
$db->bindParam(':user_id', $id);
$db->bindParam(':color', $color);
$db->execute();
Now if that statement is true, run this 3rd SELECT statement:
$db = $stmt->("SELECT * FROM friends WHERE user_id = :user_id");
$db->bindParam(':user_id', $id);
$db->execute();
You can merge/combine
SELECT * FROM user_settings WHERE user_id = :user_id AND color = :color
and
SELECT * FROM friends WHERE user_id = :user_id
Into one query with a JOIN
SELECT
*
FROM
user_settings
INNER JOIN
friends
ON
user_settings.user_id = friends.user_id
AND
user_settings.color = :color
Or a other JOIN format
SELECT
*
FROM
user_settings
INNER JOIN
friends
ON
user_settings.user_id = friends.user_id
WHERE
user_settings.color = :color
Or you can write it as this JOIN. (most likely slower)
SELECT
*
FROM (
SELECT
*
FROM
user_settings
WHERE
user_settings.color = :color
) AS user_settings
INNER JOIN
friends
ON
user_settings.user_id = friends.user_id
The used method which is best/faster can/will be depending which indexes you have in use.

Select and unlink file on 3 tables

Please help me to check my query. I have search a lot and I have'nt try to select 3 tables before.
I think I got it right but I dont know why there's nothing happen.
public function delSection($delete_id)
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt->execute(array(":del_id"=>$delete_id));
while($linkRow=$stmt->fetch(PDO::FETCH_ASSOC))
{
unlink(__DIR__."/Admin/cover_images/".$linkRow['sec_cover']);
unlink(__DIR__."/Admin/Files/".$linkRow['sec_id']."/".$linkRow['file_name']);
rmdir(__DIR__."/Admin/Files/".$linkRow['sec_id']);
}
$stmt2 = $this->conn->prepare("DELETE tbl_section, tbl_login, tbl_content FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt2->bindparam(":del_id",$delete_id);
$stmt2->execute();
return true;
}
What I am trying to do is to select * from 3 tables and fetch their data with fk sec_id
here's the manual running of query
link:
Code:
Done With LEFT OUTER JOIN QUERY
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
LEFT OUTER JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
LEFT OUTER JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:unlink_id");

Inner Join more than 2 tables (5 tables)

Before you guys send me to something else because there is already something like this answered. I don't quite understand the examples given there.
function joinBestelling (){
$sql =( "SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}
so i have two tables now artikel and factuurregel how do i add like 3 more tables with info in the join ?
Simply join the outher tables like the first one:
SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode
INNER JOIN table3 on .....
INNER JOIN table4 on .....
Hopefully that was your question
function joinBestelling (){
$sql =( "SELECT factuurregel.factuurNummer, factuurregel.artikelCode, factuurregel.aantal, factuurregel.prijs, factuur.factuurNummer, factuur.factuurDatum, factuur.klantCode, klant.klantCode, klant.voorLetter, klant.achterNaam, artikel.artikelCode, artikel.artikel, artikel.prijs FROM factuurregel JOIN factuur ON factuurregel.factuurNummer = factuur.factuurNummer JOIN klant ON factuur.klantCode = klant.klantCode JOIN artikel ON factuurregel.artikelCode = artikel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}

php mysql select something and get columns from other tables

I'm using prepared statements and I need to "select" other table, apart from these two, to get data but I get this:
Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\views\user\referral.php on line 16
If I add in SELECT table1.* , table.* , "theothertable.*"
$stmt = $mysqli->prepare("SELECT friends.*, rc_usuario.* // or just *
FROM friends
INNER JOIN rc_usuario ON rc_usuario.id = friends.friendID
WHERE friends.userID = ?");
$stmt->bind_param('s', $connectedUserID);
This is working fine, I get what i need, but I also need to get data from another table and I can't make other select because i need it all in a while to print all the data together.
The question is, can I SELECT something like that from 2 tables and also get data from other table/s?
Thank YOU!
EDIT: Add the new statement:
if ($stmt = $mysqli->prepare("SELECT friends.*, members.*, account_type.*
FROM friends
INNER JOIN members ON members.id = friends.friendID
INNER JOIN account_type ON account_type.name = members.acc_type
WHERE friends.userID = ? AND members.acc_type = ?")) {
$stmt->bind_param('is', $connectedUserID, $connectedAcc_type);
$stmt->execute();
} else echo $mysqli->error;
You can join more tables by using another INNER JOIN, like as follows;
INNER JOIN rc_usuario ON rc_usuario.id = friends.friendID
INNER JOIN rc_another ON rc_another.col = friends.coljoin
Just make sure you select all the columns you want in the joined table.
It might also help to run your prepare statement in an if, like this;
if($stmt = $mysqli->prepare("SELECT ...")) { // ... where the rest of your query is
$stmt->bind_param('s', $connectedUserID);
$stmt->execute();
}
else {
echo $mysqli->error;
}
which will give you an idea of any problems with the SQL syntax.
Hope this helps.

Categories