LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-28-2004, 07:24 AM   #1
Boby
Member
 
Registered: Feb 2004
Location: Sighisoara/Cluj-Napoca (Romania)
Distribution: CentOS 4, Fedora Core 6
Posts: 781

Rep: Reputation: 31
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.
 
Old 05-28-2004, 07:40 AM   #2
flakzeus
Member
 
Registered: Jan 2004
Location: Salisbury, NC
Distribution: Gentoo 2004.1
Posts: 48

Rep: Reputation: 15
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.
 
Old 05-28-2004, 09:06 AM   #3
Boby
Member
 
Registered: Feb 2004
Location: Sighisoara/Cluj-Napoca (Romania)
Distribution: CentOS 4, Fedora Core 6
Posts: 781

Original Poster
Rep: Reputation: 31
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!
 
Old 05-28-2004, 09:29 AM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,290

Rep: Reputation: 69
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 ?
 
Old 05-28-2004, 09:38 AM   #5
Boby
Member
 
Registered: Feb 2004
Location: Sighisoara/Cluj-Napoca (Romania)
Distribution: CentOS 4, Fedora Core 6
Posts: 781

Original Poster
Rep: Reputation: 31
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.
 
Old 05-28-2004, 11:06 AM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,290

Rep: Reputation: 69
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')";
 
Old 05-28-2004, 12:19 PM   #7
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
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.
 
Old 05-28-2004, 12:23 PM   #8
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,290

Rep: Reputation: 69
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
 
Old 05-28-2004, 01:02 PM   #9
flakzeus
Member
 
Registered: Jan 2004
Location: Salisbury, NC
Distribution: Gentoo 2004.1
Posts: 48

Rep: Reputation: 15
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.
 
Old 05-28-2004, 03:26 PM   #10
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,290

Rep: Reputation: 69
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.
 
Old 05-29-2004, 10:07 AM   #11
Boby
Member
 
Registered: Feb 2004
Location: Sighisoara/Cluj-Napoca (Romania)
Distribution: CentOS 4, Fedora Core 6
Posts: 781

Original Poster
Rep: Reputation: 31
hey it's working... thanX a lot guys!!

cheers, boby

Last edited by Boby; 05-29-2004 at 10:12 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DBI perl "insert" problems Xris718 Programming 3 02-09-2005 03:33 PM
MySQL/PHP/Horde "Failed to write session data" Big Money Linux - Software 0 11-23-2004 01:33 PM
Php or Mysql upgrade on Fedora Core 2 "Uptodate" Program geninblaze Fedora 1 07-12-2004 09:56 PM
PHP and MYSQL ("mysql_connect") smaida Programming 4 05-18-2004 05:27 PM
php ide with "break" and "trace" Kayaker Programming 3 04-25-2003 02:52 PM


All times are GMT -5. The time now is 06:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration