SQL query to return mixed tables - php

Hello I have the following url
http://www.test.nl/test.php?itemnr=123
In my database in table items2 my columns are:
itemnr | itemId | Description | etc
Because I want to show the itemnr AND the itemId on this page I have this query:
<?php
$itemnummer = intval($_GET['itemnr']);
$itemIDnummer = "SELECT DISTINCT itemId from items2 where itemnr = '" .$itemnummer. "'";
$resultaat = mysql_query($itemIDnummer) or die(mysql_error());
echo "$resultaat";
?>
Can anyone see my fault?

Add so that your code looks like this
<?php
$itemnummer = intval($_GET['itemnr']);
$itemIDnummer = "SELECT DISTINCT itemId from items2 where itemnr = '" .$itemnummer. "'";
$resultaat = mysql_query($itemIDnummer) or die(mysql_error());
$fetchaat = mysql_fetch_assoc($resultaat);
echo $fetchaat["itemId"];
?>
Updated Question:
Because I want to show the itemnr AND the itemId on this page I have this query:
<?php
$itemnummer = intval($_GET['itemnr']);
$itemIDnummer = "SELECT DISTINCT itemId, itemnr from items2 where itemnr = '" .$itemnummer. "'";
$resultaat = mysql_query($itemIDnummer) or die(mysql_error());
$fetchaat = mysql_fetch_assoc($resultaat);
echo $fetchaat["itemId"];
echo $fetchaat["itemnr"];
?>

Related

SQL - Displaying names instead of ID [duplicate]

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;

php mysql query is not doing sum properly

I am doing sum with the below query but it is not giving result properly.
If there are four items and it is showing the result like:
1.000 2.000 3.000 4.000 and it should be like 10.000
I don't know where I am mistaken please help.
<?php
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTC = $torder['item_id'];
$qtyT = $torder['qty'];
$chTP = mysql_query("
select * from temp_choices
where item_id = '".$prITTC."'
AND ses_mem = 113
AND status = 1
");
while($chGET = mysql_fetch_array($chTP)){
$fID = $chGET['id'];
$field = $chGET['choice_id'];
$order_tempCHP = mysql_query("
select sum(price) as total, id, ename, choice_id, item_id, price
from choice_price
WHERE
id IN('$field')
");
while ($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
}
?>
according to my question above after trying and trying i found the solution and resolved my problem according to below code:
Thanks to #John Kugelman at MySQL query using an array
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTD = $torder['id'];
$prITTC = $torder['item_id'];
$chTPaa = mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa = mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist = implode(",",$temp);
$order_tempCHP = mysql_query("
select sum(price) as total
from choice_price
WHERE
id IN ($thelist)
AND
item_id = '".$prITTC."'
");
while($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }

How to show the result form getting array?

Database :
--> product table
P_id P_name P_uploadKey
1 Cemera 7365
2 Notebook 7222
3 Monitor 7355
4 Printer 7242
--> buy table
B_id P_id B_name date
1 1,3,4 somchai 12/3/2016
2 2,3 kri 12/3/2016
This sql to show the find id on buy table where $_GET['B_id'] = '2' :
$bid = $_GET['B_id'];
$sqlB ="select * from buy where B_id ='$bid' ";
$Recordset2 = mysql_query($sqlB, $connect) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
And this sql code to show the result what is they buy, by get the $row_Recordset2['P_id'] like a 2,3 from code above :
$pid = $row_Recordset2['P_id'];
$sqlp ="select * from buy where P_id ='$pid' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
I want the to show like this, how we edit it:
Notebook
Monitor
This is answer i can do it.
$pid = $row_Recordset2['P_id'];
$array = explode(',', $pid);
foreach ($array as $item) {
$sqlp ="select * from buy where P_id ='$item' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
}
You can use like below,
$sqlp ="select * from product where P_id IN '($pid)'";
instead of
$sqlp ="select * from buy where P_id ='$pid' ";

i have difficulties with the sql part

I posted a code below about my website. In this code i want to update rows in my database, if the user changed the name of the topic on the website's form. Everything is working except the sql part. I mean the part where:"LIMIT 1 OFFSET '$x'" this part of the sql code is not good for some reason, but i don't know why. I tested it in xampp phpmyadmin and it works but here something just wrong.
<?php
$sql = "SELECT topicname, username, created, COUNT(commentid)
FROM user, topic, comment
WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner
AND user.username = '" . $_SESSION['user_name '] . "'
GROUP BY topicname ";
$lekerdezes = mysql_query($sql);
$num_rows = mysql_num_rows($lekerdezes); ?>
<?php
if (isset($_POST['delete']))
{
if (!empty($_POST['forumnev']))
{
for ($x = 0; $x < $num_rows; $x++)
{
foreach ($_POST['forumnev'] as $selected)
{
$seged = mysql_query("SELECT created FROM topic WHERE
created IN (SELECT created FROM user, topic, comment WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner AND user.username = '" . $_SESSION['user_name '] . "'
GROUP BY topicname ORDER BY created)
LIMIT 1 OFFSET '$x'");
if (!$seged)
{
echo mysql_error();
}
$seged2 = mysql_fetch_array($seged);
$seged2 = $seged2[0];
if (!$seged2)
{
echo mysql_error();
}
$sql = mysql_query("UPDATE topic SET topicname = '$selected' WHERE created = '$seged2'");
}
}
header("Location: topicedit.php");
}
}
?>
Try updating as follows:(Hope your limit: 1 and offset: $x)
$seged = mysql_query("SELECT created FROM topic WHERE created IN (SELECT created
FROM user,topic,comment
WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner
AND user.username = '". $_SESSION['user_name'] ."'
GROUP BY topicname
ORDER BY created)
LIMIT $x, 1");

PHP - Get data from database then fetch it again

I am trying to get data from database then fetch it again with different mysql_query using while() in both query , but the problem it is producing results more than one time because i used while in first query . So any answer to get all data without while() for first query ?
$AllFrnd = "SELECT friend , followers FROM frndlist WHERE userid = '".$_SESSION[' user_id ']."' ORDER BY id DESC";
$getfrnd = mysql_query($AllFrnd);
while($frnd = mysql_fetch_array($getfrnd)) {
$query2 = "SELECT * FROM post WHERE (userid ='".$frnd['friend']."') OR (userid = '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC";
$rs = mysql_query($query2);
while($row = mysql_fetch_array($rs)) {
echo ''.$row['content'].'';
you can try this :
// Associative array
$frnd =mysql_fetch_array($getfrnd,MYSQL_ASSOC);
then get your data as like :
echo $frnd ["friend"]
echo $frnd ["followers"]
And you should use mysqli_fetch_array instead of mysql_fetch_array
Hope it helps
You can use a query just like this :
SELECT *
FROM post
WHERE userid IN (
SELECT followers
FROM frndlist
WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid IN (
SELECT friend
FROM frndlist
WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid = '" . $_SESSION['user_id'] . "'
ORDER BY id DESC
You can easyly handle by using this function
function sel($table,$field="*", $condition="1",$sort="" ){
if($sort!='') $sort="order by $sort ";
//echo "select $field from $table where $condition $sort ";
$sel_query=mysql_query("select $field from $table where $condition $sort ");
//$sel_result=array();
while($temp_res=#mysql_fetch_array($sel_query))
{
$sel_result[]=$temp_res;
}
return isset($sel_result)?$sel_result: 0;
}
while($frnd = mysql_fetch_array($getfrnd)) {
$temp_res=sel("post","*"," (userid ='".$frnd['friend']."') OR (userid = '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC");
if($temp_res)foreach($temp_res as $row){
echo $row['content'];
}
}

Categories