Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP & MySQL] Tutorials

Status
Not open for further replies.
Joined
May 19, 2007
Messages
440
Reaction score
78
[PHP & MySQL] Tutorials & Examples

1. Inserting data to MySQL Table. (MySQL - Insert)
2. Select from MySQL Table WHERE. (MySQL - Where & Fetch)
3. Updating existing MySQL row. (MySQL - Update)
4. Deleting specific content from table. (MySQL - Delete)
5. What is MySQL die command? (MySQL Error Reporting)
6. How to do MySQL Configuration file? (Misc)
6B. Requiring the Configuration file? (Misc)
6C. How to use MySQL Configuration file with ex. MySQL connection? (Misc)
7. Limit for "SELECT * FROM" Query. (MySQL - Where & Fetch Result Limit)
8. Connect to IP AND Port. (MySQL - FSockOpen)
9. Setting cookie. (PHP Cookies)
9B. Page restriction by cookie. (PHP Cookie REQUIRED)

1. Inserting data to MySQL Table.

PHP:
<?php
// Connecting to the MySQL Server with specific command.
mysql_connect("Connection", "MySQLUser", "MySQLPassword") or die(mysql_error());
// Selecting MySQL Database.
mysql_select_db("DBExample") or die(mysql_error());
// MySQL "Action" the (QueryString)
mysql_query("INSERT INTO Example (ColumnOne, ColumnTwo) VALUES('ColumnOnesValue', 'ColumnTwosValue')") or die(mysql_error());
?>

2. Select from table WHERE.

PHP:
<?php
// Connecting to the MySQL Server with specific command.
mysql_connect("Connection", "MySQLUser", "MySQLPassword") or die(mysql_error());
// Selecting MySQL Database.
mysql_select_db("DBExample") or die(mysql_error());
// MySQL "Action" the (QueryString)
$shortcut = mysql_query("SELECT * FROM Example WHERE ColumnOne='ColumnOnesValue'") or die(mysql_error());
while($row = mysql_fetch_array($shortcut)) {
// Showing results from column one.
echo $row['ColumnOne'];
// New line for another result.
echo '<br />';
// Showing results from column two.
echo $row['ColumnTwo'];
}
?>

3. Updating existing row in table.

PHP:
<?php
// Connecting to the MySQL Server with specific command.
mysql_connect("Connection", "MySQLUser", "MySQLPassword") or die(mysql_error());
// Selecting MySQL Database.
mysql_select_db("DBExample") or die(mysql_error());
// MySQL "Action" the (QueryString)
mysql_query("UPDATE Example SET ColumnTwo='Value' WHERE ColumnOne='AnotherValue'") or die(mysql_error());
?>

4. Deleting specific content from table.

PHP:
<?php
// Connecting to the MySQL Server with specific command.
mysql_connect("Connection", "MySQLUser", "MySQLPassword") or die(mysql_error());
// Selecting MySQL Database.
mysql_select_db("DBExample") or die(mysql_error());
// MySQL "Action" the (QueryString)
mysql_query("DELETE FROM Example WHERE ColumnOne='Value'") or die(mysql_error());
?>

5. What is this "or die(mysql_error());" ?
If your code doesn't work correctly it will report you information about it what's wrong.

6. Creating a MySQL Configuration File.

PHP:
<?php
// Creating shortcuts with values.
$Con = "Connection";
$User = "Username";
$Pass = "Password";
$DB = "Database";
?>

6B. How you can use it?
Everywhere you want to use it you must require the configuration file to receive every "shortcut" to the current document.
PHP:
<?php require("ConfigurationFile.php"); ?>

6C. Using a MySQL Configuration File with MySQL Connection.

PHP:
<?php
// Requiring the configuration file.
// This code must be (in the page) which uses "shortcuts"
require('Path/ConfigurationFile.php');

// Then... connection with it..
mysql_connect($Con, $User, $Pass) or die(mysql_error());
?>

7. Limit for "SELECT * FROM" Query.

PHP:
// Just simply place "LIMIT {Amount}" to query.
mysql_query("SELECT * FROM Example WHERE ColumnOne='ColumnOnesValue' LIMIT 5")

8. Connect to IP AND Port.
PHP:
<?php
// Renaming connection command as "$Server".
$Server = fsockopen("YourIP", "Port", $ErrNo, $ErrStr, 5)
// Connecting to the server using "shortcut" $Server.
if($Server) {
// Show text "Online" if connection success.
echo "Online";
}
else
{
// Show text "Offline" if connection failed.
echo "Offline";
}
?>

9. Setting the cookie.

PHP:
<?php
// Cookies are very simple.
setcookie("CookieName", "ValueOfCookie", time()+86000)
?>

9B. Page need cookie to be shown.

PHP:
<?php
// Checking if cookie = true then...
if(!isset($_COOKIE['CookieName'])) {
die('You need cookie to see this page.');
}
else
{
// Do nothing. In other words continue executing other scripts and page.
}
?>

This is for advanced coders.
The reason why i'm not using if(!$Con) { commands etc functions with my code in this thread is that, because all of those functions confuses new PHP coders.

Never is not too late for learning.
First learn all of these basics and after that... grinding the code.
 
Last edited:
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Nice tutorial ;)

Updating existing table.
I think, should be,

Updating Rows in Existing Table.

Because it might be confused with "Alter Table".

Consider adding, 'Create Table', 'Alter Table' and 'Drop Table'.

Maybe add a little more referencing, and description. The comments are good.
 
Joined
May 19, 2007
Messages
440
Reaction score
78
Nice turtorial :) I liek this one.
Thank you for commenting.

Nice tutorial ;)


I think, should be,

Updating Rows in Existing Table.

Because it might be confused with "Alter Table".

Consider adding, 'Create Table', 'Alter Table' and 'Drop Table'.

Maybe add a little more referencing, and description. The comments are good.
Thank you for suggestion i modified it. :)
 
Elite Diviner
Joined
May 26, 2009
Messages
428
Reaction score
16
Is it necessary to include the mysql_close function?

Btw, check your variables in the select from table.

Code:
$shortcut = mysql_query("SELECT * FROM Example WHERE ColumnOne='ColumnOnesValue'") or die(mysql_error());
[u]$onerow[/u] = mysql_fetch_array($shortcut);
// We show results WHERE ColumnOne is ColumnOnesValue
echo [u]$row[/u]['ColumnOne'];
// We show our result's another column
echo [u]$row[/u]['ColumnTwo'];

Shouldn't the variables $row be $onerow or the $onerow be $row?
 
Last edited:

Zen

Custom Title Activated
Loyal Member
Joined
Dec 2, 2006
Messages
1,621
Reaction score
152
Is it necessary to include the mysql_close function?

Btw, check your variables in the select from table.

Code:
$shortcut = mysql_query("SELECT * FROM Example WHERE ColumnOne='ColumnOnesValue'") or die(mysql_error());
[U]$onerow[/U] = mysql_fetch_array($shortcut);
// We show results WHERE ColumnOne is ColumnOnesValue
echo [U]$row[/U]['ColumnOne'];
// We show our result's another column
echo [U]$row[/U]['ColumnTwo'];
Shouldn't the variables $row be $onerow or the $onerow be $row?

depends what you're doing, but its good practice to end your mysql session when you've finished with it
 
Joined
May 19, 2007
Messages
440
Reaction score
78
Thank you guys for reporting a small kind of problems for me.
Problem was that, because i didn't tested a code and i took them directly from my head.

I'm starting update this thread.
 
Joined
May 19, 2007
Messages
440
Reaction score
78
Is it necessary to include the mysql_close function?

Btw, check your variables in the select from table.

Code:
$shortcut = mysql_query("SELECT * FROM Example WHERE ColumnOne='ColumnOnesValue'") or die(mysql_error());
[u]$onerow[/u] = mysql_fetch_array($shortcut);
// We show results WHERE ColumnOne is ColumnOnesValue
echo [u]$row[/u]['ColumnOne'];
// We show our result's another column
echo [u]$row[/u]['ColumnTwo'];

Shouldn't the variables $row be $onerow or the $onerow be $row?

It's fixed now. Thank you for reporting.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Re: [PHP & MySQL] Tutorials & Examples

Great improvements! This is a prime example of the best part of this tutorial's section. Tutorials can always get better!

I also have a new suggestion or two.
PHP:
<?php
if($_COOKIE['CookieName'] == "") {
die('You need cookie to see this page.');
}
else
{
// Do nothing. In other words continue executing other scripts and page.
}
?>
This is for advanced coders.
The reason why i'm not using if(!$Con) { commands etc functions with my code in this thread is that, because all of those functions confuses new PHP coders.

Never is not too late for learning.
First learn all of these basics and after that... grinding the code.
I think you should only use the not in the conditional statement with isset. The other one isn't quite right. You're checking if the cookie is defined, and with a blank value. In PHP, the cookie gets defined inside the condition itself, the moment it reads the $_COOKIE['etc'], the 'etc' cookie is defined with a blank value (which is why it works). With the isset function, it's a little more clear what's going on in the script.

PHP is a strange language- but easy for people learning a first scripting/programming language.


Also, PHP's mysqli extension is (rightfully so) becoming more popular these days which has a greater support for MySQL 5 and PHP5's new OOP features.

It's highly recommended over the mysql extension by PHP.net
 
Last edited:
Status
Not open for further replies.
Back
Top