I am attempting to get a 'total weight' for items a character is carrying. I am doing this by selecting the quantity of items in the characteritem table and comparing the weight which is set in the item table and they are joined by the iid (item id) column
I have tried a lot of different methods to no avail so I have looked up join statements. The issue I have is why the $result would return bool(false) and then how I can get the weights to add up afterwards.
Here is the code I am working with currently:
$sql = "SELECT *
FROM `characteritem` WHERE `owner` = '$user'
INNER JOIN item ON characteritem.iid=item.iid";
$result = $db_conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$iid = $row['characteritem.iid'];
$quantity = $row['characteritem.quantity'];
$itemweight = $row['item.itemweight'];
$itemtotal = $itemweight + $itemweight;
echo $itemtotal;
}
}
var_dump($result);
SQL is working fine now. I have got the weight and quantity and gotten the individual results. How would I get the values to add together for $itemtotal?
$sql = "SELECT *
FROM `characteritem`
INNER JOIN item ON characteritem.iid=item.iid
WHERE `owner` = '$user'";
$result = $db_conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$iid = $row['characteritem.iid'];
$quantity = $row['quantity'];
$itemweight = $row['itemweight'];
$itemtotal = $itemweight * $quantity;
echo $itemtotal;
}
}
Your SQL sintax is wrong, try
$sql = "SELECT *
FROM `characteritem`
INNER JOIN item ON characteritem.iid=item.iid
WHERE `owner` = '$user'";
Try This
$sql = "SELECT *
FROM `characteritem`
INNER JOIN item ON characteritem.iid=item.iid" WHERE `owner` = '$user';
$result = $db_conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$iid = $row['characteritem.iid'];
$quantity = $row['characteritem.quantity'];
$itemweight = $row['item.itemweight'];
$itemtotal = $itemweight + $itemweight;
echo $itemtotal;
}
}
var_dump($result);
Related
I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}
I am trying to create an PHP api in which I am trying to retrieve news storys and all its comments and send it as json.
Here is what i have so far:
$sql = "SELECT * FROM fb_clubnews WHERE clubid='$groupId' AND ori_newsid = 0 ORDER BY newsid DESC LIMIT 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$storId = $row["newsid"];
$commentArray = array();
$wallAray[] = array("author"=>$row["userid"],
"story"=>$row["news"],
"date"=>$row["date"],
"time"=>$row["time"],
"matchid"=>$row["fk_match_id"],
"comments"=>$commentsArray);
}
}
My issue is, that I would like to avoid creating a sql inside an sql and loop through it?! The second inside sql would be:
$sql = "SELECT * FROM fb_clubnews WHERE ori_newsid = $storId ORDER BY newsid DESC";
How do I get the $commentArray filled up with comments.
My DB Structure for fb_clubnews looks like this:
int newsid (autoincrement),
int userid,
text news,
int data,
int time,
int matchid,
int ori_newsid
Hoping for help on this and thanks in advance :-)
Something like this:
$sql = "SELECT *, A.newsid as main_news_id FROM fb_clubnews A LEFT JOIN fb_clubnews B ON B.ori_newsid = A.newsid WHERE A.clubid='$groupId' AND A.ori_newsid = 0 ORDER BY A.newsid DESC";
$result = $conn->query($sql);
$wallAray = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$storId = $row["main_news_id"];
if(array_key_exists($storId, $wallAray)) {
$commentArray[] = $wallAray;
} else {
$wallAray[$storId] = array();
$commentArray = array();
}
$wallAray[] = array("author"=>$row["userid"],
"story"=>$row["news"],
"date"=>$row["date"],
"time"=>$row["time"],
"matchid"=>$row["fk_match_id"],
"comments"=>$commentsArray);
}
}
I'm making a ranking system. But what I want is to order the results I get ($kn) from highest to lowest. How can I do this?
include "includes/core.inc.php";
require "includes/connect.inc.php";
$id = $_GET["id"];
$query = "SELECT * FROM submitted WHERE id= '$id'";
$query_run = $db->query($query);
while($row = mysqli_fetch_assoc($query_run)){
$name= $row["name"];
$sql = "SELECT * FROM submitted WHERE name= '$name' AND pending = 'Accept'";
$sql_run = $db->query($sql);
$count = $sql_run->num_rows;
$nums= "SELECT * FROM ranking WHERE name= '$name'";
$nums_run = $db->query($nums);
$num = $nums_run->num_rows;
$kn = ($count * 0.4) + (($num * 0.2) * 3);
echo '$name';
echo '$kn';
}
Looping over a list and querying each element is almost never a good idea. Instead, you can move the entire logic to the query, and then sort it there:
$query =
"SELECT s.name AS name, (cnt_submitted * 0.4) + ((cnt_ranking * 0.2) * 3) AS kn
FROM (SELECT name, COUNT(*) AS cnt_submitted
FROM submitted
WHERE id = '$id' AND
pending = 'Accept'
GROUP BY name) s
JOIN (SELECT name, COUNT(*) AS cnt_ranking
FROM ranking
GROUP BY name) r ON r.name = s.name
ORDER BY 2 DESC";
$query_run = $db->query($query);
while ($row = mysqli_fetch_assoc($query_run)) {
$name = $row["name"];
$kn = $row["kn"];
echo '$name';
echo '$kn';
}
Note:
The $id variable should probably be a bound variable in a prepared statement to safe-guard against SQL-injection attacks.
I left it as it was in the OP, though, since this is not the point of the question and I don't want to add additional confusion.
You can do it also in PHP:
$result = [];
while (...) {
.....
$kn = ($count * 4) + (($num * 2) * 30);
$result[] = [
'rank' => $kn,
'name' => $name
];
}
usort($result, function($a, $b) {
return $b['rank'] - $a['rank'];
});
Hello here is my current code:
$sqlpack = "Select * from package_in_plan where plan_id='$package_id' order by plan_id";
$planres = mysqli_query($conn,$sqlpack);
$plan = array();
while ($row1 = mysqli_fetch_assoc($planres)){
$plan[] = $row1['package_id'];
}
$plan_1 = implode(',', $plan);
for ($x = 0; $x < count($plan); $x++) {
$sql_service = "Select * from service_in_package where package_id='".$plan[$x]."'";
$chid = mysqli_query($conn,$sql_service);
while ($row = mysqli_fetch_array($chid)){
$ch_id = $row['service_id'];
$sql = "Select * from itv where status='1' and id='$ch_id' order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
}
}
What i need is the output to be sorted by number($chnr), right now my code is not sorting because it's receiving specific id from previous select ($ch_id).
How can i let the output of $results to be sorted "order by number".
Number is INT in itv table.
You could make use of subqueries which results in a single query that can be sorted.
$sql = "Select * from itv where status='1' and id IN
(Select service_id from service_in_package where package_id IN
(Select package_id from package_in_plan where plan_id='$package_id' order by plan_id))
order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
Hi I am trying to use in_array, I think my syntax is correct,
but it says "Wrong datatype for second argument"
My code is
$result = mysqli_query($con, "SELECT * FROM Products WHERE Quantity_On_Hand < Min_Stock");
$filter = mysqli_query($con, "SELECT ProductID FROM Orders");
while($row = mysqli_fetch_array($result))
{
if (in_array($row['ProductID'], $filter))
{
}
}
My idea is to find out if the ProductID from Products Table is in the Order Table.
Could someone helps me, Thanks :-)
$filter isn't an array; it's a mysqli_result object:
$filter = mysqli_query($con, "SELECT ProductID FROM Orders");
I think you want to iterate over that, add each ProductID to a new array, and then pass that array to the in_array function like so:
$filter = mysqli_query($con, "SELECT ProductID FROM Orders");
$product_ids = array();
while ($row = $filter->fetch_assoc())
{
$product_ids[] = $row['ProductID'];
}
$result = mysqli_query($con, "SELECT * FROM Products WHERE Quantity_On_Hand < Min_Stock");
while($row = mysqli_fetch_array($result))
{
if (in_array($row['ProductID'], $product_ids))
{
}
}
Your code is failing because $filter is a MySQLi result resource, not an array. Really, this is better accomplished with a simple inner join between the two tables. If a ProductID does not exist in Orders, the INNER JOIN will exclude it from the result set in the first place.
$sql = "
SELECT Products.*
FROM
Products
INNER JOIN Orders ON Products.ProductID = Orders.ProductID
WHERE Quantity_on_Hand < Min_stock";
$result = mysqli_query($con, $sql);
if ($result) {
$results = array();
while ($row = mysqli_fetch_array($result)) {
$results[] = $row;
}
}
// Now $results is a 2D array of all your Products
If instead you want to retrieve all the Products, and simply have an indication of whether it has an active order, use a LEFT JOIN and test if Orders.ProductID is null in the SELECT list:
$sql = "
SELECT
Products.* ,
/* No orders will print 'no-orders' in a pseudo column called has_orders */
CASE WHEN Orders.ProductID IS NULL THEN 'no-orders' ELSE 'has-orders' AS has_orders
FROM
Products
LEFT JOIN Orders ON Products.ProductID = Orders.ProductID
WHERE Quantity_on_Hand < Min_stock";
$result = mysqli_query($con, $sql);
if ($result) {
$results = array();
while ($row = mysqli_fetch_array($result)) {
$results[] = $row;
}
}
// Now $results is a 2D array of all your Products
// And the column $row['has_orders'] will tell you if it has any...
In this case, you may test in a loop over your rowset whether it has orders:
foreach ($results as $r) {
if ($r['has_orders'] == 'has-orders') {
// this has orders
}
else {
// it doesn't...
}
}