MySQL Tutorial

Bobswat

New Member
Messages
104
Reaction score
0
Points
0
MySQL: The Basics
Covered in this: I am going to attempt to explain data insertion, Update queries, filtering clauses, all the parts of a query, and probably more, I just forgot what else. Please bear with me on this, it's my first tutorial, and I'll probably edit it a million times.


MySQL and its uses
: MySQL Is the language you use to communicate with a Database(DB). So say you have a basic form, and you want to add members to your website after you create the login system. You would want the members and their information added to the database after each account is registered. Below is an example of a simple MySQL query used along with php.

PHP:
<?php
include('yourconnection.php');		   
session_start();							 
$username = $_POST['username'];	   
$password = $_POST['password'];	   

mysql_query("INSERT INTO `table name`(`username`, `password`) VALUES('$username', '$password')") or die(mysql_error());

?>


ok, so now we'll go over each part of the query


1. mysql_query() - The php function used for a MySQL query

2. INSERT INTO - This is the actual action of the query
some other examples are:
SELECT - Used to get information from the DB
UPDATE - Used to update existing information
INSERT - used above, to add data to a table

3. `table name` - the name of the table you are modifying

4. (`username`, `password`) - The names of the rows the data will go into, these are only used in an INSERT INTO query, and they are optional, its best to use them though, so if your table changes, the data will still be inserted into the correct row.

5. VALUES('$username', '$password') - this is the data to be inserted in the table, they correspond with the previous piece of code, such that:
`username` will be given the value '$username' If the optional item is not used, then it will go in order of the rows in your table. which is why you should specify the optional, incase you change the rows in your table.

6. or die(mysql_error()); - If there is an error with your query code, a mysql error will be generated telling you the problem with it. ALWAYS USE THIS AFTER ANY QUERY.

Examples of other queries:
SELECT - SELECT `row name` FROM `table name`

UPDATE - UPDATE `table name` WHERE `row name` = '$variable'

Data Filtering:

there are certain clauses we can use to filter data the way we want it such as:

WHERE - used in the above example, it says to update the table SO the row you choose is given the value of the variable

LIKE - used mainly with wildcards and searches on a site



If anybody would like me to continue and add more things to this, post a reply and tell me so, also if anyone would like help with anything MySQL, PHP, or HTML related send me a message via aim, my screen name is
bob from walton
,Sorry, I just got bored of writing for now, tell me what else you need help with and ill add it to this ASAP.

 
Last edited:

Bobswat

New Member
Messages
104
Reaction score
0
Points
0
Is this helpful for anyone? if not ill just not worry about adding anymore to it
 

Matthews255

New Member
Messages
826
Reaction score
0
Points
0
Please add more, its a really good tut.

If you put that on a web page and submit it to Pixel2Life it would be cool.

you could say, seen it here first.


EDIT: what would be REALLY helpful is a code to delete posts from my sql tables.

Like deleting entrys from my news database.
 
Last edited:

Bobswat

New Member
Messages
104
Reaction score
0
Points
0
Well to delete entires you would do:

PHP:
<?php
$value = 'value';

mysql_query("DELETE from `tablename` WHERE `fieldname`= '$value' LIMIT 1");
?>

you would just change the value of the $value variable to whatever criteria you need.

DELETE from is the command, obviously to delete.

`tablename` is the name of the table on the database you are deleting the entry from

WHERE
is the filtering agent to declare which row to delete

`fieldname`
is the field you are searching by

`value`
is the value that the `fieldname` holds in the row, the one which you wish to delete

LIMIT 1
is sort of a safety measure to make sure that you don't delete like 500 rows that match the criteria from your table, unless thats what you are trying to do. Something such as deleting all records where the `fieldname` is equal to lets say '4' would be a situation where you would not do any sort of LIMIT.

any other suggestions are welcome. If this tutorial is helping any of you guys, then please let me know by posting. Also post any requests that you would like in here, it doesn't just have to pertain to just MySQL alone, anything php or html is also acceptable and if I get enough requests I will do a php tutorial also.
 
Last edited:

klnce

New Member
Messages
35
Reaction score
0
Points
0
i need that config file. Because i need to connect to mysql from php or by asp.......... plz post it........



My site is

http://klnce.elementfx.com
Edit:
i need that config file. Because i need to connect to mysql from php or by asp.......... plz post it........
 
Last edited:

Bobswat

New Member
Messages
104
Reaction score
0
Points
0
Here is a copy of the file I use:

PHP:
<?php
$username = 'your_username_here';
$password = 'your_password_here';
$host = 'localhost';
$database = 'your_database_here';

mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

?>

use
PHP:
include('path_to_connection');
at the top of all the files that need MySQL access


That will work for php, and as of now, x10 does NOT support asp, at least thats what ive read in the forums. You may PM them and ask for it to be added to your account, they have done it on an account-by-account basis before.

good luck!


Anyone else need anything? Feel free to post in here
 
Last edited:

port5900

New Member
Messages
150
Reaction score
0
Points
0
Thank you, MySql is some thing I was staying away from till now.
 
Top