Text file database

Is there any way to create a text file database and search it using Sql in PHP?
[79 byte] By [Rich2189] at [2007-11-19 21:38:41]
# 1 Re: Text file database
you use explode() fopen, fread, and all kinds of other stuff! basically you parse your text file into an array and the output your values from the array.

file data.txt contains:
1;bob;doe;date

$file = "path/to/data.txt";
$handle = file_get_contents($file); // you can use file() to return an array per line
//and then create a deeper array with explode within anothe loop
$contents = explode(";", $handle);

fclose($handle);

print_r($contents);

outputs
array(
0=>1
1=>Bob
2=>Doe
3=>Date
}

to print bob

echo $content[1];

not tested but along those lines
vincenzobar at 2007-11-10 3:57:34 >
# 2 Re: Text file database
It depends on want you want to do
you can create a csv fiel and parse it
yourself(like vincenzobar already described)
or you can use a ready to use php csv parser
e.g.
http://de.php.net/fgetcsv
http://www.phpclasses.org/browse/package/849.html
http://commoncontent.org/catalog/web/technical/1404/

or a good laternative to mysql is sqlite
http://www.zend.com/php5/articles/php5-sqlite.php
http://de3.php.net/sqlite
blueday54555 at 2007-11-10 3:58:28 >
# 3 Re: Text file database
Thanks guys, hmmm other than having to parse it is there any file that can be outputed to in PHP that can be searched with sql?
Rich2189 at 2007-11-10 3:59:27 >
# 4 Re: Text file database
Thanks guys, hmmm other than having to parse it is there any file that can be outputed to in PHP that can be searched with sql?
No. SQL is a command language that works directly with a SQL based server.
PeejAvery at 2007-11-10 4:00:33 >
# 5 Re: Text file database
No. SQL is a command language that works directly with a SQL based server.

Hmmm if thats the case is there anyway to copy the contents of a database to a back up file so it can be moved to another location?. The code i am using is as follows if it helps

<?php
$sql = $_POST['sql'];

$link = mysql_connect('localhost', 'dbname', 'password');
if (!$link) {
die('0:' . mysql_error());
}

if (!mysql_select_db('dbName', $link)) {
echo '0: Could not select database';
exit;
}

$result = mysql_query($sql, $link);

if (!$result) {
echo "0: could not query the database\n";
echo mysql_error();
exit;
}
else
{

$row=mysql_fetch_array($result);
echo $row[0];

mysql_free_result($result);
}

mysql_close($link);

?>
Rich2189 at 2007-11-10 4:01:32 >
# 6 Re: Text file database
Hmmm if thats the case is there anyway to copy the contents of a database to a back up file so it can be moved to another location?
Well, I usually don't like to share something of the following nature but here is the full source code to one of my own MySQL backup PHP pages. It will connect to MySQL and output the complete MySQL database to a PHP file. That PHP file, when run, will be exactly the same as the old MySQL database.

EDIT: INSTRUCTIONS...
1. Put this on your webserver.
2. Run it from a URL. (http://localhost/backup.php)
3. It compiles a PHP file of the whole database in the same directory as that of backup.php.
4. Now that PHP file can be put on a webserver and run from a URL.
5. When executed, it makes that MySQL server identical to the one it came from.
<?php

$date = date(".Ymd.Hi");
$file = "bckp" . $date . ".php";

$output = fopen($file, 'w');

$user = ""; // Put MySQL username here
$pass = ""; // Put MySQL password here

fwrite($output, "<?php\r\n\r\n");
fwrite($output, "\$user = '$user';\r\n");
fwrite($output, "\$pass = '$pass';\r\n");
fwrite($output, "\$connection = mysql_connect('localhost', \$user, \$pass);\r\n\r\n");

$connection = mysql_connect('localhost', $user, $pass);
$dblist = mysql_list_dbs($connection);
while($row = mysql_fetch_object($dblist)){
$db = $row->Database;
$dbcreate = "mysql_query(\"CREATE DATABASE `$db`\");";
fwrite($output, $dbcreate . "\r\n");
$dbselect = mysql_select_db('$db');
fwrite($output, "mysql_select_db('$db');\r\n");
$tbl = mysql_list_tables($db);
while($table = mysql_fetch_row($tbl)){
$fld = mysql_query("SELECT * FROM `$table[0]`");
$fields = mysql_num_fields($fld);
unset($tblcreate);
$tblcreate = "CREATE TABLE `$table[0]` (";
$arrfield = array();
$arrfieldnum = 0;
for($i=0;$i<$fields;$i++){
$fieldinfo = mysql_fetch_field($fld, $i);
unset($fieldname);
$fieldname = $fieldinfo->name;
$arrfield[$arrfieldnum] = $fieldname;
$arrfieldnum++;
$fieldlen = mysql_field_len($fld, $i);
if($fieldlen=="65535"){$fieldtype = "TEXT";}
else{$fieldtype = "VARCHAR($fieldlen)";}
$tblcreate .= "`$fieldname` " . $fieldtype . " NOT NULL";
if($i !== $fields - 1){$tblcreate .= ", ";}
}
$tblcreate .= ")";
fwrite($output, " mysql_query(\"" . $tblcreate . "\", \$connection);\r\n");
$readtable = "SELECT * FROM `$table[0]`";
$retval = mysql_query($readtable);
while($col = mysql_fetch_row($retval)){
$insert = "mysql_query(\"INSERT INTO `$table[0]` (";
for($ifield=0;$ifield<count($arrfield)-1;$ifield++){
$insert .= "`" . $arrfield[$ifield] . "`, ";
}
$insert .= "`" . $arrfield[$ifield] . "`) VALUES (";
for($icol=0;$icol<count($col)-1;$icol++){
$col[$icol] = str_replace("\"", "'", $col[$icol]);
$insert .= "'" . $col[$icol] . "', ";
}
$insert .= "'" . $col[$icol] . "'";
$insert .= ")\", \$connection);";
fwrite($output, " $insert\r\n");
}
}
}
mysql_close($connection);

fwrite($output, "\r\nmysql_close(\$connection);\r\n\r\n");
fwrite($output, "?>");

fclose($output);

?>
<html>
<title>MySQL Backup</title>
<body>
<font face="arial" size=5 color="#000000"><b>Backup Complete!</b></font>
</body>
</html>
PeejAvery at 2007-11-10 4:02:39 >
# 7 Re: Text file database
Decent looks cool, but i dont understand how it works. You mean, it connects to the database and copies it to a new php file. Then when you run the new php file, does it copy it back to another sql database?
Rich2189 at 2007-11-10 4:03:37 >
# 8 Re: Text file database
Decent looks cool, but i dont understand how it works.
1. Put this on your webserver.
2. Run it from a URL. (http://localhost/backup.php)
3. It compiles a PHP file of the whole database in the same directory as that of backup.php.
4. Now that PHP file can be put on a webserver and run from a URL.
5. When executed, it makes that MySQL server identical to the one it came from.
PeejAvery at 2007-11-10 4:04:40 >
# 9 Re: Text file database
Thanks thats what I figured, just i case you had any doubts im not going to use it with bad intentions, i just want it there as a safe guard incase i loose the free webspace i have. Then if I loose it i can pull the database information out and move on easily. I was worried since there is no actually files that users can access.
Rich2189 at 2007-11-10 4:05:38 >