Jump to content

[SOLVED] mysql statements


andrew_biggart

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.