Get support from Xavier Media
It is currently Sun Dec 08, 2013 12:26 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Mysql Connections Through PHP
PostPosted: Sun Aug 27, 2006 12:21 pm 

Points:
This tutorial is about connecting to a Mysql Database so that you can display data from the database like (in this example) products from a store.

This time I will write the code in single steps.
Firstly the skeleton.

Code:
<html>
<head>

</head>

<body>

</body>
</html>


Were using the tag head to put the connection so that we do not get confused with the connection and the presentation of the data.

Next, let's connect to the mysql server.

[php]
<html>
<head>
<?
$mysql_server = "localhost";
$mysql_user = "root";
$mysql_password = "12345";

$connect = mysql_connect ( $mysql_server, $mysql_user, $mysql_password) or die ("Could not connect to database!");

?>
</head>

<body>

</body>
</html>
[/php]


Now we are connected to the database: In this case we are connected to a locahost mysql server with the username root and the password 12345. Now we need to connect to a database.


[php]
<html>
<head>
<?
$mysql_server = "localhost";
$mysql_user = "root";
$mysql_password = "12345";

$database = "store_db";

$connect = mysql_connect ( $mysql_server, $mysql_user, $mysql_password) or die ("Could not connect to database!");

mysql_select_db ($database) or die ("Could not connect to Table!");

?>
</head>

<body>

</body>
</html>
[/php]

Now we are connected to the database called store_db! But we still need to connect to a table, so let's finish the <head> part.

[php]
<html>
<head>
<?
$mysql_server = "localhost";
$mysql_user = "root";
$mysql_password = "12345";

$database = "store_db";

$connect = mysql_connect ( $mysql_server, $mysql_user, $mysql_password) or die ("Could not connect to database!");

mysql_select_db ($database) or die ("Could not connect to Table!");

$question = "select * from products";

$answer = mysql_query ($question) or die ("Query error!");

$data = mysql_fetch_row($answer)

?>
</head>

<body>

</body>
</html>
[/php]

Now we added the sql in the variable $question so that we select all of the data in the table products. The answer picks the results and the variable data gets the result. We now have data from the database in the arrays of $data. Let's print something.

[php]<html>
<head>
<?

$mysql_server = "localhost";
$mysql_user = "root";
$mysql_password = "12345";

$database = "store_db";

$connect = mysql_connect ( $mysql_server, $mysql_user, $mysql_password) or die ("Could not connect to database!");

mysql_select_db ($database) or die ("Could not connect to Table!");

$question = "select * from products";

$answer = mysql_query ($question) or die ("Query error!");

$data = mysql_fetch_row($answer)

?>
</head>

<body>

<?

echo "Here there are our products:<br>";
echo $data[1];
echo $data[2];

?>[/php]

Now we printed the name and description of the product ( in the database the first column was the id of the product and was stored in the $data[0], the name of the product was stored in second column $data[1] and in the third column there was the description, $data[2]).

But we only printed one of the products, now imagine that you wanted to print the whole products in your database, then, the code will be like this:

[php]<html>
<head>
<?

$mysql_server = "localhost";
$mysql_user = "root";
$mysql_password = "12345";

$database = "store_db";

$connect = mysql_connect ( $mysql_server, $mysql_user, $mysql_password) or die ("Could not connect to database!");

mysql_select_db ($database) or die ("Could not connect to Table!");

$question = "select * from products";

$answer = mysql_query ($question) or die ("Query error!");

$data = mysql_fetch_row($answer)

?>
</head>

<body>

<?

echo "Here there are our products:";

while ($data = mysql_fetch_row($answer)){

echo $data[1];
echo $data[2];
}

?>[/php]

There it is! Hope you liked it! Sorry for not puting comments...Doubts? Put here below.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]