In this Tutorial we are going to show you how to create a news script, it will update by itself when you enter new data in the database, for this we will be using MySql.Â
How.Â
Before we go any further and show code, we will explain the logic behind it and mostly how it works, we are going to create a simple database table that will contain the article text and other info. Anything you would like to have like category, writer, date etc.
Â
After that we’ll have to make a query that will call all of this info, when all of the info is returned to the script the script then shows it on the website, the good thing is that if we add a new item to the database a quick refresh on the page will show it instantly, meaning we wont have to write the page again and upload it again, cutting down on a lot of manual work.
Â
HTML first.Â
We are going work this with the help of a HTML table, the rows are going to be generated for each article to use for a loop. We will get into that later, first let’s see how this is going to look.
That’s our table layout wonderfully plain but it gets the job done anyway…
So let’s take a look at the code for this table.
Â
<table border=”1″ width=”500″><tr><td align=”center”>Title Here</td></tr><tr><td>Article Here</td></tr><tr><td align=”right”><font size=”1″>Author and Date</font></td></tr></table>Â
That’s just plain HTML nothing much to explain about it, if you don’t know html then you should probably be learning that before going onto PHP, what we want to do here is replace the variables with the following lines “Title Hereâ€, “Article Hereâ€, Author and Dateâ€, we’ll pull that out of the database, let’s see how that’s done.
Â
Â
Â
Â
Â
Â
Â
MySql Connect.Â
In order to pull data from a MySql database you will need to login the database so you need some information.
Username.
Password.
Address.
Database Name.
Once you have all of this info you can start connecting to the database. mysql_connect definition on php.net is
Â
mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )Â
If we were to connect to a localhost server with the username as root and no password we would use something like this
Â
mysql_connect ( ‘localhost’, ‘root’ ,’’)Â
This will work, but it’s always better to use variables with data in them to connect to the db, especially when your working on a big project where you can set all of the info onto a config file and you can pull the same info all the time, just by typing the name of the variable rather then typing the login info all over the place, also if your working on your site in your computer and then you upload it but the login info is different you just change the value on the variables and it works, otherwise you would have to edit all the files individually.
Â
For this tutorial we are not going to use 2 different files we will just start with the variables in the same file, so first lets declare the variable then connect to the database using them.
Â
$mysql_address = ‘localhost’;$mysql_uname = ‘root’;$mysql_pass = ‘putpasshere’;$mysql_dbname = ‘putuserhere’;Â
Now that we have all the info to connect we can write the string to connect to the database.
Â
mysql_connect ($mysql_address, $mysql_uname, $mysql_pass) or die (”There seems to be a problem with the SQL Server please try again later”);mysql_select_db ($mysql_dbname) or die( mysql_error());Â
Â
Â
The connection is a Boolean connection if succeeded then all is good otherwise it will die and stop the document right there and show the message “There seems to be a problem with the SQL Server please try again later†also we are going to set the mysql_connect string as a value to a variable because we are going to use that string many times so we are going to use this:
Â
$connection = mysql_connect ($mysql_address, $mysql_uname, $mysql_pass) or die (”There seems to be a problem with the SQL Server please try again later”);mysql_select_db ($mysql_dbname) or die( mysql_error());Â
Now that we got that sorted out we can go and look at how to pull data from the database and how we can use it.
Â
SQL database.Â
First we need to get our SQL database ready, first create a table with 5 fields called.
ID.
Author.
Date.
Article.
Title.
Â
Make sure you have set ID to auto_increment, now add any news with any info to it on the database (Note: the date wont work yet since it will need to be input with php to then be read out again, but that’s not going be in this tutorial). If you do not know how to create the Mysql database, please check out our forum and post any questions you may have.
Â
MySql Query and MySql results.Â
Now we’re ready to start to get some query on the database and update our site with it.
When you want to pull some data out of a SQL database you need a query. A query is a lot like a search you tell the database what information you want out of it and it will return it to you for our Example we want everything from out table called news so the query will look like this.
Â
SELECT * FROM `news` LIMIT 0 , 30Â
As you can probably guess it says to select everything from the news table, the limit is for return purposes that will return the first 30 results. You can change that or get rid of it all together but is good when you have a lot of news and you want to have different pages showing more and more. Now we need to integrate that query into php so what we do is declare a variable and give it the query as value.
Â
$news_query = ‘SELECT * FROM `news` LIMIT 0 , 30’;Â
Next is the result variable.
Â
$news_result = mysql_query ($news_query, $connection) or die (mysql_error());Â
On this string you see that php actually calls out a mysql query using all the strings we have set up earlier, on the die section this time we don’t have a message but we have mysql_error() this will tell us what the error was if any.
Â
Â
The While loop.Â
This is the main part of the program, a while loop will go trough all of the info on the database and display them on screen, the while loop logic is very simple.
Â
while(condition){Â Â Â Â Â Â Â Â Â Â Â If the condition is through every thing between the {} brackets will be executed}Â
For this we are going to fetch results from the query we made earlier. Then there’s going to be results shown that will be looped, when no more results come up the loop will stop by itself, the syntax for this is.
Â
while($row = mysql_fetch_assoc($result))Â
Now all we have to do is echo the right data, we set up a table for the output as we saw at the beginning of the tutorial. On the first tutorial in our November 2006 issue we showed you how to display a php variable that was something like this
Â
Echo $variable;Â
Now we are going to mix the 2 together, having a variable and static data displayed on the screen, we do that by connecting with the echo, the process is really simple. In an echo everything that is between quotes single or double will be displayed as if it was typed, everything outside it will need to be a variable with a value that can be displayed. We can connect things together very simply, let’s say we have a value that gets changed depending on what the user types, so we have a html input form where a user can type in a number and we want to display on another page the “you have typed: X well doneâ€. X is where the number the user type would be displayed, to do so what we need to do is get the value and assign it to a variable that we are going to call $number, and then we are going echo everything like this.
Â
Echo ‘you have typed: ‘ . $number . ‘well done’; Â
So as you see here we have closed the quotes, then used a full stop before and after the variable, then we opened the quotes again, this is how you show static data and variables at the same time, now we can use the same idea for our script like this.
Â
echo’<tr><td align=”center”>’. $row[”title”] .’</td></tr><tr><td>’. $row[”article”].’</td></tr><tr><td align=”right”><font size=”1″>’.$row[”Author”].’</font></td></tr>‘;Â
As you can see here we open and close quotes a lot (Note: when you use echo to display html you always use single quotes for the echo otherwise you will encounter errors because double quotes are used in html as well), the $row[“titleâ€] means that we want to display from the last row of data only, the title information on the database you can change with anything you like, the final while loop will look like this.
Â
while($row = mysql_fetch_assoc($result)){Â Â Â Â Â Â Â Â Â Â Â echo’Â Â Â Â Â Â Â Â Â Â Â <tr><td align=”center”>’. $row[”title”] .’</td></tr>Â Â Â Â Â Â Â Â Â Â Â <tr><td>’. $row[”article”].’</td></tr>Â Â Â Â Â Â Â Â Â Â Â <tr><td align=”right”><font size=”1″>’.$row[”Author”].’</font></td></tr>Â Â Â Â Â Â Â Â Â Â Â ‘;}Â
This will display 30 news you add in the database, because if you remember on the sql query that we made earlier we had LIMIT 0, 30 you can change the limit to your liking and have different results, on the next tutorial I will show you how to make it dynamic so that we can have more pages of news and also how to display a single news, plans for now are to show first the basic, then when the basic are done we’ll create a fully featured blog that you can use on your website, it’s all for this tutorial, thanks for reading.