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;
Related
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 ?
Somewhere you have patience for takes you somewhere, only now the last problem.
It works for one party, only for the persons who not have any recipies, the peron with recipies can't see it, it only see a blank รณ page.
$usersi = $dbh->prepare('SELECT * FROM recettes WHERE id_user = :id');
$usersi->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$usersi->execute();
$usersis = $usersi->fetchAll(PDO::FETCH_ASSOC);
For a user with id = 1:
<?php
$usersi_sql = $dbh->prepare('SELECT * FROM recettes WHERE id_user = 1');
$usersi_sql->execute();
$usersi = $usersi_sql->fetchAll(PDO::FETCH_ASSOC);
?>
The solution on my question is:
$usersi_sql = $dbh->prepare('SELECT * FROM recettes WHERE id_user = :id');
$usersi_sql->bindParam(':id', $ma['id'], PDO::PARAM_INT);
$usersi_sql->execute();
$usersi = $usersi_sql->fetchAll(PDO::FETCH_ASSOC);
if(isset($usersi[0])) {
?>
Here your HTML code
<?php
}
}else{
?>
Here your HTML else code
<?php } ?>
This in the top of your php file
$ma_sql = $dbh->prepare('SELECT * FROM users WHERE id = :id');
$ma_sql->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$ma_sql->execute();
$ma = $ma_sql->fetch();
I used this in my top because it also check if the user is logged in or not, but this above is a part of that code.
If you want all recepies linked to all users -
SELECT * FROM recettes INNER JOIN users ON recettes.id_user = users.id LIMIT 20
If you want distinct recepies linked to all users -
SELECT DISTINCT recettes.* FROM recettes INNER JOIN users ON recettes.id_user = users.id LIMIT 20
If you want just for one user -
SELECT * FROM recettes WHERE id_user = user LIMIT 20
Change PHP call to -
$usersi_sql->bindParam(':user', $user_id);
$usersi_sql->execute()
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'
How can I grab the count value from the query MySQL query below using PHP.
Here is My MySQL code.
$dbc = mysqli_query($mysqli,"SELECT COUNT(*) FROM((SELECT users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = 1
AND users_friends.friendship_status = '1')
UNION
(SELECT users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.friend_id = users.user_id
WHERE users_friends.friend_id = 1
AND users_friends.friendship_status = '1')) as friends");
using SQL_CALC_FOUND_ROWS should simplify things:
$dbc = mysqli_query($mysqli,"SELECT SQL_CALC_FOUND_ROWS users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = 1
AND users_friends.friendship_status = '1'
");
then afterwards do
$rs = mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$rec = $rs->fetch_array();
$count = $rec[0];
This method will return the number of records that match the query, ignoring any LIMIT statement, whereas using $rs->num_rows will only give you the number of records actually returned. Depends which one you want.
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
http://us.php.net/manual/en/mysqli.query.php
Assuming that you are correctly connected to the MySQL server and your query are executed correctly, you can use the following code:
$values = mysql_fetch_row($dbc);
$count = $values[0];
Your query should look like SELECT COUNT(*) as numThings FROM xxx
The numThings is what you will reference in PHP:
$result = mysql_query("SELECT COUNT(*) as `numThings` FROM xxx");
$row = mysql_fetch_assoc($result);
$count = $row['numThings'];