Re: Algorithm isn't working




<mythis@xxxxxxxxx> wrote in message
news:1121022819.584177.139070@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hey guys,
>
> I seem to be having a problem with part of my code. This code is
> supposed to connect to a database and perform a query to get all the
> name fields in all the records. The program then compares the results
> to the file names of all the files in a certain directory. I'm doing
> this so I will know if another record needs to be added.
> Unfortunately, the algorithm I am using does not work. I can't seem to
> find what is wrong with it. Instead of the giving me the next file
> name that needs to be added, it gives me the last file name that I have
> already added.
>
> Here is the code:
> <?php
> $files = scandir("c:\apache2triad\htdocs\pictures");
> $numberElements = count($files);
>
> // Connect to the server and select the database
> @mysql_connect("localhost", "user", "password")
> or die("Could not connect to MySQL server!");
> @mysql_select_db("website") or die("Could not select database!");
>
> // process query
> $query = "SELECT name FROM pictures WHERE 1";
> $result = mysql_query($query);
>
> if(!$result) echo "Error processing query!";
>
> // find number of rows returned in the result of query
> $numberRows = mysql_numrows($result);
>
> // compare query results with file names
> for( $rowNumber = $numberRows; $rowNumber > 0; $rowNumber-- )
> {
> $exists = false;
> $name = mysql_result( $result, $rowNumber - 1, name );
> for( $elementNumber = $numberElements; $elementNumber > 0;
> $elementNumber-- )
> {
> if( $files[elementNumber - 1] == $name )
> $exists = true;
> }
> if( $exists == false )
> {
> echo "File $name needs to be added to the database!";
> break;
> }
> }
>
> mysql_close();
> ?>
>
> Can you help me?
> Thanks!
>

What makes you so certain that the order in the $files array corresponds
one-to-one with the result of the query? Also, you say that you want to add
a record (to the DB) if the file is not there. Yet, you are only searching
over the records in the DB and not over the files you have from scandir. It
seems the opposite of what you want.

I think you should first query the database as you do and build an array of
names from there. Then you should loop over the files in the array you
obtain from scandir. For each loop you should check for its presence in the
array from the query. If it is not there, then add it.

Shelly


.



Relevant Pages

  • Re: Converting from Access to SQL2k via Excel- HELP!!!!!
    ... Then create a new query and paste the following into its SQL ... > In your database you'd need to start with a query that ... The function that you pointed me to allows me to concatenate all ... The problem that I have is that I need to put it into a loop ...
    (microsoft.public.access.conversion)
  • RE: Array Help
    ... Dim strSQL As String ... the Access "INSERT INTO" SQL query is: ... one field is changed for each query, I was thinking that I could loop though ... I am not sure how to do a loop through the array or write any code for it as ...
    (microsoft.public.access.modulesdaovba)
  • RE: Delete All Rows Each Table
    ... You can loop through the table collection and then perform a sim Query to ... If Err.Number = 0 Then Exit Function ... I have a MS-Access database that has approximately 20 tables. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Copying a table to an array
    ... You probably don't need a query, ... Dim rstRSet As Recordset ... table to enable data from the latter to be read into an array with GetRows. ... exercise has therefore to be repeated prior to each interrogation. ...
    (microsoft.public.access.formscoding)
  • Re: Mix/Max/Avg Help based on dynamic ranges
    ... Did you enter Biff's formula as an array? ... The query I gave you is dependent on your breaking out the ranges as Biff ... Where Nums are the values to avg based on the head count range. ... deemed optimal for building device profiles based on locn size and device ...
    (microsoft.public.excel.worksheet.functions)