Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying database info, please help

Status
Not open for further replies.

lentildal

Programmer
Sep 24, 2001
25
GB
hi people, this is my first post

im pretty new to this stuff, hence a simple question. could someone please explain how to write a php script to display the most recent entry from a mySQL database.

i want to be able to output the info to an html page

it would be useful to know how to adapt it to display the last 5 results too.

thanks in advance

give me all your lentils
 
// database info.
$sqlhost = "localhost";
$database_name = "database_name";
$db_user = "user_id_here";
$db_password = "password_here";


// connect to the database.
$dbh = mysql_connect($sqlhost,$db_user,$db_password);
mysql_select_db($database_name,$dbh);
$err = mysql_error();
if ($err){
echo &quot;Error in database connection : $err<BR>&quot;;
exit();
};

// get the last 5 inserted records - assumes the primary
// key is auto_increment else use a data field.
$SQL = &quot;SELECT * FROM mytable ORDER BY primary_field
DESC LIMIT 5&quot;;
$result = mysql_query($SQL);

// print out each record.
while ($record = mysql_fetch_array($result)){
$fname = $record[first_name];
$lname = $record[last_name];
$age = $record[age];
echo &quot;
Full Name : $fname $lanm <BR>
AGE : $age <BR><BR>
&quot;;
};

cheers :) devnull22

--
Apparently if you play the Windows NT CD backwards you hear satanic messages. If you think that's bad, play it forwards and it installs Windows NT !
 
muchas gracias muchacho

i'll check out dat code right now

nice signature btw are you a mac user or just hate NT?

hasta luego hombre

give me all your lentils
 
hey devnull22

it didnt like this bit much, im afraid im nt proficient enough with this php/mySQL stuff to know what is wrong with it

while ($record = mysql_fetch_array($result)){

the error i got was like this...

Warning: Supplied argument is not a valid MySQL result resource

you know what is going on here? help would me much appreciated amigo

give me all your lentils
 
Check that your SQL statement is correct.
put this code after &quot;$result = mysql_query($SQL);&quot;

$err = mysql_error();
if ($err){
echo &quot;SQL ERROR : $err<BR>&quot;;
};


--- end code --

chances are , your SQL statement is incorrect. The above
code will print the error.
Try a simple SQL statement like
&quot;SELECT * FROM table_name&quot;

just to check if the code is ok.

cheers

btw. I'm not a MAC user - just don't like MickeySoft products - but forced to use it :( devnull22

--
Apparently if you play the Windows NT CD backwards you hear satanic messages. If you think that's bad, play it forwards and it installs Windows NT !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top