Re: newbie: MySQL importing data from a variable
| From: | Indrek Järv | Date: | Mon, 12 Mar 2001 13:52:04 +0000 |
| Subject: | Re: newbie: MySQL importing data from a variable | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-7319@lists.php.net to get a copy of this message | ||
Hi Gavin.
Maybe this help You :)
file: import.php
<?php
$my_host = "localhost";
$my_database = "xxxxxxx";
$my_user = "xxxxxxx";
$my_password = "xxxxxxx";
if (!$db) {
$db=mysql_pconnect($my_host, $my_user, $my_password);
if(!$db){
echo "FATAL:Couldn't connect to db.<br>";
exit;
}
mysql_select_db($my_database, $db);
}
$dh = dir('/home/httpd/html');
while ($entry = $dh->read()) {
if (preg_match('/\.sql$/i', $entry)) {
$matching[] = $entry;
}
}
for ($j=0; $j<count($matching); $j++) {
$line = file("/home/httpd/html/matching[$j]");
$nr = 0;
for ($i=0; $i<count($line); $i++) {
$line[$i] = trim( $line[$i] );
# for browser timeout problem handling
if ($nr > 100) {
echo "#";
$nr = 0;
} else{
$nr = $nr + 1;
}
$result = mysql_query($line[$i], $db);
# need add error handling
}
}
echo "Ready\n";
mysql_close($db);
file: file1.sql
INSERT INTO firma VALUES ( 'XXXXX', 'XXXX', '', '',
'', '', '*');
INSERT INTO firma VALUES ( 'YYYYY', 'YYYY', 'YYYY', 'YYYY',
'222', '', '*');
file: file2.sql
INSERT INTO firma VALUES ( 'XXXXX', 'XXXX', '', '',
'', '', '*');
INSERT INTO firma VALUES ( 'YYYYY', 'YYYY', 'YYYY', 'YYYY',
'222', '', '*');
and now call import script:
http://www.wwwwww.com/import.php
Indrek Järv
indrekj@uninet.ee
Gavin Tong wrote:
> Hi, I've been trying to get this script to read a directory and then pass the information
> into a Mysql table. I can open the directory and print it's contents, or I can get other
> information from variables passed to the mysql table, but I can't seem to do both. If you have
> any idea what I've done wrong or have an idea where I can read more about this problem your
> thoughts will be greatly appreciated.
> Sincerely,
> Gavin Tong
>
> <?php
> $handle=opendir('.');
> while (false!==($file = readdir($handle))) {
> if ($file != "." && $file != "..") {
> echo "$file\n";
> }
> }
> closedir($handle);
> $db = mysql_connect("local host", "person", "pasword");
> mysql_select_db("photos",$db);
> $result = mysql_query("insert into template (name) values ('$file')",$db);
> ?>