Beginners PHP 3: Introduction to MySQL
5 posts
Page 1 of 1
In the previous tutorial, you have learned about the ways you can use the POST function in PHP. In this tutorial, you will learn about using MySQL in your websites.
What will we learn today about MySQL?
You can either download it from: http://mysql.com/.
Or you can sign up at a webhost that provides free PHP and MySQL. I recommend 000Webhost, found here: http://000Webhost.com/
Lets get started!
MySQL is a database that you can use for websites. It stores information, for example: PHP forums run on a database. It can be MySQL, PostgreSQL, MsSQL, or any other database out there. I recommend MySQL because it's lightweight, easy to use, and secure.
The function to connect to a MySQL database is the mysql_connect($host, $user, $pass) function, and it has 3 parameters. They are:
To connect to a database, REQUIRED if you connect to a database, the syntax is mysql_select_db(string $db [, resource $link_identifier] ).
The parameters are:
The die() function:
If you want some text to appear, or anything like that, when your website fails at connecting to your MySQL or database, you will use the die() function.
It's used like:
The syntax used for executing sql queries is mysql_query(string $query [, resource $link_identifier ] )
The parameters for this are:
Now lets insert something into it.
Wrap it up
Well, we have now learned how to connect, select a database, die() function, and how to execute SQL queries into your database. Remember, this is only an introduction. You will learn more as you follow through in my tutorials.
Notes:
Thanks,
Alex.
What will we learn today about MySQL?
- How to connect to MySQL and a Database
- The die() function.
- How to execute SQL to your database.
- A MySQL database(No duh...)
You can either download it from: http://mysql.com/.
Or you can sign up at a webhost that provides free PHP and MySQL. I recommend 000Webhost, found here: http://000Webhost.com/
Lets get started!
MySQL is a database that you can use for websites. It stores information, for example: PHP forums run on a database. It can be MySQL, PostgreSQL, MsSQL, or any other database out there. I recommend MySQL because it's lightweight, easy to use, and secure.
The function to connect to a MySQL database is the mysql_connect($host, $user, $pass) function, and it has 3 parameters. They are:
- $host: The host of the database. If you downloaded it, it will be "localhost", if you got 000Webhost, it will be something like "mysql06.000webhost.com" or any other server, like mysql07.
- $user: The username for your database. If you downloaded it, it will be "root". If you got 000Webhost, it will be whatever you set it to be.
- $pass: The password for your database. If you downloaded it, it will be what you set it to be. If you got 000Webhost, it will also be what you set it to be.
To connect to a database, REQUIRED if you connect to a database, the syntax is mysql_select_db(string $db [, resource $link_identifier] ).
The parameters are:
- $db = The name of the database, or schema, you are connecting to. Must be lowercased.
- $link_identifier= The name of the connection you're using. (OPTIONAL)
Code: Select all
Then the syntax for connecting to a database would be:
<?php
$connecttodb = mysql_connect("localhost","root","password");
?>
Code: Select all
If you don't set mysql_connect("localhost","root","password"); to a variable, then you don't need to use the $link_identifier paramater.mysql_select_db("testdb",$connecttodb);
The die() function:
If you want some text to appear, or anything like that, when your website fails at connecting to your MySQL or database, you will use the die() function.
It's used like:
Code: Select all
Same can be used for selecting the database.
mysql_connect("localhost","root","password") or die("Could not connect");
Code: Select all
Executing SQL Queries:mysql_select_db("testdb") or die("Could not select database");
The syntax used for executing sql queries is mysql_query(string $query [, resource $link_identifier ] )
The parameters for this are:
- $query: The query you are identifying.
- $link_identifier: Same as mysql_select_db. (OPTIONAL)
Code: Select all
This creates a table, Users with 3 fields.
mysql_query("CREATE TABLE users (
id int(11) AUTO_INCREMENT NOT NULL,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY(id)
);");
- id: integer(11) as you insert something into it, it auto increments, and it cannot be null.
- username: varchar(255) and it cannot be null.
- password: varchar(255) and it cannot be null.
Now lets insert something into it.
Code: Select all
This inserts into the users table with the values of "alex" and "password" into "username" and "password".mysql_query("INSERT INTO users (username, password) VALUES ('alex','password')");
Wrap it up
Well, we have now learned how to connect, select a database, die() function, and how to execute SQL queries into your database. Remember, this is only an introduction. You will learn more as you follow through in my tutorials.
Notes:
- Connect to MySQL using the mysql_connect($host, $user, $pass) function.
- Connect to a MySQL database using the mysql_select_db(string $db [, resource $link_identifier] ) function.
- The die() function uses "or" inbetween functions.
- Execute DB queries using the mysql_query(string $query [, resource $link_identifier] ) function.
Thanks,
Alex.
<?php
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
I love it, but can you post tutorials for databases in Visual Basic .NET too?
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Your format of tuts is really nice. Makes it more attractive!
Please become more active and post tutorials about php
You are a very good article writer ! such good tutorials are very rare these times
You are a very good article writer ! such good tutorials are very rare these times
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023