Archive

Posts Tagged ‘mysql’

PHP MYSQL Tips and Tricks

September 17th, 2009 R Arun Raj No comments

This is a small article containing tricks and tips I aquired by reading PHP 6 Text Book.

I was used isset() function to check whether these arguements are set or not. And also I need to check this is empty or not.

Here is another function to check both of this requirement at single line.

empty()

It checks if a variable has an empty value: an empty string, 0, NULL,or FALSE.

Example

if ( !empty($_POST[‘name’]) && !empty($_POST[‘comments’]) && !empty($_POST[‘email’]) ) {

echo “Yes”;

}

Categories: PHP, Programming Tags: , , ,

Export Database to Excel or CSV File

August 29th, 2008 R Arun Raj 2 comments

The below code help you to export data from database to csv file

exporttoxls

To export a complete database, you should invoke all the tables via below code and apply the above code
$dbname = ‘mysql_dbname’;

if (!mysql_connect(‘mysql_host’, ‘mysql_user’, ‘mysql_password’)) {
echo ‘Could not connect to mysql’;
exit;
}

$sql = “SHOW TABLES FROM $dbname”;
$result = mysql_query($sql);

if (!$result) {
echo “DB Error, could not list tables\n”;
echo ‘MySQL Error: ‘ . mysql_error();
exit;
}

while ($row = mysql_fetch_row($result)) {
echo “Table: {$row[0]}\n”;
}

mysql_free_result($result);
?>

Categories: PHP, Programming Tags: , , , , ,

How to create a directory structure ?

March 7th, 2008 R Arun Raj No comments

How to create a directory structure ? or
How to creata a tree structure  ? or
How to design a database for a tree structure ? or
How to design a database for storing hierarchical structure ?

using single table

Create a table like this

database

CREATE TABLE `category` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
`descr` text,
`parent` int(11) default NOT NULL,
PRIMARY KEY  (`id`)
)

This table containing fields id,name,description and parent.Here all are familiar fields except parent.
Parent field is used to store the id of parent item . So that we can select the sub category by querying the database

with parent==id .

First upon we have to display main category by querying parent==’0′,
Then sub category by querying parent==1 or some thing like that
I think this information useful .

Regards,
Arun Raj R
Kadampanad

Categories: Mysql, database Tags: , , , ,