Jump to content

Recommended Posts

Ok... Well here is the code first of all.

<?php

						include("mysql.php");

						$query = "SELECT * FROM website"; 
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_array($result)){
						echo $row['liveblog']. "-" .$row['liveblog_name'] ;
						echo "<br />";
						}
						?>

 

Now, it shows the oldest entry first.

I want it to show the newest entry first and only show 15 of them.

 

Also here is the code for when i submit it to the database

<?php

						include("mysql.php");

						$liveblog = $_POST['liveblog'];
						$name = $_POST['liveblog_name'];

						mysql_query("INSERT INTO website 
						(liveblog, liveblog_name) VALUES('$liveblog', '$name' ) ") 
						or die(mysql_error());  

						include ("addliveblog.php");

						?>

to limit it to 15 you do the following;

 

SELECT * FROM website LIMIT 15

 

To get everything in reverse order you need to `ORDER` your query by one of the field values. This is where an id field would come into play. Your only options at the moment are to order by liveblog or liblog_name, but these wont give you the desired effect.

 

I'd recommend adding a third field to the table `id` of type INT which is a primary key, and auto increments.

Ok, i got the limit part, but the auto incriment. how can i do that? can you give me some sample code to use?

 

heres my form code

<form id="form1" name="form1" method="post" action="postblog.php">
  <label>liveblog_name
  <input type="text" name="liveblog_name" />
  </label>
  <p>
    <label>liveblog
    <input type="text" name="liveblog" />
    </label>
  </p>
  <p>
    <label>Post
    <input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
</form>

I was refering to you mysql table. You've already made a table with 2 fields; liveblog and liveblog_name.

 

You want to make a third, try this if you like;

 

ALTER TABLE `website` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY 

 

If you use phpmyadmin, use it to run an sql query with the above code.

Ok, what is wrong with this:

<?php
//showing live blog

						include("mysql.php");

						$query = "SELECT * FROM website limit 15 ORDER BY id"; 
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_array($result)){
						echo $row['liveblog']. "-" .$row['liveblog_name'] ;
						echo "<br />";
						}
						?>

 

error code is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id' at line 1

You have to do things in the correct order :)

 

SELECT * FROM website ORDER BY `id` LIMIT 15

 

Also, to get it in the correct direction you'll have to order it descending

 

SELECT * FROM website ORDER BY `id` DESC LIMIT 15

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.