Hey guys, I'm looking to build a custom script for my website and I want to create a file called admin.php. There I will be able to enter something into an input box which saves in MySQL. I know how to do this so far. But the one thing I dont understand is how would I make it so that everytime I submitted something to the MySQL Database, it would be on top of the page and what I have submitted previously, would go right below it. Like 10 of these per page and if I have more then each page would have 10 . Anyone know any tutorials on this or can show me how to do this? Thanks,
your query should be like this $query = "SELECT id, title, description FROM yourtable ORDER BY id DESC"; PHP: order by .... desc
Just to add to baris22's point above, there's also the 'LIMIT' clause, which you can use for paging. So to get the first page your SQL query should end with something like: ... ORDER BY id DESC LIMIT 10; Code (markup): Second page: ... ORDER BY id DESC LIMIT 10 OFFSET 11; Code (markup):
You could also use it like this: ... ORDER BY id DESC LIMIT 11, 10; Code (markup): (the syntax is: LIMIT offset, row_count OR LIMIT row_count)
Ok well I'm pretty new to PHP so I put this on my page and it didnt work. <?php include "base.php"; ?> <?php $query = "SELECT username FROM users ORDER BY id DESC"; ?> Should I put something after it? base.php connects to my database. I'm really confused on this. The table is called "users" and the usernames are under "usernames". I just want to make list all the usernames in order by id. Thanks,
This will print out a list of usernames, assuming that the base.php connects to the DB OK. <?php include ("base.php"); $query = "SELECT username FROM users ORDER BY id ASC"; <ul> while($row = mysql_fetch_array($query)) { echo "<li>" . $row['username'] . "</li>"; } </ul> ?> Code (markup): As you are new to PHP/MySQL I reccomend reading the tutorials at w3schools. I found them very helpful when i was learning, and still do.
yea, like this: include ("base.php"); $query = "SELECT username FROM users ORDER BY id ASC"; $result = mysql_query($query); echo "<ul>"; while($row = mysql_fetch_array($result)) { echo "<li>" . $row['username'] . "</li>"; } echo "</ul>"; Code (markup):
the better way to check queries is to set a function which checks whether the query is correct or not. You can do so by using this function: function check_query($query) { if(!$query) { die("Database error: ".mysql_error()); } } PHP: You can call this function after each mysql_query($query) statement like this: // you can define this function anywhere in the page. function check_query($query) { if(!$query) { die("Database error: ".mysql_error()); } } $query = "SELECT username FROM users ORDER BY id ASC"; $result = mysql_query($query); check_query($result); PHP: this function saves time for retyping the die thing..Hope this helps.