Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Shane McC
3,005 PointsCheck to see if a query has one result?
Hi Everyone,
How would one go about checking if a SQL query has more than one result?
How would one go about checking to see if a SQL query only has one result?
Thanks
3 Answers
Tiffany McAllister
25,806 PointsIs something like this what you are after?
$query = "ENTER QUERY HERE'";
$result = mysql_query($query) or die ("Query Failed");
$count = mysql_num_rows($result);
if ($count == 1){
// DO SOMETHING;
}
Dustin Newbold
4,253 PointsWhat language are you writing to execute this query?
If you're wanting to ensure that there is only one result, you can use LIMIT. Example: SELECT * FROM Posts LIMIT 0,1 where 0 is where to start (0-based index) and the 1 is the amount of results to return.
Shane McC
3,005 PointsHI Dustin,
I'll be using PHP/SQL. My goal isn't to limit the query but I want to check if the query has more than one query.
If the query has only one result I want the code to do something.
If the query has more than one query I want the code to do something else entirely.
Thanks
Shane McC
3,005 PointsHI Dustin,
I'll be using PHP/SQL. My goal isn't to limit the query but I want to check if the query has more than one query.
If the query has only one result I want the code to do something.
If the query has more than one query I want the code to do something else entirely.
Thanks
Shane McC
3,005 PointsShane McC
3,005 PointsYes, that's the logic. Thanks
Tiffany McAllister
25,806 PointsTiffany McAllister
25,806 PointsNot a problem :)