andrew_biggart Posted February 20, 2009 Share Posted February 20, 2009 what kind of statement would i use to do the following/ i would like to select the latest say recipe added to the database so how would i tell it to select the last recipe_id ? and also is there a way that i can count the number of users i have in my database and display it as a number? thanks Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/ Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 $sql = "SELECT * FROM recipes ORDER BY recipe_id DESC" $sql = "SELECT count(userid) as userscnt FROM members"; Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/#findComment-767202 Share on other sites More sharing options...
Mark Baker Posted February 20, 2009 Share Posted February 20, 2009 i would like to select the latest say recipe added to the database so how would i tell it to select the last recipe_id ? SELECT * FROM recipes ORDER BY recipe_id DESC LIMIT 1 and also is there a way that i can count the number of users i have in my database and display it as a number? SELECT COUNT(*) AS userCount FROM users Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/#findComment-767203 Share on other sites More sharing options...
allworknoplay Posted February 20, 2009 Share Posted February 20, 2009 For your recipe table, it's probably a good idea to have a timestamp of when it was submitted. this way you can show dates for when people submited or modified recipes. You can order by date, instead of just the last recipe and give you more control over how you want to show recipes.... Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/#findComment-767211 Share on other sites More sharing options...
andrew_biggart Posted February 20, 2009 Author Share Posted February 20, 2009 is this right then? <br /> <img alt="Newest recipe" src="../Header_images/newestrecipe.gif" /><br /> <br /> <?php include("config_recipes.php"); // Retrieve data from database $sql="SELECT * FROM User_recipesT ORDER BY Recipe_id LIMIT 1 "; $result=mysql_query($sql); // Start looping rows in mysql database. while($rows=mysql_fetch_array($result)){ ?> <table> <tr><td class="newmain"> <table> <tr><td><h1 class="new_up">Uploaded by :</h1></td><td><h1 class="new_sub_user"><a href="profile.php?username=<? echo $rows['Recipe_username']; ?>"><? echo $rows['Recipe_username']; ?></a></h1></td></tr> </table> <table> <tr><td><h1 class="new_sub"><? echo $rows['Recipe_subject']; ?></h1></td></tr> </table> <? echo $rows['Recipe_content']; ?> </td></tr> </table> <br /> <? // close while loop } // close connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/#findComment-767213 Share on other sites More sharing options...
andrew_biggart Posted February 20, 2009 Author Share Posted February 20, 2009 yea i have a timestamp on it hwo would i do the select statement then? cheers Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/#findComment-767214 Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 First part, no. $sql="SELECT * FROM User_recipesT ORDER BY Recipe_id DESC LIMIT 1 "; Notice the DESC, which Mark and I added in our post. With a timestamp it would be similar (I cannot remember if the desc is needed for timestamps or not. You will have to test this.) $sql="SELECT * FROM User_recipesT ORDER BY Recipe_date DESC LIMIT 1 "; Just change the order by column. Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/#findComment-767215 Share on other sites More sharing options...
andrew_biggart Posted February 20, 2009 Author Share Posted February 20, 2009 Cheers everyone thats worked Quote Link to comment https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/#findComment-767224 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.