Jump to content

help please - how do i use SELECT MAX properly?


codlife

Recommended Posts

Hi there,

Pretty new to php so you'll have to bear with me if you don't mind!

 

What i've got:

  • sql table "news" with 2 field
  • field 1: "user_id" - auto-inc integer
  • field 2: "lnews" - text field

 

What I'm trying to do:

  • get my php include to display only the newest news item (highest incr. integer)

 

My current non working code:

<?php

 

/*define variables*/

$con = mysql_connect("localhost", "root");

 

 

/*display error msg if cannot connect*/

if (!$con)

{

die("could not connect" . mysql_error());

}

 

 

/*select database and connect to it*/

mysql_select_db("controlpanel", $con);

 

 

/*display data from max user_id*/

$news_entry = mysql_query("SELECT MAX user_id FROM news");

 

while ($row = mysql_fetch_assoc($news_entry))

{

$latestnews = $row['lnews'];

echo $row["lnews"];

}

 

 

/*write the entry here as this is the include for the site - apply css on the site*/

 

mysql_close($con);

 

 

 

?>

 

 

Any help would be majorly appreciated as have been staring at this for hours!

 

 

this is not a select max, its a ORBDER BY problem

 

 

also, the way your table is set up is pretty weird, why would you order by the User id?...create more fields, like "News_id"

 

$res = mysql_query("SELECT * FROM news ORDER BY user_id DESC LIMIT 5")or die(mysql_error());

Archived

This topic is now archived and is closed to further replies.

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