In this tutorial I am going to try and teach you how to use mysql properly.
First of all, you need to know the basics.
To insert into a mysql database, you use a code similiar to this
CODE
<?
$query = mysql_query("INSERT INTO `users` (`username`, `password`, `email`) VALUES
('$username','$password','$email')");
?>
$query = mysql_query("INSERT INTO `users` (`username`, `password`, `email`) VALUES
('$username','$password','$email')");
?>
You can probably see what you need to change etc, But incase you dont. I am going
to explain anyway.
INSERT INTO users - change users to your table name
(username, password, email) - change to the fields in the table, you want to insert into
seperated with a comma and a space
('$username','$password','$email') - Sets what to insert into each field.
Next i will show you how to select from a database
CODE
<?
$select = mysql_query("SELECT * FROM `users`");
?>
$select = mysql_query("SELECT * FROM `users`");
?>
Now there is a few things you can do after that.
Here is two of them.
CODE
<?
$select = mysql_query("SELECT * FROM `users`");
$select = mysql_fetch_array($select);
?>
$select = mysql_query("SELECT * FROM `users`");
$select = mysql_fetch_array($select);
?>
This will select the information from the table you set in the mysql query.
CODE
<?
$select = mysql_query("SELECT * FROM `users`");
$select = mysql_num_rows($select);
?>
$select = mysql_query("SELECT * FROM `users`");
$select = mysql_num_rows($select);
?>
The above will count how many rows in the table you set in the mysql query.
Next i am going to show you how you can select certain information from the table.
CODE
<?
$select = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'");
$select = mysql_num_rows($select);
?>
$select = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'");
$select = mysql_num_rows($select);
?>
That will select from the table users, how many rows have the username $username.
You obviously have to set the variable $username.
Now im going to show you how to use a while.
CODE
<?
$select = mysql_query("SELECT * FROM `users`");
while($r = mysql_fetch_array($select))
{
echo("$r[username]<br>");
}
?>
$select = mysql_query("SELECT * FROM `users`");
while($r = mysql_fetch_array($select))
{
echo("$r[username]<br>");
}
?>
That would echo out, All the users in your database, one user on each line.
You can also set persific information in the while query aswell
Like i showed you earlier, You can select where username=adam127 etc.
Now we get a bit more advanced
Next, To update a row in a table, you will need a code similiar to the one below.
CODE
<?
$query = mysql_query("UPDATE `users` SET `username` = '$username' , `password` = '$password
WHERE `id` = '$id' ");
?>
$query = mysql_query("UPDATE `users` SET `username` = '$username' , `password` = '$password
WHERE `id` = '$id' ");
?>
You have to set the variables, but thats not what this tutorial is about
It explains most of this, further up in the tutorial
Now I will show you how to delete from a database.
You will have a code similiar to this one
CODE
<?
$query = mysql_query("DELETE FROM `users` WHERE `username` = '$username'");
?>
$query = mysql_query("DELETE FROM `users` WHERE `username` = '$username'");
?>
same as before, you should know what to change.
Okay, You know how to:
- Insert To Database
- Select From Database
- Update Database
- Delete From Database
Now im going to show you 1 more thing.
Im going to show you how to update all rows in the specified table.
You would use something like this
CODE
<?
$select = mysql_query("SELECT * FROM `users`");
$get = mysql_num_rows($select);
$update = mysql_query("UPDATE `users` SET `level` = '0' LIMIT $get");
?>
$select = mysql_query("SELECT * FROM `users`");
$get = mysql_num_rows($select);
$update = mysql_query("UPDATE `users` SET `level` = '0' LIMIT $get");
?>
That would set every user in the database, level to 0
Now theres one more thing im going to show you, and it comes in very useful sometimes.
or die(mysql_error());
Now you may already know this, but this it for...
if you have an error in your mysql query, it will die ( stop ) the script, and echo the error.
This is how you add it.. below is an example:
CODE
<?
$select = mysql_query("SELECT * FROM `users`") or die(mysql_error());
?>
$select = mysql_query("SELECT * FROM `users`") or die(mysql_error());
?>
I hope this tutorial Has helped you understand mysql if you didnt already know it.
Please post C & C Below
- Adam