Author Topic: Connecting, Inserting Data and Showing Data from MySQL  (Read 1619 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Connecting, Inserting Data and Showing Data from MySQL
« 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 :)


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: [Select]
<?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: [Select]
<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: [Select]
<?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: [Select]
<?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

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Connecting, Inserting Data and Showing Data from MySQL
« 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: [Select]
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.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal