Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Database Information pull.
Old 06-06-2005, 06:41 AM Database Information pull.
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
PHP Code:
function listInvoice() {
error_reporting(E_ALL);
 
$sql_text "SELECT name FROM ".$this->TABLES['invoice']."";
 
    
$the_array = Array();
    
$handle opendir('invoice'); 
    while (
false !== ($file readdir($handle))) { 
       if (
$file != "." && $file != "..") {   /* as descripted below: these "files" will not be added to the array */
       
$the_array[] = $file
   } 
}
closedir($handle); 
foreach (
$the_array as $element) {
 
$filename '$element';

    if (
$name == $element) {
       echo 
"$element \n" ;
    } else {
      
$this->sql->Insert("INSERT INTO ".$this->TABLES['invoice']." (name) VALUES ('".$element."')");
    }
  }
  } 
This is my code. It simple gets the names of files and places those names into a variable which is able to be outputed.

The thing is, I need to be able to compare it against a database, to see if it already exists in the database :

PHP Code:
if ($name == $element) {
       echo 
"$element \n" ;
    } else {
      
$this->sql->Insert("INSERT INTO ".$this->TABLES['invoice']." (name) VALUES ('".$element."')");
    } 
This is always a true statement. $element is the current name of the current file.

So, it checks the database to see if the filename is already in the database. If it is, not much happens, if it isnt, it stores the filename into the database. But at the moment, it seems as though its not getting any information from the database. For it is always false.

I have no idea why this is. Maybe $sql_text = "SELECT name FROM ".$this->TABLES['invoice'].""; is the incorrect way to pull the info from the database?
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-06-2005, 01:03 PM
.Mike's Avatar
Novice Talker

Posts: 9
Location: Savannah, GA
Trades: 0
Quote:
Originally Posted by lothop
I have no idea why this is. Maybe $sql_text = "SELECT name FROM ".$this->TABLES['invoice'].""; is the incorrect way to pull the info from the database?
I assume you have already created a connection to your database. Otherwise, you'll need to do that before you can execute any queries.

As far as I can tell, all you have done is assigned your query to a string variable. You have not actually executed the query.

Check out Example 1 on the following page from the PHP MySQL function reference. It will walk you through creating a basic database connection, running a simple select query, and outputting the results:

http://us2.php.net/mysql

In particular, look at the section for mysql_query

I hope that helps!

Mike
.Mike is offline
Reply With Quote
View Public Profile Visit .Mike's homepage!
 
Old 06-07-2005, 12:53 AM
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Yes, it already connects to the database.

Its just running the function. The thing is, I dont want to output any information. I just want to check to see if the filename already exists in the database.

It works for my other functions, but for some reason its as if it isnt getting the info out of the database.

Cheers for reply
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 06-07-2005, 02:00 AM
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
I've also tried it this way.

PHP Code:
function listInvoice() {
error_reporting(E_ALL);
    
$the_array = Array();
    
$handle opendir('invoice'); 
    while (
false !== ($file readdir($handle))) { 
       if (
$file != "." && $file != "..") {   /* as descripted below: these "files" will not be added to the array */
       
$the_array[] = $file
   } 
}
closedir($handle);
foreach (
$the_array as $element) {
 
$filename '$element';

        if (
$this->sql->Select("SELECT filename FROM ".$this->TABLES['invoice']." WHERE filename = '".$element."'")) {
               
$this->sql->Insert("INSERT INTO ".$this->TABLES['invoice']." (filename) VALUES ('".$element."')");
               
                } else {
                  
                }
                }
  }
  } 
PHP Code:
if ($this->sql->Select("SELECT filename FROM ".$this->TABLES['invoice']." WHERE filename = '".$element."'")) { 
Looks at database to see if the filename already exists, if it doesnt,
PHP Code:
$this->sql->Insert("INSERT INTO ".$this->TABLES['invoice']." (filename) VALUES ('".$element."')"); 
puts the filename into the database. And if does it
PHP Code:
echo "$element \n" 

Ok, this does work, but not exactly how I wanted it.
It only selects the first database entry.

Basically, what I want it to do, is look at the database for a duplicate entry of $element.
If there is a duplicate entry, it does nothing.
If there isnt a duplicate entry, it inserts the $element
__________________
Websites Created;
warscope.com
ratepayers.org.nz

Last edited by lothop; 06-07-2005 at 02:43 AM..
lothop is offline
Reply With Quote
View Public Profile
 
Old 06-07-2005, 04:08 AM Horay!
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
WOOHOO

Managed to get it working

PHP Code:
foreach ($the_array as $element) {
 
$filename '$element';
        
$query mysql_query("SELECT * FROM invoice WHERE filename = '".$element."'");
        
$result mysql_fetch_array($query);
               if(!
$result){
                
$this->sql->Insert("INSERT INTO ".$this->TABLES['invoice']." (filename) VALUES ('".$element."')");
                } else {
                echo 
"$element<br>" ;
                 
                }
                }
  }
  } 
Thanks for ur help .Mike
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Database Information pull.
 

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15774 seconds with 12 queries