Results 1 to 7 of 7

Thread: PHP and MySQL

  1. #1
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default PHP and MySQL

    Hi all,
    I am working on an idea for my site but not sure how to go about part if it. The idea is that I have several image files each with a php script in, when I run this PHP script it gets a list of all the pictures in the folder and adds them into a database, but if the picture already exists in the database then it skips it. Does anyone have any ideas of how do do this?
    Cheers

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    foreach(scandir('.') as $file)
      if(!mysql_num_rows(mysql_query(sprintf('select filename from files where filename=\'%s\'', mysql_real_escape_string($file)))))
        mysql_query(sprintf('insert into files (filename) values (\'%s\')', mysql_real_escape_string($file)));
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Thanks for the quick reply Twey.
    I am new to PHP and am still learning about it, so I am just trying to work out what each part of the code does, but I am stuck at two bits:

    PHP Code:
    filename=\'%s\' 
    What does the %s mean?

    And:
    PHP Code:
    mysql_real_escape_string($file
    I have never heard of this command before.

    If you dont mind please could you explain them?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    What does the %s mean?
    "String:" it's replaced by sprintf() with the second argument.
    I have never heard of this command before.
    The term is "function" -- it returns a value. It escapes the string so it's safe to use in MySQL queries to the current connection.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    What does the %s mean?
    %s is a placeholder for mysql_real_escape_string($file)
    So, %s will be replaced with mysql_real_escape_string($file)

    I have never heard of this command before.
    To prevent MySQL injection.

    Example:
    Say I put in your URL ?page=DELETE database 'database';
    Without mysql_real_escape_string($file), your database would be deleted.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  6. #6
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Thanks to both of you. But I have one more question

    Say I wanted to have a the file path put into the database as well would this work:

    Code:
    foreach(scandir('.') as $file)
      if(!mysql_num_rows(mysql_query(sprintf('select filename from files where filename=\'%s\'', mysql_real_escape_string($file)))))
        mysql_query(sprintf('insert into files (filename, path) values (\'%s\', 'pictures/thunbnails/%s') mysql_real_escape_string($file)));
    ?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Send the same value as the next parameter to sprintf() as well, and yes. There's not much point storing it if it's the same for each file, though.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •