Well to solve some of your problems here are some easy guides on how to connect yo MySQL using PHP.
first of all this guides uses WAMP you can download it @ WAMP SERVER.
So after installing WAMP. You would first need to create a database. Its easy all you have to do is go to http://localhost/phpmyadmin and you can see the create database button there. Just input the database name you want then press create.
Afterwards you have to choose your database on the right side and lets start creating some table!
First create a simple notepad. And put this codes in side the notepad and save it as anything you want.
Create table member
(
m_id int unsigned not null auto_increment primary key,
username varchar(100) not null,
password varchar(100) not null
);
This means you will create a table with the name of member. With attributes m_id as a unsigned integer and its default value is not null this attribute also does auto_increment and is your primary key for this table. Next you have the m_username & m_password as varchar with a maximum character size of 100 and its not null too.
After saving this you will need to import them using phpMyAdmin. You can easily see the import button on the header of phpMyAdmin. After selecting the file and importing voila you have your very own table!
To simply add data into your table you must first creat a form. Well i know you can do this without me teaching you. So next after clicking on the submit button you would be directed to the next page and this is where the coding starts!
Lets store the datas posted first into variables.
$username = $_POST['username'];
$password = $_POST['password'];
username & password is not constant it simply depends on whats the name of the input you have coded in the form.
Next you have to conncet to a database.
$dbhost = ‘localhost’;
$dbuser = ‘dcwarez_karlpox’;
$dbpass = ’secret’;
$db=mysql_connect($dbhost, $dbuser, $dbpass) or die(’Error connecting to mysql’) or die(’Error : ‘ . mysql_error());
$dbname = ‘example’;
mysql_select_db($dbname) or die(’Error : ‘ . mysql_error());
Well this code simply means that i have connected to the mysql database with those information. And choose the example as my database (lets just say you have created example as database name from above). Now if you would probaby be wondering whats that dcwarez_karlpox is and secret. Well those are priveledges. You can create them on the phpMyAdmin home page. A priveledge link can be seen there and you have to create your own user. But you can still use the default user which is
$dbuser = ‘root’;
$dbpass = ”;
With those information above you can connect to a MySQL if you are using WAMP.
Now to add datas in your table you can simply do this query
$q="insert into member (m_username,m_password) values (’".$username."’, ‘".$password."’)";
$r=mysql_query($q);
Take not that i didnt include the m_id. Simply because its already auto_increment you dont have to add data into it anymore.
Next is to View data from a table you simply do this code.
$q="select * from member"
$r=mysql_query($q);
while($row=mysql_fetch_array($r,MYSQL_ASSOC))
{
echo ‘Username: ‘.$row['username'].’ Password: ‘.$row['password'].’<br/>’;
}
This code will output all the username and password under the member table. To get specific datas only you do this by using the where and like functions. Example.
$q="select * from member where m_username like ‘karlo’ and m_password like ’sobiaco’;
$r=mysql_query($q);
$row=mysql_fetch_array($r,MYSQL_ASSOC));
echo ‘Username: ‘.$row['username'].’ Password: ‘.$row['password'].’<br/>’;
Take note that i didnt use any looping for this. Simply because you are using a member table and you must not have any duplicates in right? Well its your job to find a way to avoid duplicates!
Next is to delete data from a database. You simply do this by this code.
$q="delete from member where m_username like ‘karlo’;
$r=mysql_query($q);
This code simply deletes all the rows in the table member that has a m_username of karlo.
Lastly we have the UPDATE. To do this just analyze the code below.
$q="update member set m_username = ‘karlpox’ where m_username like ‘karlo’";
$r=mysql_query($q);
This code simply updates all m_username with a value of karlo into karlpox. You can also update more datas by using this query.
$q="update member set m_username = ‘karlpox’, m_password=’12345′ where m_username like ‘karlo’";
Now that query replaces both the m_username & m_password of m_username’s which has a value of karlo.
Thats all!