I want to get data from another table by just calling if the ID and data from another table is the same.
Table_1
Id | name | lastname
1 | Fred | Moore
Table 2
Id | table1_id
1 | 1
I can already get and store the table 1 id to my table 2, but i want to echo the name and lastname from the table 1.
e.g. if table 2 table1_id is equal to Table_1 Id it will print the name and lastname .
Try this query
select table_1.name, table_1.lastname
from table_1
left join on table_2 on table_1.id = table_2.table1_id
<?php
$a = "Table_1";
$b = "Table_2 ";
$allItem = $cxn->query("SELECT *
FROM $a
INNER JOIN $b
ON $a.id = $b.id
WHERE $a.id = 1);
$allItem = $allItem->fetchAll();
$lastName = $allItem["lastname"];
$name = $allItem["name"];
?>
Maybe it could work!
<?php
$conn = mysqli_connect("localhost","username","password","database_name");
if(!$conn){
echo "Database Connection Failed!";
}
$query1 = mysqli_query($conn,"SELECT table1_id,first_name,last_name FROM table_1")or trigger_error(mysqli_error($conn));
if(mysqli_num_rows($query1)){
while($row1 = mysqli_fetch_array($query1)){
$table1_id = $row1['table1_id'];
$table1_first_name = $row1['first_name'];
$table1_last_name = $row1['last_name'];
$query2 = mysqli_query($conn,"SELECT table2_id,table1_id,first_name,last_name FROM table_2 WHERE table1_id='$table1_id' ORDER BY first_name,last_name ASC")or trigger_error(mysqli_error($conn)); //check table1_id from table2 AND table1_id from table1 if matched
$row2 = mysqli_fetch_array($query2);
$table2_first_name = $row2['first_name'];
$table2_last_name = $row2['last_name'];
echo "First Name: ".$table2_first_name."<br>"; //print first name
echo "Last Name: ".$table2_last_name."<br>"; //print last name
}
}
else{
echo "No Result Found.";
}
?>
Related
This question already has answers here:
Select Name instead OF ID in table with ID-Ref Column SQL
(2 answers)
Show Name Instead of ID from Different Table
(2 answers)
Closed 1 year ago.
I have 2 tables
rankID | name
1 | new
2 | learner
3 | experienced
4 | pro
And another with all the user info and passwords and stuff
id | username | rankID
1 | hello | 3
2 | hey | 3
I have come so far so I can display their rank number, but I want to display the rank name. How can I do that? I have tried a lot of things but I'm not so good at sql and the php part of it.
This is the code I use to display the rank number
//Get rankID
$query = "SELECT rankID FROM users WHERE id = '$userId'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$rank = $row['rankID'];
And to display the rank number:
Rank: <?php echo $rank; ?>
Simple JOIN query :-
"SELECT rank.name as rank_name,users.rankID as rankID from users LEFT JOIN rank ON rank.rankID = users.rankID WHERE id = '$userId'"
And then After:-
$query = "SELECT rank.name as rank_name,users.rankID as rankID from users LEFT JOIN rank ON rank.rankID = users.rankID WHERE id = '$userId'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
Do:-
$rank = $row['rankID'];
$rank_name = $row['rank_name'];
Rank: <?php echo $rank; ?>
RankName: <?php echo $rank_name; ?>
Or
$rank_data = $row;
Rank: <?php echo $rank_data['rankID']; ?>
RankName: <?php echo $rank_data['rank_name']; ?>
Not:- lot of other possible ways are there which are listed by other programmers in comment and answer as well.
//Get datas
$query = "SELECT rankID, name FROM users WHERE id = '$userId'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$rank = $row['rankID'];
$rank = $row['name'];
And to display the datas:
Rank: <?php echo $rank; ?>
Name: <?php echo $name; ?>
Hope so this should make a trick for you.
$query = "SELECT rankID FROM users WHERE id = '".$userId."'";
$result = $conn->query($query);
$count = $result->num_rows;
if($count==0)
{
return false;
}
else
{
$rows=[];
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
}
Please use below code
$query = "SELECT name FROM users as u JOIN rank as r ON r.rankID = u.rankID WHERE u.id = '$userId'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
Name: <?php echo $name; ?>
When you want to get data from two different table.You need join query.
Here is your query which will solve your proble definitely :
$q="select a.name,b.rankID from rankname as a INNER JOIN user as b
ON a.rankID = b.rankID";
For more know about How to join two tables see this:http://www.tutorialspoint.com/sql/sql-using-joins.htm
Hope this will help you better.
Please try this
//Get rankID
$query = "SELECT r.name as rank_name FROM rank as r inner join users as u on r.rankID = u.rankID WHERE u.id = '$userId'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$rank = $row['rank_name'];
echo 'Rank: '. $rank;
try this:
//Get rankID
$query = "SELECT rankID, rank.name AS rank_name FROM rank, users WHERE id = '$userId' and users.rankid = rank.rankid";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$rank = $row['rank_name'];
echo $rank;
i have the following code.
i already try'd some sulotions from the search option, but nothing helped.
all tables have a row called: user_id with data in it, but still the query gives false
The $user variable has the value 29 and in my tables look like this:
+---------+---------+-------+-----------+
+ id | user_id | jaar + kenmerk |
+---------+---------+-------+-----------+
+ 1 | 29 | 2015 + standaard |
+---------+---------+-------+-----------+
$query = "SELECT buitenklimaat.user_id, kasklimaat.user_id, watermangement.user_id, energie.user_id FROM buitenklimaat, kasklimaat, watermangement, energie WHERE user_id = ".$user." AND kenmerk = 'standaard' AND jaar = ".date("Y")."";
$result = mysqli_query($conn, $query);
if($result === false) {
echo 'Query failed';
die();
}
else {
// do something
}
If the field user_id exists in more than one table, you need to prefix the field with the table name in your where clause, too. Otherwise your database will not know in which table it should look for the user_id field. But even after you fixed this error the query will probably not return what you want it to. Have a look at some SQL tutorials that cover the join syntax.
$query = "SELECT buitenklimaat.user_id, kasklimaat.user_id, watermangement.user_id, energie.user_id FROM buitenklimaat, kasklimaat, watermangement, energie WHERE <TABLENAME MISSING>.user_id = ".$user." AND kenmerk = 'standaard' AND jaar = ".date("Y")."";
You may need to join all those tables together to get the user_id condition to work properly:
$query = "SELECT buitenklimaat.user_id, kasklimaat.user_id,
watermangement.user_id, energie.user_id FROM buitenklimaat, kasklimaat,
watermangement, energie
WHERE buitenklimaat.user_id = kasklimaat.user_id
AND buitenklimaat.user_id = watermangement.user_id
AND buitenklimaat.user_id = energie .user_id
AND buitenklimaat.user_id = ".$user." AND kenmerk = 'standaard'
AND jaar = ".date("Y")."";
table 1
two column >> paid - order_num1
table 2
two column >> order_num2
I want get order_num2 value from table 2 and
update paid(insert paid = 1) in table one with same order_num value
If order_num1=order_num2 then paid = 1 in table 1
$q = mysql_query("select order_num2 from table2 where samevalue = samevalue ");
$x = mysql_fetch_row($q);
mysql_query("update table1 set paid=1 where order_num1='$x['order_num2']'");
But it does not work!
First get from one table and update paid from another table if order_num have same value
Try
$table2 = mysqli_query("SELECT * FROM table2");
$row = mysqli_fetch_array($table2);
$num2 = $row['order_num2'];
$table1 = mysqli_query("SELECT * from table1");
$roww = mysqli_fetch_array($table1);
$num1 = $row['order_num1'];
if ($num2 == $num1) {
$updateDB = mysqli_query("UPDATE table1 SET paid = 1 WHERE order_num1 = '$num2'");
} else {
//Not equal so the paid column wont get updated
}
I have a URL that is dynamically generated:
https://mywebsite.co.uk/report.php?taskid=25&rep=1&rep=2&rep=3
I can retrieve the variable for the task ID and rep fine:
if (isset($_GET['taskid'])) { $taskid = $_GET['taskid']; }
if (isset($_GET['rep'])) { $referenceID = $_GET['rep']; }
What I'm trying to do is create an SQL statement on the page that selects a row based on the rep number in the URL. For example:
SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID = $referenceID
However, when I echo the result of $referenceID it is always the last rep, so in this case 3. How do I select the rows from the database where ID = 1,2 & 3?
I then want to display each row, so it would be something like:
<table>
$result = mysqli_query($con,"SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID = $referenceID");
while($row = mysqli_fetch_array($result))
{
$ID = $row['ID'];
$NAME = $row['NAME'];
print "<tr><td>$ID</td><td>$NAME</td></tr>";
}
</table>
This query should return 3 rows in the table with the ID AND NAME in each row.
Your help would be appreciated.
First, you need to change your parameter name from rep to rep[]
This will cause PHP $_GET['rep'] to return an array.
Then you need to implode the array to obtain a string with commas:
if (isset($_GET['rep'])) {
$referenceID = implode(',',$_GET['rep']);
}
You have to change your SQL syntax to this:
SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID IN ($referenceID)
try this query
$result = mysqli_query($con,"SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND (ID <= $referenceID and ID>= 1)");
You should use array-style URL parameters, e.g.
https://mywebsite.co.uk/report.php?taskid=25&rep%5B%5D=1&rep%5B%5D=2&rep%5B%5D=3
Then $_GET['rep'] will be an array, and you can do:
$referenceIDs = implode(',', array_map(array($con, 'real_escape_string'), $_GET['rep']));
$sql = "SELECT TASK_ID, ID, NAME
FROM mytable
WHERE TASK_ID = $taskid
AND ID IN ($referenceIDs)";
I have two table, table2 has nameColumn that include name of table1 column like
table 1
id|check|status|...|
1 |abc |1 |...|
table2
|nameColumn|...|
|check |...|
|status |...|
I want get value in table1 with column name is value in table2 like
$sql="SELECT * FROM `table1` WHERE `id` = 1 ";
$result = mysqli_query($conn,$sql) or die (mysql_error ());
$rowTable = mysqli_fetch_array($result);
$s = "SELECT * FROM table2";
$r = mysqli_query($conn,$s);
while($row1 = mysqli_fetch_array($r)){
$columnName = $row1['nameColumn'];
$value = $rowTable["'".$columnName."'"]; // fail why?
//$value = $rowTable['check']; // working?
}
I don't know why $value = $rowTable["'".$columnName."'"]; fail the error like
Notice: Undefined index: 'check' ...
But if i using direct like $value = $rowTable['check']; // working why?
How to fix that thanks
its failing because your adding unnecessary ' char to it. try the code below
$value = $rowTable[$columnName];
$value = $rowTable['".$columnName."'];