Twice a where in a survey - php

Instead of wanting the whole table i only want one result based on a link.
This works:
$BANDID = intval($_GET['BANDID']);
$sql = "SELECT
BANDID,
NAAMBAND,
CONTACTBAND,
GENRE,
OMSCHRIJVING,
LEDEN,
PLAATS,
PRIJS,
BOEKERID,
WEBSITE,
YOUTUBE,
NOTITIES
FROM
`BANDS`
WHERE
BANDID = $BANDID";
$BANDID is get by clicking a link on a previous page, but i wanted it combined the above
$sql = "SELECT
NAAM,
BANDID,
NAAMBAND,
CONTACTBAND,
BOEKERID
FROM
`BANDS`
INNER JOIN
`adres`
WHERE
`BANDS`.BOEKERID = `adres`.id ";
but this doenst work:
$BANDID = intval($_GET['BANDID']);
$sql1 = "SELECT
NAAM,
BANDID,
NAAMBAND,
CONTACTBAND,
BOEKERID
FROM
`BANDS`
INNER JOIN
`adres`
WHERE
`BANDS`.BOEKERID = `adres`.id
WHERE
BANDID = $BANDID";
Please help me further, thks

You should use ON and combine it with WHERE like this:
$sql1 = "SELECT NAAM,BANDID, NAAMBAND, CONTACTBAND, BOEKERID
FROM `BANDS` INNER JOIN `adres`
ON `BANDS`.BOEKERID = `adres`.id
WHERE BANDID=$BANDID";
That's how you do proper joins.

WHERE `BANDS`.BOEKERID = `adres`.id WHERE BANDID=$BANDID";
replace second where with AND
WHERE `BANDS`.BOEKERID = `adres`.id AND BANDID=$BANDID";

You need to change to proper join syntax. Every join needs an ON clause and then you can put your WHERE clause below that.
$sql1 = "SELECT NAAM,BANDID, NAAMBAND, CONTACTBAND, BOEKERID
FROM `BANDS`
INNER JOIN `adres`
ON`BANDS`.BOEKERID = `adres`.id
WHERE BANDID=$BANDID";

Related

How to create IF statement in MYSQL query WHERE statement

Is this possible to be done with SQL?
I need to make a SQL selection depending if a $query is false WHERE (u.id_user = ".$userId." OR fu.id_user = ".$userId." OR ff.id_user = ".$userId.")" ELSE "$query";
That's my starting point where both conditions have to be met:
$validatedSearchData = array(
"q"=>strip_tags($_GET["q"])
);
$query= " AND a.tags LIKE ".lib::$db->qstr("%".$validatedSearchData["q"]."%");
$feed = lib::$db->GetAll("SELECT SQL_CALC_FOUND_ROWS
a.*,
u.name,
fu.id_user AS fu_user_id,
ff.id_followed_user AS ff_user_id
FROM feed AS a
LEFT JOIN userfollow AS fu ON a.id_author = fu.id_user
LEFT JOIN userfollow AS ff ON a.id_author = ff.id_followed_user
INNER JOIN user_profiles AS u ON a.id_author = u.id_user
WHERE (u.id_user = ".$userId." OR fu.id_user = ".$userId." OR ff.id_user = ".$userId.")" . $query. "
GROUP BY a.id_article
");
Change this line
$query= " OR a.tags LIKE ".lib::$db->qstr("%".$validatedSearchData["q"]."%");
replace OR in the place of AND

PHP/PDO - EXPLAIN SELECT for PDO is it possible?

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 ?

Using php if...else statement with two queries

I have two queries that count the number of data for both "artists" and "groups" in my database. I want to display a message if there is data to display for either artists or groups (or both), and if the data returns 0 for both of them then not to display anything.
I have the following code which doesn't seem to work:
<?php if (($numrowsartists==0)OR($numrowsgroups==0)) {
} else {
echo "There is information to display.";
}
?>
Below are the queries I have:
$query = "SELECT COUNT(*) FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.song_id = $id";
$res = mysql_query($query);
$numrowsartists = mysql_fetch_assoc($res);
$query = "SELECT COUNT(*) FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = $id
ORDER BY ag.group_name ASC";
$res = mysql_query($query);
$numrowsgroups = mysql_fetch_assoc($res);
Thanks in advance. I'm sure it's probably a super basic fix but I'm still very new to php and would appreciate some help.
You should getthe value frorm the row eg using alias for column name
$query = "SELECT COUNT(*) as num_artists FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.song_id = $id";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$numrowsartists = row['num_artists'];
$query = "SELECT COUNT(*) as num_groups FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = $id
ORDER BY ag.group_name ASC";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$numrowsgroups = row['num_groups'];
There are several solutions, the easiest being the following:
if($numrowsartists[0]+$numrowsgroups[0] > 0)
However, as people have said, you shouldn't use mysql_* functions anymore.
Assuming the ID is user input, you should really use prepared statements.
Also, you can handle both tests in a single query:
$stmt = $con->mysqli_prepare("SELECT COUNT(1) as `count` FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = ?");
$stmt->bind_param('i',$id);
$stmt->execute();
if($stmt->get_result()->fetch_array()[0] > 0){
...
}else{
//message that nothing was found
}

Correct Syntax on MySQL Table Joins?

I have two tables that I'm trying to join into a new table. I have a "users" table, a "tasks" table and I have a blank "user_tasks" table. I'm trying to join them into the "user_tasks" table.
Here are my tables.
users = user_id, first_name, last_name
tasks = task_id, task_description
user_tasks = ut_id, ut_user_id, ut_first_name, ut_last_name, ut_task_id, ut_task_description.
Here is my PHP & SQL query. I'm not sure what is wrong. I get an undefined index. Do I need to add hidden inputs on my HTML form? Any help would be appreciated.
<?php
if(isset($_POST['submit'])){
$user_id = $_POST['user_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$task_id = $_POST['task_id'];
$task_description = $_POST['task_description'];
$ut_id = $_POST['ut_id'];
$ut_user_id = $_POST['ut_user_id'];
$ut_first_name = $_POST['ut_first_name'];
$ut_last_name = $_POST['ut_last_name'];
$ut_task_id = $_POST['ut_task_id'];
$ut_task_description = $_POST['ut_task_description'];
$join_query = "SELECT users.user_id, users.first_name, users.last_name, tasks.task_id, tasks.task_description FROM users, tasks INNER JOIN user_tasks ON users.user_id = user_tasks.ut_id INNER JOIN user_tasks ON tasks.task_id = user_tasks.ut_id INSERT INTO user_tasks (ut_user_id, ut_first_name, ut_last_name, ut_task_id, ut_task_description) VALUES ($user_id, $first_name, '$last_name', '$task_id', $task_description) WHERE ('$first_name' = '$ut_first_name', '$last_name' = '$ut_last_name', $task_id = $ut_task_id, '$task_description' = '$ut_task_description') ";
$result = mysqli_query($connection, $join_query);
}
?>
I think the table('user,tasks') in your select is wrong,maybe the table is 'user_tasks'
You have two concatenated queries,select + insert
, try this:
1- insert + select separate:
$join_query = "SELECT users.user_id, users.first_name,
users.last_name, tasks.task_id,
tasks.task_description FROM users
INNER JOIN user_tasks ON users.user_id = user_tasks.ut_user_id
/*users.user_id = user_tasks.ut_id*/
INNER JOIN tasks ON task.task_id=user_task.ut_task_id";
$result = mysqli_query($connection, $join_query);
AND Insert After,Insert does not use where:
$insert_query="INSERT INTO user_tasks (ut_user_id, ut_first_name, ut_last_name,
ut_task_id, ut_task_description)
VALUES ($user_id, '$first_name', '$last_name',
'$task_id', '$task_description')";
mysqli_query($connection,$insert_query) or die(mysqli_error($connection));
2- INSERT with SELECT:
$query="INSERT INTO user_tasks (ut_user_id, ut_first_name, ut_last_name,
ut_task_id, ut_task_description)
SELECT users.user_id, users.first_name,
users.last_name, tasks.task_id,
tasks.task_description FROM users
INNER JOIN user_tasks ON users.user_id = user_tasks.ut_user_id /*users.user_id = user_tasks.ut_id*/
INNER JOIN tasks ON task.task_id=user_task.ut_task_id";
mysqli_query($connection,$query) or die(mysqli_error($connection));

get information from other table where id=id

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;

Categories