Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/146140-solved-mysql-statements/
Share on other sites

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

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....

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();
?>

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.

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.