I have created a mysql seect statement with a nested select statement. Now my mysql skills (or lack there of) are very limited. Below is the code that I wrote. I was getting blank results or the Warning. mysql_fetch_Array error. Now I am currently receiving an error that says "Subquery returns more than 1 row" Can anyone point me in the right direction on how I can begin to fix this problem. Thanks for the help.
<?php
session_start();
$memberId = $_GET['id'];
$loggedId = $_SESSION['id'];
include('../connect_DB.php');
$sql = 'SELECT bins.tag_Id, tagging_Info.plant_Id, tagging_Info.photo_Id FROM bins inner join tagging_Info on bins.tag_Id = tagging_Info.tag_Id inner join collections on collections.id = bins.collection_Id WHERE collections.member_Id ='.$memberId.' and collections.id=(SELECT id FROM collections where member_Id='.$memberId.')';
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$collection = "Success"; // test to see if working
}
echo $collection;
?>
Have you tried executing just the subquery to see how many records are actually being returned and to ensure that $memberId is actually set?
It would make sense for a user to have multiple collections so you should probably adjust your main query to use IN instead of = on the subquery results.
Related
I'm new here on stackoverflow.
I'm performing a select query to populate the output I need. While it is fetching rows from query, every rows that has been fetched is I'm inserting them into a specific table. But however, the exact amount of rows that I need to achieve is 1,767 rows but after performing the query, 1759 was the output. I have 8 rows missing.
What's the problem with my code?
Here's my code:
$query2 = "SELECT trihadjustmentitems.AdjID AS `adjid1`, trihadjustment.Adj_ID AS `adjid2`,
trihadjustment.AdjToUnitID AS `adjtounitid`, trihadjustment.AdjDate AS `adjdate`, trihadjustmentitems.InvItemsID AS `invitemid`,
trihadjustmentitems.SlsItemsID AS `slsitemid`, trihadjustmentitems.RecipeID AS `recipeid`, trihadjustmentitems.Remark AS `remark`,
trihadjustmentitems.AdjQty AS `adjqty`,
trihadjustment.StockCenter_ID AS `stockcenterid1`, trihadjustmentitems.StockCenter_ID AS `stockcenterid2`
FROM trihadjustmentitems
INNER JOIN trihadjustment ON trihadjustmentitems.AdjID = trihadjustment.Adj_ID";
$result2 = mysqli_query($connection, $query2);
while($row2 = mysqli_fetch_array($result2))
{
$query3 = "INSERT INTO adjustments (adjid1, adjid2, adjtounitid, adjdate, invitemid, slsitemid, recipeid, remark, adjqty, stockcenterid1, stockcenterid2) VALUES ('$row2[adjid1]', '$row2[adjid2]', '$row2[adjtounitid]', '$row2[adjdate]', '$row2[invitemid]', '$row2[slsitemid]', '$row2[recipeid]', '$row2[remark]', '$row2[adjqty]', '$row2[stockcenterid1]', '$row2[stockcenterid2]')";
$result3 = mysqli_query($connection, $query3);
}
There's not enough information to determine that anything goes wrong at all. Your code, although dated, doesn't contain anything that can help anyone determine what's wrong, if anything is wrong. We can only trust you when it comes to numbers and that's not how IT works.
Your query is not needed at all. You can do what you're after without the while loop. Simply use INSERT INTO ... SELECT syntax.
I'll use your code to illustrate
$query = <<<EOF
INSERT INTO adjustments
(adjid1, adjid2, adjtounitid, adjdate, invitemid, slsitemid, recipeid, remark, adjqty, stockcenterid1, stockcenterid2)
SELECT
trihadjustmentitems.AdjID AS `adjid1`,
trihadjustment.Adj_ID AS `adjid2`,
trihadjustment.AdjToUnitID AS `adjtounitid`,
trihadjustment.AdjDate AS `adjdate`,
trihadjustmentitems.InvItemsID AS `invitemid`,
trihadjustmentitems.SlsItemsID AS `slsitemid`,
trihadjustmentitems.RecipeID AS `recipeid`,
trihadjustmentitems.Remark AS `remark`,
trihadjustmentitems.AdjQty AS `adjqty`,
trihadjustment.StockCenter_ID AS `stockcenterid1`,
trihadjustmentitems.StockCenter_ID AS `stockcenterid2`
FROM trihadjustmentitems
INNER JOIN trihadjustment ON trihadjustmentitems.AdjID = trihadjustment.Adj_ID
EOF;
$result = mysqli_query($query);
if(!result) {
echo 'An error occurred';
}
INSERT INTO tbl_name
(a,b,c)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
there is no need to run query in loop you can try this after loop complete
It don't seem to be a PHP problem.
The problem could be the INNER JOIN, you could use instead LEFT JOIN in order to extract all the record from trihadjustmentitems table.
I hope this help.
First, why $result1, $result2, $result3?... And the same with all vars.
Instead of while, you could use foreach() loop.
foreach($resultQuery as $result){
$insertQuery = "INSERT INTO adjustments...
}
use var_dump($result); if you don't know what its $result.
Make sure all the results have the same structure. Maybe you can't insert something in adjustment because the value is NULL. Check it.
I have three tables (questions, answers, user) and I want to query all questions and answers posted by user.
Here is my code:
$query = $this->db->select('user.*,questions.*,answers.*')
->from('user')
->join('questions','questions.user_id = user.u_id','LEFT')
->join('answers','answers.user_id = user.u_id','LEFT')
->where('user.u_id',$profile[0]['u_id'])
->group_by('user.u_id')
->get();
echo $query->num_rows();
I have 1 posted question and 1 answer in my tables, but when I am trying this code it gives me only 1 row.
Try this with method..
write your query in single line like this
$query= "select * from user where" ; as well ( this is only query example you have to write your query)
then
if ($result = mysqli_query($connection, $query )) {
/* fetch associative array */
while ($res = mysqli_fetch_assoc($result)) {
print_r($res);
}
mysqli_free_result($result);
}
in this case $connection is a your database connection
Have you tried outer join, instead left join?
Maybe even a left outer join, solves your problem, just try it.
Oh, and instead of 'num_rows()' i suppose you just want to use 'row()' to
retrieve all table´s data.
$this->db->select('user.*,questions.*,answers.*');
$this->db->join('questions','questions.user_id = user.u_id','OUTER LEFT');
$this->db->join('answers','answers.user_id = user.u_id','OUTER LEFT');
$this->db->where('user.u_id',$profile[0]['u_id']);
$this->db->group_by('user.u_id')
$query = $this->db->get('user')->row();
echo $query;
I´ve changed a little your write, just copy and test it if you´d want
I'm kinda new on this one, and I'm building my first website. Just for fun and to learn how everything works.
I have this weird issue, I can't figure out how to do it:
$query = "SELECT * from artikel inner join categorie on artikel.artikelnummer = categorie.artikelnummer
inner join categorienaam on categorie.categorienummer = categorienaam.categorienummer
left join bestandsnaam on artikel.artikelnummer = bestandsnaam.artikelnummer
where categorie.categorienummer = ".filter_var($_GET['categorienummer'], FILTER_SANITIZE_NUMBER_INT)." LIMIT $first_product_shown, $products_per_page";
while($row = mysqli_fetch_assoc($query))
{
do stuff
}
It works when I'm not using the LIMIT. But I don't know how to get this limit to work. For example, this one works:
$query = mysqli_query($conn, "SELECT * FROM artikel LEFT JOIN bestandsnaam ON artikel.artikelnummer = bestandsnaam.artikelnummer LIMIT $first_product_shown, $products_per_page");
while($row = mysqli_fetch_assoc($query))
{
do stuff
}
I think my code fails because of the combined use of 'FILTER_SANITIZED_NUMBER_INT' and 'LIMIT'. Maybe anyone knows what the problem is? I need this to get my products on my website splitted into pages.
Thanks in advance!
Your code is failing because you appear to be missing the query call in the first code block. Try echoing out the mysqli error
In your second block you have called the SQL inside the query directly, but not in the first and have not added the query.
This should be a basic question, but I haven't used Mysql for a very long time and forgot all the basic stuff. So SO programmers please bear with me.
I have 2 tables like this:
Table 1 (events): here
Table 2 (users): here
I would like to select all rows in the events table where event_invitees contains a username. I was able to do this using:
SELECT * FROM meetmeup_events WHERE event_invitees LIKE '%$username%'
Now I'd like to also select the event_invitees's photo from the users table (column called user_userphoto). My attempt to this was this:
$result = mysql_query("SELECT meetmeup_events.*, meetmeup_user.user_photo
FROM meetmeup_events
WHERE event_invitees LIKE '%$username%'
INNER JOIN meetmeup_user
ON meetmeup_user.user_username = meetmeup_events.event_inviter");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['meetmeup_user'][] = $r;
}
echo json_encode($rows);
This gave me an error: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
How can I do this? What am I missing? Can you give me some examples?
Thanks in advance! I'll be sure to accept the working answer!
You should change your mysql functions to either mysqli / PDO, although the problem seems to be the query itsef. Should be:
SELECT meetmeup_events.*, meetmeup_user.user_photo
FROM meetmeup_events
INNER JOIN meetmeup_user
ON meetmeup_user.user_username = meetmeup_events.event_inviter
WHERE event_invitees LIKE '%$username%'
(the WHERE clause at the end)
Sql fiddle demo: http://sqlfiddle.com/#!2/852a2/1
Its just a matter of getting the query coded in the correct order, and you might like to make it a little more managable by using alias's for the table names
Try this :-
SELECT me.*,
mu.user_photo
FROM meetmeup_events me
INNER JOIN meetmeup_user mu ON mu.user_username = me.event_inviter
WHERE me.event_invitees LIKE '%$username%'
This of course assumes that all the column names are correct and the mu.user_username = me.event_inviter does in fact make sence because those fields are in fact equal
Additional Suggestion
You are not actually issuing the query for execution by mysql.
You have to do this :-
$sql = "SELECT me.*,
mu.user_photo
FROM meetmeup_events me
INNER JOIN meetmeup_user mu ON mu.user_username = me.event_inviter
WHERE me.event_invitees LIKE '%$username%'";
$result = mysql_query($sql);
$rows = array('mysql_count' => mysql_num_rows($result) );
while($r = mysql_fetch_assoc($result)) {
$rows['meetmeup_user'][] = $r;
}
echo json_encode($rows);
Now in your browser using the javascript debugger look at the data that is returned. There should at least be a mysql_count field in it even if there is no 'meetmeup_user' array, and if it is zero you know it found nothing using your criteria.
i got a fairly simple layout going and for the life of me i cant figure out why this returns nothing:
<?php
// Gets A List Of Comic Arcs
$result = mysql_query("SELECT * FROM ".$db_tbl_comics." GROUP BY ".$db_fld_comics_arc." ORDER BY ".$db_fld_comics_date." DESC LIMIT 20");
while ($comic = mysql_fetch_array($result)) {
// Now Go Back And Count Issues For Each Comic Arc Above
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."=".$comic[$db_fld_comics_arc]);
$total_issues = mysql_num_rows($result22);
echo $total_issues;
}
?>
No other query is refered to as $result22.
$comic[] has already been defined in the previous query.
echo mysql_error($result22); returns no errors.
Let me know if you need any other info.
I am assuming that the column $db_fld_comics_arc is a string.
Change:
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."=".$comic[$db_fld_comics_arc]);
To:
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."='".$comic[$db_fld_comics_arc]."'");
Am I wrong? If so, let me know the table structure, and what your error reporting is set to.
Also, could you let us know the purpose of your SQL? It may also be possible to put the data together in one query, instead of looping sql queries through, and using data from a first query.
Maybe it is because $db_fld_comics_arc is in $comic[$db_fld_comics_arc]
if both are the same then you should try replacing $db_fld_camics_arc with $comic[$db_fld_comics_arc].