View Full Version : insert values into a database using a file
mancroft
07-13-2003, 11:24 AM
What I wish to do is insert values into a database using a file (insert.php).
1. Is the layout of the file (see below) the proper way to do it?
2. Once the file is created, what do I do?, just put it on the server and then run the url http://pathtofile/insert.php
<?
//dbuser
$usr = "yyyyyyyy";
//dbpass
$pwd = "xxxxxxxx";
//dbname
$db = "zzzzzzzzzz";
//dbhost
$host = "localhost";
// connect to the sql database
$link = mysql_connect($server, $user, $pass);
$db = mysql_select_db($database, $link);
"insert into items values(0, 'Tony', 'Tony blah', 23.95)";
"insert into items values(0, 'FI', 'FI blah',36.50)";
?>
Jeewhizz
07-13-2003, 12:10 PM
You have conflicting variables there - two lots of $db... you don't actually need to label the mysql_select_db() function, so just use this:
mysql_select_db($db,$link);
You will also need to run your queries like so:
mysql_query("INSERT INTO `items` values(0, 'Tony', 'Tony blah', 23.95)");
mysql_query("INSERT INTO `items` values(0, 'FI', 'FI blah',36.50)");
and so on...
Then just run the file by using a web browser once uploaded by going to the url
You have multiple options:
- through the commandline, in batchmode.
- through a db-frontend
- through PHP
I'd recommend them in that order, because:
- if you do this through a PHP file, you're creating unnescecary load on the webserver. There's no reason for that. You should directly connect to the MySQL server.
- if yoyu use a db-front --> if you use phpMyAdmin, you're basically doing the same as above. But it's easier. And you could import your file as a csv file.
Or you could just write an importfile (= manually creating a dumpfle and run that. --> see step 2 and 3 of this sticky
http://www.codingforums.com/showthread.php?s=&threadid=22880)
You just create a file with the sql-statements you wan't to be run
- the best sollution is to cut all the overhead is by doing it in commandline. Check out my last post here
http://www.codingforums.com/showthread.php?s=&threadid=22738&highlight=batchfile
Create a batchile like
cd c:
cd \
cd phpdev\mysql\bin
mysql -h localhost -u root -p -H < test.sql
Test.sql wuld then look like
use test;
insert into items values(0, 'Tony', 'Tony blah', 23.95);
insert into items values(0, 'FI', 'FI blah',36.50);
(that is, if your insertstatements are correct --> will only work if you insert the value in that order and if all columns then get a value)
<edit>posts crossed</edit>
mancroft
07-13-2003, 01:10 PM
Thanks, raf.
You're welcome.:thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.