PHP/MySQL Tutorial – PHP MySQL Connection

By | March 13, 2013

PHP/ My SQL Tutorial – PHP/My SQL Connection

Displaying My SQL Table Data

Step 4: After creating a table and entering the data its proper display is important. This can be done with the help of basic HTML code which summon to PHP script.

Let’s take an example and start from beginning, fill a new database table with a data.

The given HTML code will assemble the data from the text boxes and will surpass it to PHP script.

<form action=”insert.php” method=”post”>
Value1: <input type=”text” name=”field1-name”><br>
Value2: <input type=”text” name=”field2-name”><br>
Value3: <input type=”text” name=”field3-name”><br>
Value4: <input type=”text” name=”field4-name”><br>
Value5: <input type=”text” name=”field5-name”><br>
<input type=”Submit”>
</form>

In the next step you require a new PHP script to insert the data in the database

<?php
$username=”username”;
$password=”password”;
$database=”your_database”;

$field1-name=$_POST[‘Value1’];
$field2-name=$_POST[‘Value2’];
$field3-name=$_POST[‘Value3’];
$field4-name=$_POST[‘Value4’];
$field5-name=$_POST[‘Value5’];

mysql_connect (localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);

$query = “INSERT INTO tablename VALUES
(”,’$field1-name’,’$field2-name’,’$field3-name’,’$field4-name’,’$field5-name’)”;

mysql_query ($query);

mysql_close ();
?>

Save the script as insert.php so, that it can be easily recognized as HTML form.

Now you should have at least one record in your database, so that you can know how to output this data with the help of PHP. We provide cheapest web hosting in india

The first command that you require is select it from My SQL statement:

SELECT * FROM table name

It is the basic My SQL query which informs script to choose the entire database from the tablename table. As soon as query will be executed the result gets allocated to variable:

$query=”SELECT * FROM tablename”;
$result=mysql_query($query);

The whole substance of the table is now added in PHP array named $ result. Change every single piece into a separate variable, before giving output of this data. It includes two steps:

The first steps include counting the number of rows. Before going through the data of your result, count the number of database rows. You can add this in coded form but it’s not as safe as the script code has to change every time, when new row is added, in place of it use the command:

$num=mysql_numrows ($result);

The $num value represents the number of rows accumulated in $result. And this information will be used in loop to get all data and to display it on the monitor.

The second step includes setting up the loop. It consumes each row of the result and gets the data printed from there. In the code given below, $i show the number of times loops runs, so, in this way all the results are shown:

$i=0;
while ($i < $num) {

CODE

$i++;
}

This is the fundamental PHP loop which will execute codes in correct number of times. In every step $i will be incremented by one. This is valuable as $i inform the script which line of the results must be read. It performs accurately, if we get output 0 as mentioned in first line of My SQL.

The last and final part of output script allocates each piece of data its own variable:

$variable=mysql_result ($result,$i,”fieldname”);

So, for including each separate piece of data into our database, we would proceed the following way:

$field1-name=mysql_result($result,$i,”field1-name”);
$field2-name=mysql_result($result,$i,”field2-name”);
$field3-name=mysql_result($result,$i,”field3-name”);
$field4-name=mysql_result($result,$i,”field4-name”);
$field5-name=mysql_result($result,$i,”field5-name”);

You need not require to get the ID field as it has it plays no role in the output page. We provide Email Hosting India

Write a complete script of output data, as this script data is not formatted when it is send for printing:

<? php
$username=”username”;
$password=”password”;
$database=”your_database”;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=”SELECT * FROM tablename”;
$result=mysql_query($query);

$num=mysql_numrows ($result);

mysql_close ();

Echo “<b><centre>Database Output</centre></b><br><br>”;

$i=0;
while ($i < $num) {

$field1-name=mysql_result($result,$i,”field1-name”);
$field2-name=mysql_result($result,$i,”field2-name”);
$field3-name=mysql_result($result,$i,”field3-name”);
$field4-name=mysql_result($result,$i,”field4-name”);
$field5-name=mysql_result($result,$i,”field5-name”);

echo “<b>$field1-name 
$field2-name2</b><br>$field3-name<br>$field4-name<br>$field5-name<hr><br>”;

$i++;
}

?>

This output shows the list of all values saved in database and will give you the fundamental output. Not fruitful for working websites. Still you can format it into table form and display information in it that would be better. Formatting is not a tough and complicated process but the only thing you have to do is use HTML to print the result and add variables in appropriate spaces. You can do it easily by closing your PHP tag and enter HTML normally. The moment you reach a variable position proceed as follows:

<? echo $variablename; ?>

Use code in correct place

With the help of PHP loop repeat the correct code and add it as a part of larger table. The final result will be:

<html>
<body>
<?php
$username=”username”;
$password=”password”;
$database=”your_database”;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=”SELECT * FROM tablename”;
$result=mysql_query($query);

$num=mysql_numrows ($result);

mysql_close();
?>
<table border=”0″ cell spacing=”2″ cell padding=”2″>
<tr>
<td><font face=”Arial, Helvetica, sans-serif”>Value1</font></td>
<td><font face=”Arial, Helvetica, sans-serif”>Value2</font></td>
<td><font face=”Arial, Helvetica, sans-serif”>Value3</font></td>
<td><font face=”Arial, Helvetica, sans-serif”>Value4</font></td>
<td><font face=”Arial, Helvetica, sans-serif”>Value5</font></td>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,”field1″);
$f2=mysql_result($result,$i,”field2″);
$f3=mysql_result($result,$i,”field3″);
$f4=mysql_result($result,$i,”field4″);
$f5=mysql_result($result,$i,”field5″);
?>

<tr>
<td><font face=”Arial, Helvetica, sans-serif”><?php echo $f1; ?></font></td>
<td><font face=”Arial, Helvetica, sans-serif”><?php echo $f2; ?></font></td>
<td><font face=”Arial, Helvetica, sans-serif”><?php echo $f3; ?></font></td>
<td><font face=”Arial, Helvetica, sans-serif”><?php echo $f4; ?></font></td>
<td><font face=”Arial, Helvetica, sans-serif”><?php echo $f5; ?></font></td>
</tr>

<? php
$i++;
}
?>
</body>
</html>

This code will take out the print of table content and include an additional row for each record in the database, formatting Data in its printed form.

Leave a Reply