Jump to content

txt file mysql


shage

Recommended Posts

if i have a large database full of sites, each site is under a category table such as electronic,clothes,etc im trying to export the information into a txt file that says

 

Electronic Stores

site A

site B

 

Clothes Stores

site A

site B

 

im lost and new thank you

Link to comment
Share on other sites

I think this might be what you are looking for.

 

It basically goes through all the tables in your database and then takes the column sitename (which obviusly you will need to change to suit your needs) and goes through each value. It is all added to the variable $string which is then written into the db.txt which you will need to create one and name it what ever you want.

 

<?php
$db_host = '';
$db_username = '';
$db_password = '';
$db_database = '';

$db_connection = mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
mysql_select_db($db_database) or die(mysql_error());

$string = "";

$sql = "SHOW TABLES FROM $db_database";
$result = mysql_query($sql) or die(mysql_error());
while($table_name = mysql_fetch_array($result))
{
$sql2 = "SELECT sitename FROM $table_name[0]";
$result2 = mysql_query($sql2) or die(mysql_error());
$string .= "\n{$table_name[0]}\n";
while($column_name = mysql_fetch_array($result2))
{
	$string .= "{$column_name[0]}\n";
}
}

$file = "db.txt";
if(is_writable($file))
{
    if(!$handle = fopen($file, 'w'))
{
        die("Cannot open file ($file)");
    }
    if(fwrite($handle, $string) === FALSE)
{
        die("Cannot write to file ($file)");
    }

    echo "Success, wrote to file ($file)";

    fclose($handle);

} else
{
	echo "The file $file is not writable";
}
?>

 

I hope it helps ;D

 

~ Chocopi

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.