Jump to content

[SOLVED] query not returning correct number of records. please help


debuitls

Recommended Posts

Hi,

 

I'm developing a reverse auction style site.

 

However, I'm really struggling with the following.

 

$result = mysql_query("SELECT proposal.*,count(bid.proposalid) AS bids,SYSDATE(), TIME_FORMAT(TIMEDIFF(tomorrowtime, SYSDATE()), '%H hours, %i minutes') AS timeleft
FROM proposal
LEFT JOIN bid ON proposal.proposalid = bid.proposalid
WHERE SYSDATE() < proposal.tomorrowtime AND proposal.username = '$username'
GROUP BY bid.proposalid");

 

There are three records that this query should be returning. However when I run this query and attempt to echo to the screen it only prints one?

 

If I run this query it counts the correct number of records?

 


$sql = mysql_query("SELECT COUNT(proposalid) FROM proposal
WHERE SYSDATE() < tomorrowtime AND username = '$username'");

 

I know it's difficult without seeing the tables but just wondering if anybody can spot why this is happening?

 

Any suggestions would be much appreciated.

Link to comment
Share on other sites

Thanks kratsg for getting back to me!

 

I'm not sure im 100% clear on your suggestion.

 

Ill rephrase my question just to make sure I'm being clear about what the problem is.

 

$result = mysql_query("SELECT proposal.*,count(bid.proposalid) AS bids,SYSDATE(), TIME_FORMAT(TIMEDIFF(tomorrowtime, SYSDATE()), '%H hours, %i minutes') AS timeleft
FROM proposal
LEFT JOIN bid ON proposal.proposalid = bid.proposalid
WHERE SYSDATE() < proposal.tomorrowtime AND proposal.username = '$username'
GROUP BY bid.proposalid");

 

This is only returning one record to the screen. It should be returning 3.

 

I ws just wondering if anybody can see why it would return only one record?

 

Thanks

Link to comment
Share on other sites

Hi Stuie_b

 

I'm using a while loop

 

while($row = mysql_fetch_array($result))
{
echo "<tr onclick=\"window.location='results.php?id=" . $row['proposalid'] . "'\">";
			  echo "<td>" . $row['county'] . "</td>";
			  echo "<td>" . $row['checkin'] . "</td>";
			  echo "<td>" . $row['checkout'] . "</td>";
			  echo "<td>" . $row['budget'] . "</td>";
			  echo "<td>" . $row['adults'] . "</td>";
			  echo "<td>" . $row['children'] . "</td>";
			  echo "<td>" . $row['timeleft'] . "</td>";	
			  echo "<td>" . $row['bids'] . "</td>";
			  				  
			echo "</tr>";
			echo "</tbody>";
}
echo "</table>";

 

So I dont think thats the problem.

 

If I remove the

 

WHERE SYSDATE() < proposal.tomorrowtime 

 

it seems to print all the results.

 

Thanks

Link to comment
Share on other sites

Hi,

 

I just realised that the reason that only one row was returning and not three was because of the

 

count(bid.proposalid) AS bids

 

GROUP BY bid.proposalid

 

parts of the sql code.

 

When I removed these it returned all three rows.

 

So that great, but I'm now left with the problem of not having the number of bids made on each proposal in the table.

 

I was just wondering if anybody can think of another way of counting the number of bids made??

 

I think this is what kratsg was suggesting earlier in the thread?

 

This can also count the number of records returned:

mysql_num_rows($sql);

 

Can anyone explain what kratsg meant by this? I'm not so sure.

 

Alternatively if anyone else has any sugestions it would be great!

 

Cheers

Link to comment
Share on other sites

GROUP BY bid.proposalid

 

Add that line back in, and then do the following

 

GROUP_CONCAT(bid.proposalid) AS 'bids'

 

This will group your bids as a comma separated list

 

$num_bids = count(",",explode($row['bids']));

 

Then you can count the length of the array created by exploding into an array around the commas.

Link to comment
Share on other sites

Thanks very much for your suggestion kratsg!

 

$result = mysql_query("SELECT proposal.*,SYSDATE(),GROUP_CONCAT(bid.proposalid) AS 'bids', TIME_FORMAT(TIMEDIFF(tomorrowtime, SYSDATE()), '%H hours, %i minutes') AS timeleft
FROM proposal
JOIN bid ON proposal.proposalid = bid.proposalid
WHERE NOW() < proposal.tomorrowtime AND proposal.username = '$username'
GROUP BY bid.proposalid

...

echo "<td>" . count(",",explode($row['bids'])). "</td>";

");

 

I gave it a try but I still seem to be runnin into the problem of sql only retrieving one record.

 

Whats strange is that if I make a bid on any of the proposals that arent showing then they seem to be retrieved and echo on screen.

 

Does anyone have any other suggestions?

 

Link to comment
Share on other sites

Then we'll add the LEFT OUTER JOIN statement. The main problem with the group by is that if the table with bids is empty, then it won't even return that record, even if that proposal exists.

 

I had a similar problem and was able to google and fix this like shown:

 

Your query:

SELECT proposal.*,SYSDATE(),GROUP_CONCAT(bid.proposalid) AS 'bids', TIME_FORMAT(TIMEDIFF(tomorrowtime, SYSDATE()), '%H hours, %i minutes') AS timeleft
FROM proposal
JOIN bid ON proposal.proposalid = bid.proposalid
WHERE NOW() < proposal.tomorrowtime AND proposal.username = '$username'
GROUP BY bid.proposalid

 

FIXED Query:

SELECT proposal.*,SYSDATE(),GROUP_CONCAT(bid.proposalid) AS 'bids', TIME_FORMAT(TIMEDIFF(tomorrowtime, SYSDATE()), '%H hours, %i minutes') AS timeleft
FROM proposal
LEFT OUTER JOIN bid ON proposal.proposalid = bid.proposalid
WHERE NOW() < proposal.tomorrowtime AND proposal.username = '$username'
GROUP BY bid.proposalid

 

Let me know if this fixes it (you may want to add some code checking that if there are no bids, ie if $row['bids'] is empty, set it to 0 or something)

Link to comment
Share on other sites

Thanks again for your time on this kratsg.

 

I added the LEFT OUTER JOIN statement as you suggested but still only returining proposals which have bids made.

 

So I think I may need some code checking as you say. 

 

I'm sorry, only learning php, is there any way you could give me an example of how to structure the statement to check if there are no bids for a particular proposal.

Link to comment
Share on other sites

When I run it there in phpmyadmin it only returns the one row when it should be retrieving 4.

 

SELECT proposal . * , SYSDATE( ) , GROUP_CONCAT( bid.proposalid ) AS 'bids', TIME_FORMAT( TIMEDIFF( tomorrowtime, SYSDATE( ) ) , '%H hours, %i minutes' ) AS timeleft
FROM proposal
LEFT OUTER JOIN bid ON proposal.proposalid = bid.proposalid
WHERE NOW( ) < proposal.tomorrowtime
AND proposal.username = 'abrett'
GROUP BY bid.proposalid

 

Anyone have any more ideas?

 

Sorry for dragging it out on everyone!

 

Link to comment
Share on other sites

Ok, let's try it this way.

 

Give us two screenshots: one of the row in the 'proposal' table showing the username='abrett' and then the other being the rows in the 'bid' table where the ids match and we'll see what's going on.

 

Or maybe it's only showing one row because there's only one proposal with the username 'abrett'?

Link to comment
Share on other sites

if the number of bids made is equal to the number of rows returned, than kats previous advice to use mysql_num_rows would work. I'm not entirely sure if this is true, if it is, then that would probably be the easiest solution if you are having so much trouble getting the query to return the count.

 

to use mysql_num_rows just stick in a line like that after your query

$query = mysql_query("MY QUERY");
//THE FOLLOWING LINE IS THE IMPORTANT ONE
$num = mysql_num_rows($query);

 

this is of course a very simplified version without any debug checks, but the basic logic is there.

Link to comment
Share on other sites

Ok so you'll see in proposal screenshot that user abrett has made three proposals, proposalid 103,104,105

 

You can see that only two of the proposals have bids on them in the bidtable screenshot, 103 and 104. There is no bid on 105!

 

However in the site screenshot all three proposals appear with one bid on them each.

 

If none of the proposals had a bid on them then only one proposal would print to screen. 

 

I hope this makes it less confusing!

 

Thanks so much for helpin me out!

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Sounds promising

 

$result = mysql_query("SELECT proposal.*,SYSDATE(),GROUP_CONCAT(bid.proposalid) AS 'bids', TIME_FORMAT(TIMEDIFF(tomorrowtime, SYSDATE()), '%H hours, %i minutes') AS timeleft
FROM proposal
LEFT OUTER JOIN bid ON proposal.proposalid = bid.proposalid
WHERE NOW() < proposal.tomorrowtime AND proposal.username = '$username'
GROUP BY bid.proposalid
");


echo "<table id='myTable' cellpadding='0'>
<thead>
<th axis='string'>Location</th>
<th axis='date'>Check-in</th>
<th axis='date'>Check-out</th>
<th axis='number'>Budget</th>
<th axis='number'>Adults</th>
<th axis='number'>Children</th>
<th axis='string'>Auction Time Left</th>
<th axis='number'>Bids</th>


</thead>";


while($row = mysql_fetch_array($result))
{
echo "<tr onclick=\"window.location='results.php?id=" . $row['proposalid'] . "'\">";
			  echo "<td>" . $row['county'] . "</td>";
			  echo "<td>" . $row['checkin'] . "</td>";
			  echo "<td>" . $row['checkout'] . "</td>";
			  echo "<td>" . $row['budget'] . "</td>";
			  echo "<td>" . $row['adults'] . "</td>";
			  echo "<td>" . $row['children'] . "</td>";
			  echo "<td>" . $row['timeleft'] . "</td>";	
			  echo "<td>" . count(",",explode($row['bids'])). "</td>";
			  				  
			echo "</tr>";
			echo "</tbody>";
}
echo "</table>";



Link to comment
Share on other sites

I thought it would've been a problem with $row['bids']

 

Can you run the query in your phpmyadmin/whatever database you've got?

 

This query, see what's returned.

SELECT proposal.*,SYSDATE(),GROUP_CONCAT(bid.proposalid) AS 'bids', TIME_FORMAT(TIMEDIFF(tomorrowtime, SYSDATE()), '%H hours, %i minutes') AS timeleft
FROM proposal
LEFT OUTER JOIN bid ON proposal.proposalid = bid.proposalid
WHERE NOW() < proposal.tomorrowtime AND proposal.username = 'abrett'
GROUP BY bid.proposalid

 

Also, it's the first row that shouldn't have a bid (since it's antrim and not kilkenny [antrim=105])

Link to comment
Share on other sites

Sorry kratsg for being confusing.

 

I just made a new proposal (ie 107) which is the new Kilkenny proposal.

 

I deleted it there.

 

So basically back to the way it was, ran the query you gave me. Screenshot is the result

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.