Archive for the ‘PHP MySQL’ Category

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 iPower, 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’.

Disclosure: We are compensated for our reviews. Click here for details.

PHP/MySQL Tutorial Part 1

I started using PHP with MySQL a few years ago.  I found that the world of PHP has no limit at all!  You can pretty much do anything you want.

And because it is so large, people start telling me “I bought a book because I want to learn PHP.  But I just have no clue of where to start.”. 

The most important feature that PHP provides is its robustness when it comes to interacting with the database.  And yet, this is always the most desirable feature used getting to web programming.

Hence I am writing this tutorial.  I am sure you can find millions of similar tutorials on the internet.  But well, doesn’t hurt to have another one.

First of all, what sites would need a database?

  • Forums
  • Blogs
  • Shopping sites
  • Banners

After all, these sites only do a few actions – add information, read information, edit information, and delete information to or from the database.  Hence my conclusion is: you need to learn how to add/read/edit/delete to start with.

Anything else needed?  Yes, and I have listed them below:

  • A Linux server with Apache installed, OR
  • A linux hosting account.  You can get one cheaply with a domain name here.  Obviously this is the easier option.
  • A computer with notepad or any other text editor.  However, do not use Word to edit codes.  It is going to give you a lot of headache if you do.  If you are going to program a complete website with proper layout etc, I would recommend Macromedia Adobe Dreamweaver.
  • A file transfer program like WS_FTP or Filezilla. 

Are you sure you have everything ready?  Go on and test it by placing this code into an empty file:

<?php
phpinfo();
?>

Save the file as phpinfo.php.  Try to access this page with a browser – if things go well, you would see a page with the information of the server.  And you have already written your first line of PHP code!

Disclosure: We are compensated for our reviews. Click here for details.