BBGameZone.net PBBG Network | BuildingBrowserGames | Top-PBBG
March 11, 2010, 07:10:29 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Our Scripts Vault contains many game scripts that you can use to create your own game!
 
  Home   Forum   Help Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Connecting, Inserting Data and Showing Data from MySQL  (Read 792 times)
Zeggy
Global Moderator
Level 35
*****

Reputation: 10
Offline Offline

Posts: 1,148



View Profile WWW
« on: January 29, 2007, 06:27:37 AM »

This is a tutorial written by FarooqAzam from Betlik.com

This tutorial is for Connecting,Inserting Data,Selecting and Showing Data from mysql database. So lets begin Smiley


Step 1: Connecting.
----------------------------------------------------------------
First of all open your editor, i use Dreamweaver and also suggest you to use it. Run apache. Make a new file "config.php" and add the following code:-
PHP Code:
Code:
<?php
 
// Declare some variables
$host "your host";
$username "Database username";
$password "Databse password";
$db "Database Name";
 
// Connecting
$conn = @mysql_connect($host,$username,$password);
 
// If mysql connecting doesnt work
if(!$connection) {
// Then show the following text
exit("Can not connect to MySQL");

 
// If Mysqldatabase doesn't exist or if any errors then
if(!@mysql_select_db($db,$conn ) { 
 
// Show the following text:
exit("Can not select the MySQL DB");
}
?>

Save the file as "config.php"


Step 2: Inserting Data
--------------------------------------------------

Make a file and name it "forms.php" and insert the following html code:-


HTML Code:

Code:
<html>
<head>
<title>Forms</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<form name="form1" method="post" action="insert.php">
<p>Name:-<br>
<input name="name" type="text" id="name">
<br>
<br>
Age:-<br>
<input name="age" type="text" id="age">
<br>
<br>
<input type="submit" name="Submit">
</p>
</form>
</body>
</html>


In "forms.php" when you add the html code you will see two text fields and a submit button.

Ok, now make another file "insert.php" and add the following code:-

PHP Code:
Code:
<?php
 
// Include the Mysql Connecting file (config.php)
include "config.php"
 
$name $_POST['name'];
$age $_POST['age'];
 
//Insert into Mysql Database:
mysql_query("INSERT INTO tablename (name,age) VALUES( " $name ", " $age ")"); 
 
// Show a alert when data inserted to Mysql.
echo "<script language=javascript>alert('Data inserted to Mysql Databse!'); window.location = 'forms.php'; </script>";
?>


Selecting and Showing Data From Mysql Databse:-
----------------------------------------------------------------

First of all, make a new file and name it "index.php" and add the following code:-

PHP Code:
Code:
<?php
 
// Include Mysql Connecting file (config.php)
include "config.php";
 
// Selecting data from Database:
// Tip: Asteric (*) is used to select all the rows in the table name.
$result mysql_query("SELECT * FROM tablename");
while (
$show mysql_fetch_array($result)) {
 
// Write the Name and Age inserted into Mysql Database
echo "Name: " $show['name'];
echo 
"Age: " $show['age'];
 
}
 
?>

Tutorial Done!

If you have any questions don't hesitate to ask

Thanks
Logged
Sinzygy
Level 28
**

Reputation: 11
Offline Offline

Posts: 417



View Profile
« Reply #1 on: February 24, 2007, 02:32:04 PM »

Here's a small addition to this tutorial:

If you're a lazy bastard like I am you might want to save / use the following functions:

Code:
function dofullquery($query)
{
    include ('connect.php');
    $sqlquery = mysql_query($query) or die(mysql_error());
    $sqlquery = mysql_fetch_array($sqlquery);
    return $sqlquery;
}

function doquery($query)
{
    include ('connect.php');
    $sqlquery = mysql_query($query) or die(mysql_error());
   
    return $sqlquery;
}

the "dofullquery" function returns the whole array while the "doquery" function just makes the query. Of course, these aren't big fancy functions but for beginners it's quite a nice shortcut since the error message will be displayed if something goes wrong.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
SimplePortal 2.2 © 2008-2009
Valid XHTML 1.0! Valid CSS!