Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Put it into MySQL

By Darrell Shifflett
Senior Editor, Linux-2000.org

Lets get started

First lets login to mysql.

  • mysql> create database news;
    Query OK, 1 row affected (0.00 sec)

    mysql> show databases;
    +-----------+
    | Database  |
    +-----------+
    | mysql     |
    | news      |
    | test      |
    +-----------+
    3 rows in set (0.00 sec)

    - Lets populate the news database -

    mysql> use news
    Database changed

    mysql> CREATE TABLE slashdot (
        -> id int(8) DEFAULT '0' NOT NULL auto_increment,
        -> Title varchar(80),
        -> Link varchar(80),
        -> Time varchar(30),
        -> Author varchar(30),
        -> Dept varchar(80),
        -> Category varchar(40),
        -> NumComments int(8) DEFAULT '0' NOT NULL,
        -> storytype varchar(40),
        -> imagename varchar(40),
        -> PRIMARY KEY (id)
        -> );
    Query OK, 0 rows affected (0.01 sec)

    mysql> show tables;
    +----------------+
    | Tables in news |
    +----------------+
    | slashdot       |
    +----------------+
    1 row in set (0.00 sec)
    mysql> describe slashdot;
    +-------------+-------------+------+-----+---------+----------------+
    | Field       | Type        | Null | Key | Default | Extra          |
    +-------------+-------------+------+-----+---------+----------------+
    | id          | int(8)      |      | PRI | 0       | auto_increment |
    | Title       | varchar(80) | YES  |     | NULL    |                |
    | Link        | varchar(80) | YES  |     | NULL    |                |
    | Time        | varchar(30) | YES  |     | NULL    |                |
    | Author      | varchar(30) | YES  |     | NULL    |                |
    | Dept        | varchar(80) | YES  |     | NULL    |                |
    | Category    | varchar(40) | YES  |     | NULL    |                |
    | NumComments | int(8)      |      |     | 0       |                |
    | storytype   | varchar(40) | YES  |     | NULL    |                |
    | imagename   | varchar(40) | YES  |     | NULL    |                |
    +-------------+-------------+------+-----+---------+----------------+
    10 rows in set (0.00 sec)

    mysql> flush privileges;
    Query OK, 0 rows affected (0.08 sec)

    mysql> \q
    Bye
  • Make sure you have privileges to write to database
  • This can be found in the MySQL documentation

Step 3:

  • Now to the PHP script
  • examples and documentation can be found at http://www.php.net/

-------------------------//// cut ////------------------------------
<?php

$db = mysql_connect("localhost","user_id","password");
mysql_select_db("news",$db);
$result = mysql_query("SELECT * FROM slashdot LIMIT
10",$db);
if ($myrow = mysql_fetch_array($result)) {
echo "<table border=3 bgcolor=\"#009999\">\n";
do {
printf("<tr><td><b>Category:</b> %s <a
href=\"$myrow[Link]\">\n
<font
color=\"#CFCFCF\">Slashdot.org</a><br></font>\n
<b>Topic:</b> %s<br></tr>\n",
$myrow[Category], $myrow[Title], $myrow[Link]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
} else {
echo "Sorry, no records were found!";
}

?>

-------------------------//// cut ////------------------------------

  • Lets name this 'slashnews.php3'
  • chmod 700 slashnews.php3
  • Security reasons because of passwords in the file
  • insert this into a *.php3 doc

<?
include("slashnews.php3");
?>

Of course you can make the above PHP script do what you need for your needs.