Jump to content

Php DESC LIMIT by id question.


shiloam

Recommended Posts

Glad to be back in the community  :D

 

Anyways I've got a mysql table with 4 different entries from a form.  One being an id with an auto-increment.  I'm wanting to arrange them into a table, descending by the id number.  I'm having a hard time wrapping my mind around how to even start it and get the other 3 fields to retrieve and be in a table together.  I have:

 

$db="prayers";
$link = mysql_connect('localhost', 'root' , '');
if (! $link) die(mysql_error());
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());


$query = "SELECT * FROM table prayerwall BY id DESC LIMIT 10";  

$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

 

Not sure how to retrieve the other entries and then put them into a table.  A start or some help would be greatly appreciated.  Thanks everyone!

Link to comment
Share on other sites

I'm ok with the 10 entries, I'm just wondering how to put them on the page.  Do I need to retrieve the other fields in a seperate query? When I try to just

 

echo '$result'; 

 

I get this error

 

Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/1/retrieve.php on line 14
$result

Link to comment
Share on other sites

Ok I'm sorry guys I'm still struggling with this I don't really understand.

 

 


<?PHP 
//do your normal mysql setup and connection calls 
$db="prayers";
$link = mysql_connect('localhost', 'root' , '');
if (! $link) die(mysql_error());
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());


mysql_select_db($db); 
//get the stuff from da table
$sql = "SELECT name,email,prayer FROM prayerwall BY id DESC LIMIT 10";  
$dbq = mysql_query($sql); 
//now put it into a table.
echo "<TABLE>"; 
while ($row = ($dbq)) { 
echo "<TR>"; 
echo "<TD>".$row[name]."</TD>"; 
echo "<TD>".$row[email]."</TD>";          (This is the part I don't understand)
echo "<TD>".$row[prayer]."</TD>"; 
echo "</TR>"; 
} 
echo "</TABLE>"; 
?>

 

 

I want it to output where it's

 

Name Email  - Id 1

 

Prayer

 

Name Email  - Id 2

 

Prayer

 

Name Email - Id 3

 

Prayer

 

 

Not like

 

Name, Name, Name

 

 

Email, Email, Email

 

 

Prayer, Prayer, Prayer

 

 

Does that make any sense?  :shrug: haha.

Link to comment
Share on other sites

Yes, like jesirose pointed out, you query, but you don't setup a fetch loop.

 

In database parlance:

 

query  -> executes the sql, makes a result set.  The result set is sitting on the server.

fetch -> grabs a row from the result set.  If there are no rows, or no more rows, a fetch fails.

 

Your code is missing entirely the fetch function.  There are a variety of different functions, but for most people i recommend using mysql_fetch_assoc().  The php manual page shows EXACTLY how to use it, again as jesirose stated.

Link to comment
Share on other sites

You're going to need to loop through the rows and echo that info.

On the php documention for mysql functions it shows several examples.

 

Thank you that's what I needed.  For anyone else who needs something like this, here was my end base code.

 

 

<?php

$db="prayers";
$link = mysql_connect('localhost', 'root' , '');
if (! $link) die(mysql_error());
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());

$query="select * from prayerwall";
$rt=mysql_query($query);
echo mysql_error(); 



while($nt=mysql_fetch_array($rt)){
echo "$nt[name] $nt[email] $nt[prayer]<br>";     // name class and mark will be printed with one line break
}

?>

 

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.