Posts Tagged ‘PHP MySQL’

PHP Session Problem

If you wrote websites in PHP, you would very likely be using PHP Session. Using PHP session allows you to preserve certain data across subsequent accesses.  Visitor of your site would be assigned a unique session id and would be propagated throughout his / her browsing session.

However, a lot of inexperienced PHP programmers would have problems using it.  They might find it difficult to carry the session id to the other pages, or simply, no session id assigned.

A quick checklist if you face this issue:

  1. Make sure <?php session_start(); ?> is at the very first line of the page
  2. Check your php.ini file and make sure session.use_only_cookies=1

PHP/MySQL Tutorial Part 4 – Retrieve Data

To retrieve after inserting them (see older post PHP/MySQL Tutorial Part 3), simply select the record from the desired table.  Use this SQL statement:

SELECT * FROM table_name;

Eg.
$result = mysql_query(“SELECT * FROM tablename”);
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_assoc($result))
{
echo $row['name'];
echo “-”;
echo $row['email'];
}

This would display:

Terence – terence@someemail.com
Johnny – johnny@otheremail.com

You can control the results with varying the statement. Eg.

SELECT name FROM tablename – retreives the name column only
SELECT * FROM tablename ORDER BY id DESC – shows last record first
SELECT * FROM tablename LIMIT 5 – shows the first 5 results only

PHP/MySQL Tutorial Part 3 – Insert Records

After connecting to the database (see older post PHP/MySQL Tutorial Part 2), the next step would probably be inserting data into it.

It is actually pretty simple.  The steps are:

1. Connect to the database
2. Select the right table
3. put the data in

You would need to setup a form on your website.  Let’s start with a simple one which contains name and email addresses only.  Use “POST” and when the form is submitted, send the data to “insert.php”.

And in insert.php, first use “POST” to gather the data, then use the following SQL statement:

$sql = “INSERT INTO table_name (name,email) VALUES ($name, $email)”;
$result = mysql_query($sql);

That’s done.  Simple is it?

 

PHP/MySQL Tutorial Part 2 – Connect to Database

After you have verified that your server supports PHP (see older post PHP/MySQL Tutorial Part 1), you can go ahead to create a database, and connect to it.  Usually Linux server packages comes with tools like cPanel, where you can create a database, a user and a password on a web interface.  It is relatively simple, and hence we are not going to discuss it here.

To connect to a database, you would need to know:

1. Host address – 95% is ‘localhost’ but sometimes it can be different.  Eg. for iPowerWeb, it should be ‘username.ipowermysql.com’.
2. Database name
3. A username
4. A password

It is easier to start with a code snippet:

<?php
$dbhost = ‘localhost’;
$dbname = ‘databasename’;
$dbuser = ‘username’;
$dbpass = ‘password’;

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to database’);

mysql_select_db($dbname);
?>

The first part of the code specifies the host address, username and password of the database.  The second part basically connects to the database.  If it failed, it would output ‘Error connecting to database’.

The last line of the code simply selects the database to connect to.

It is also a good idea to close the connection after any operation is done.  Use this code:

<?php

mysql_close($conn);
?>

Usually we would put the first code in a file named ‘connect.php’ or ‘config.php’, and include it at the beginning of the file.  At the end of the file, we would include the last code which is saved in a file named ‘close.php’.