shiloam Posted February 10, 2011 Share Posted February 10, 2011 Glad to be back in the community 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! Quote Link to comment Share on other sites More sharing options...
bashaclick Posted February 10, 2011 Share Posted February 10, 2011 it's should be like $query = "SELECT * FROM table prayerwall order by id DESC "; try it Quote Link to comment Share on other sites More sharing options...
shiloam Posted February 10, 2011 Author Share Posted February 10, 2011 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted February 10, 2011 Share Posted February 10, 2011 as you did with mysql_select_db, user mysql_error() to see if the query works. Quote Link to comment Share on other sites More sharing options...
shiloam Posted February 11, 2011 Author Share Posted February 11, 2011 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? haha. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
shiloam Posted February 11, 2011 Author Share Posted February 11, 2011 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 } ?> Quote Link to comment Share on other sites More sharing options...
shiloam Posted February 11, 2011 Author Share Posted February 11, 2011 I forgot to add this part $query="select * from prayerwall ORDER BY id DESC LIMIT 5"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.