Main Menu

friends

Banner

Latest articles

find world writable files
09/01/2010 | mad mad mod

No file on a server should have o+w permissions (at least I can't think of a reason).The command to find all world writable files on system is: # find / -perm -0002  -type f -print
This command will [ ... ]


convert mysql table from MyISAM to InnoDB
30/12/2009 | mad mad mod

MyISAM is the default storage engine for MySQL. Unfortunately it doesn't support transactions or foreign keys (but it has some other nice features like compression). However, because InnoDB does su [ ... ]


Other Articles
Facebook MySpace Twitter Digg Delicious Stumbleupon Google Bookmarks 

Designed by:
SiteGround web hosting Joomla Templates
very simple mysql connection from php E-mail
programming

This article describes how to connect to a mysql database from a php script. It is realy the most simple way of using mysql databases in php, no fancy wrapper classes, no functions and NO security! We will care about that later...

 

 

Step 1: Connect to the mysql server

 

The command to do that is "mysql_connect" and the return value is either a connection-id or FALSE.

 

example:

 

<?PHP

$connect=mysql_connect("localhost", "dbadmin", "password");
if (!$connect) {
die('connection not possible: ' . mysql_error());
}

?>

 

In this example the mysql server and the webserver are running on the same server (localhost).

 

 

Step 2: select a database


The command to do that is "mysql_select_db" and the return value is either TRUE or FALSE.

 

example:

 

<?PHP

$connect=mysql_connect("localhost", "dbadmin", "password");

if (!$connect) {
die('connection not possible: ' . mysql_error());
}

$db=mysql_select_db("mydatabase",$connect);

if (!$db) {
die('could not select database: ' . mysql_error());
}


?>

 

 

Step 3: make a query

 

The command to make a query is "mysql_query" and the return value is either TRUE, FALSE or a resource id.

 

example:

 

<?PHP



(...)



$db=mysql_select_db("mydatabase",$connect);

if (!$db) {
die('could not select database: ' . mysql_error());
}

$result = mysql_query('SELECT * FROM mytable');
if (!$result) {
die('query failed: ' . mysql_error());
}
?>

 

Ok, but where is the result now? $result only contains a resource id and the command to read the real result is mysql_fetch_array(). mysql_fetch_array returns an array with the result from our query.

 

 

<?PHP

 

(...)


while ($row = mysql_fetch_array($result)) {
print ("row 0: " . $row[0]);
print (" row 1: " . $row[1]);
print ("\n");
}
?>

 

 

All together

 

 

<?PHP

$connect=mysql_connect("localhost", "dbadmin", "password");
if (!$connect) {
die('connection not possible: ' . mysql_error());
}


$db=mysql_select_db("mydatabase",$connect);

if (!$db) {
die('could not select database: ' . mysql_error());
}



$result = mysql_query('SELECT * FROM mytable');
if (!$result) {
die('query failed: ' . mysql_error());
}



while ($row = mysql_fetch_array($result)) {
print ("row 0: " . $row[0]);
print (" row 1: " . $row[1]);
print "\n";
}

?>

 

 

Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

 
mad mad mod, Powered by Joomla! and designed by SiteGround web hosting