Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-28-2004, 07:24 AM
|
#1
|
Member
Registered: Feb 2004
Posts: 781
Rep: 
|
PHP "for.. loop" to insert into mysql
hello!
i want to create a homepage on my LAN. i have the fotogallery saved on the web.
there are many pictures with thumbnails. i want to save all the links in a mysql database. i would like to write a script that aumatticaly writes them to the database so don't have to copy>>paste about 200 links to a manually insert.
the pictures are saved like this:
pictures are here:
www.homepage.com/gallery/holiday/pic01.jpg
.......
www.homepage.com/gallery/holiday/pic10.jpg
.......
www.homepage.com/gallery/holiday/pic50.jpg
for each picture i have 2 thumbnails:
www.homepage.com/gallery/holiday/pic01_t.jpg
.......
www.homepage.com/gallery/holiday/pic50_t.jpg
and
www.homepage.com/gallery/holiday/pic01_t1.jpg
.......
www.homepage.com/gallery/holiday/pic01.t50jpg
ok..i created a database that should look like the screenshot that i attached.
this is the screenshot of my wannabe table: http://www.frozenminds.de/table_example.png
this is the php code that i wrote...but it doesn't write anything to my database. maybe you could help me, 'cause i'm a real
Code:
<?
$db_host = "localhost";
$db_username = "boby";
$db_password = "********";
$db_name = "mydatabase";
mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
for ($i = 1; $i <= 50; $i++)
{
$pic = 'www.homepage.com/gallery/schaessburg/pic'.$i.'';
$thumb1 = 'www.homepage.com/gallery/schaessburg/pic'.$i.'t';
$thumb2 = 'www.homepage.com/gallery/schaessburg/pic'.$i.'t'.$i.'';
$gallery = 'holiday';
$query = "INSERT INTO foto (picture, thumb1, thumb2, gallery)
VALUES ('$pic','$thumb1',$thumb2','$gallery')";
mysql_query($query);
}
mysql_close();
?>
could anyone help me?
thanx a lot!
cheers, boby
Last edited by Boby; 05-28-2004 at 09:40 AM.
|
|
|
05-28-2004, 07:40 AM
|
#2
|
Member
Registered: Jan 2004
Location: Salisbury, NC
Distribution: Gentoo 2004.1
Posts: 48
Rep:
|
I see a couple of things wrong here, first you have start a php script with <?php and not just <?. Second, to use mysql_select_db() you have to give it a connection to select the database on, for example: mysql_select_db($db_name, $connection); so by putting the mysql_connect into a variable it allows you to pass that connection to select_db and any other connection you may need later.
PHP Code:
<?php
$db_host = "localhost";
$db_username = "boby";
$db_password = "********";
$db_name = "mydatabase";
$connection = mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
$databse = mysql_select_db($db_name, $connection) or die(mysql_error());
for ($i = 1; $i <= 50; $i++)
{
$pic = 'www.homepage.com/gallery/schaessburg/pic'.$i.'';
$thumb1 = 'www.homepage.com/gallery/schaessburg/pic'.$i.'t';
$thumb2 = 'www.homepage.com/gallery/schaessburg/pic'.$i.'t'.$i.'';
$gallery = 'holiday';
$query = "INSERT INTO `foto` (picture, thumb1, thumb2, gallery)VALUES ('$pic','$thumb1',$thumb2','$gallery')";
mysql_query($query, $connection);
}
mysql_close();
?>
That's just a couple of things that jump out at me. Try that and see what happens, I don't have much time right now.
|
|
|
05-28-2004, 09:06 AM
|
#3
|
Member
Registered: Feb 2004
Posts: 781
Original Poster
Rep: 
|
hhmmm...still no entries  i really don't know where's the point. i get no errors but no entries too.
flakzeus, if you have later some time for this, or somebody else, could you just try something else out? maybe another script. i tried youre script, then i tried to modify it, but it's still not working.
thank you!
|
|
|
05-28-2004, 09:29 AM
|
#4
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,858
|
The screenshot link is actually empty but the script from flakzeus should add entries to the table foto (the '.jpg' extension misses but anyway the strings should be here in the fields). You did not get any error output from the query ?
|
|
|
05-28-2004, 09:38 AM
|
#5
|
Member
Registered: Feb 2004
Posts: 781
Original Poster
Rep: 
|
i get no errors, that's why i don't know where to search for problem. i get an white, empty page.
is there maybe a mysql or linux shell script that could help me out?
ok...i fixed the link to the screenshot...i didn't wrote it correctly, sorry!
here it is: http://www.frozenminds.de/table_example.png
i'll put the same wannabe table that should be generated by the script in this thread.
here it is:
Code:
+----+-------------------------------+---------------------------------+-----------------------------------+---------+
| id | picture | thumb1 | thumb2 | gallery |
+----+-------------------------------+---------------------------------+-----------------------------------+---------+
| 1 | www.home.../holiday/pic01.jpg | www.home.../holiday/pic01_t.jpg | www.home.../holiday/pic01_t1.jpg | holiday |
| 2 | www.home.../holiday/... | www.home.../holiday/... | www.home.../holiday/... | holiday |
| 3 | www.home.../holiday/pic50.jpg | www.home.../holiday/pic50_t.jpg | www.home.../holiday/pic50_t50.jpg | holiday |
+----+-------------------------------+---------------------------------+-----------------------------------+---------+
thanX
Last edited by Boby; 05-28-2004 at 09:50 AM.
|
|
|
05-28-2004, 11:06 AM
|
#6
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,858
|
Maybe a tip : try to play with the quote (`) in the query. For my part I use quote only for field value and the entire query in double quote.
[php]
$query = "INSERT INTO foto (picture, thumb1, thumb2, gallery) VALUES ('$pic','$thumb1',$thumb2','$gallery')";
|
|
|
05-28-2004, 12:19 PM
|
#7
|
Member
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110
Rep:
|
don't know if anyone noticed this or not, but in these code samples, $thumb2 has a single quote only after it, but not before it, (it should be '$thumb2').
when you say you're not getting errors, which errors are you talking about? the ones from the apache error log?
i use php/mysql myself, and the apache error log gives you all of the php errors with filenames and line numbers the errors occured on, making debugging a file much easier.
if you don't know where it is, i'm on slack 9.1, and mine is /var/log/apache/error_log, if i remember correctly.
|
|
|
05-28-2004, 12:23 PM
|
#8
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,858
|
You right towlie I did not noticed that  and also run slackware 9.1 here, same error_log for apache. In slackware php package php.ini is configured to not show error but log them in /var/log/apache/error_log
|
|
|
05-28-2004, 01:02 PM
|
#9
|
Member
Registered: Jan 2004
Location: Salisbury, NC
Distribution: Gentoo 2004.1
Posts: 48
Rep:
|
One reason you are getting a blank screen is because your not telling it to print anything. Try this and see if you get errors.
PHP Code:
<?php
$db_host = "localhost"; $db_username = "boby"; $db_password = "********"; $db_name = "mydatabase"; $connection = mysql_connect($db_host,$db_username,$db_password) or die(mysql_error()); $databse = mysql_select_db($db_name, $connection) or die(mysql_error());
for ($i = 1; $i <= 50; $i++) { $pic = 'www.homepage.com/gallery/schaessburg/pic'.$i.'.jpg'; $thumb1 = 'www.homepage.com/gallery/schaessburg/pic'.$i.'t.jpg'; $thumb2 = 'www.homepage.com/gallery/schaessburg/pic'.$i.'t'.$i.'.jpg'; $gallery = 'holiday'; $query = "INSERT INTO `foto` (picture, thumb1, thumb2, gallery)VALUES ('$pic','$thumb1','$thumb2','$gallery')"; $result = mysql_query($query, $connection); } if ($result != FALSE) { print "Query was successful <br>"; } else { print "Query was unsuccessful <br>"; print "<br>"; print mysql_errno($connection) . ": " . mysql_error($connection) . "\n"; }
mysql_close(); ?>
Last edited by flakzeus; 05-28-2004 at 03:32 PM.
|
|
|
05-28-2004, 03:26 PM
|
#10
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,858
|
yes and don't forget the .jpg extension
PHP Code:
/* for number like 01, 02, 03... */
$picNumber = sprintf("%02d", $i);
$pic = "www.homepage.com/gallery/schaessburg/pic$picNumber.jpg";
$thumb1 = "www.homepage.com/gallery/schaessburg/pic$picNumber"."_t.jpg";
$thumb2 = "www.homepage.com/gallery/schaessburg/pic$picNumber"."_t$i.jpg";
Last edited by keefaz; 05-28-2004 at 03:28 PM.
|
|
|
05-29-2004, 10:07 AM
|
#11
|
Member
Registered: Feb 2004
Posts: 781
Original Poster
Rep: 
|
hey it's working... thanX a lot guys!!
cheers, boby
Last edited by Boby; 05-29-2004 at 10:12 AM.
|
|
|
All times are GMT -5. The time now is 08:49 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|