SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    florida, us
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    would this code work? (table check)

    PHP Code:
    function TableExists($tablename$dbname) {

    $tablename $prefix_attachments;

       
    // Get a list of tables contained within the database.
       
    $result mysql_list_tables($dbname);
       
    $rcount mysql_num_rows($result);

       
    // Check each in list for a match.
       
    for ($i=0;$i<$rcount;$i++) {
           if (
    mysql_tablename($result$i)==$tablename) { echo "yes";
       }

       echo 
    "no";

    }
      } 
    Does that work? lol I dont think so but The orignaal code was:

    PHP Code:
    function TableExists($tablename$dbname) {

    $tablename $prefix_attachments;

       
    // Get a list of tables contained within the database.
       
    $result mysql_list_tables($dbname);
       
    $rcount mysql_num_rows($result);

       
    // Check each in list for a match.
       
    for ($i=0;$i<$rcount;$i++) {
           if (
    mysql_tablename($result$i)==$tablename) { return true;
       }

       return 
    false;

    }
      } 
    But it doesnt do anything, and I need it to check if $prefix which is defined by the user earlier in the file, is already taken in the database.

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh?
    PHP Code:
    global $prefix_attachments
    at the beginning of the function. You could use
    PHP Code:
    $tablename $GLOBALS['prefix_attachments']; 
    too. But why do you insert it's value to another variable anyway?
    Shouldn't it be
    PHP Code:
    $tablename $GLOBALS['prefix_attachments'] . $tablename

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    florida, us
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I'm trying that, but you see..

    $prefix is the prefix of the table the people put in. attachments is just the first table name it needs to check to see if its there.. for example, if "yahoo_attachments" is there - then the $prefix was yahoo and attachments is just a table it makes..

    So ..

    I know

    $tablename = $GLOBALS['prefix'] . _attachments;

    wouldnt work, what would that be :P the "prefix" is the only thing it needs to actually get. attachments is just... there

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So $tablename includes the prefix and you want to get it?
    PHP Code:
    $prefix    substr($tablename0strpos($tablename'_'));
    $tablename substr($tablenamestrpos($tablename'_')); 

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    florida, us
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about we put it like this...someone help me write a code, maybe, lol, so that when the person enters a prefix for a table, which is labeled $prefix, it checks the database to see if $prefix is already being used (the first table the code creates is $prefix_attachments, so thats why it should probably check if $prefix_attachments is already made, because its made each time..)

    get it? :/

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still didn't get it but anyway, maybe it's this.
    PHP Code:
    function TableExists($prefix$tablename$dbname) {
        
    $result mysql_list_tables($dbname);
        for (
    $i 0$c mysql_num_rows($result); $i $c$i++) {
            if (
    mysql_tablename($result$i) == $prefix $tablename) { 
                return 
    true;
            } 
        }
        return 
    false;
    }

    // Example
    if (TableExists('foo_''bar''foobar')) {
        echo 
    'Use a different prefix.';



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
  •