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:\apa che2triad\htdoc s\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_d b("website") or die("Could not select database!");
// process query
$query = "SELECT name FROM pictures WHERE 1";
$result = mysql_query($qu ery);
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!
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:\apa che2triad\htdoc s\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_d b("website") or die("Could not select database!");
// process query
$query = "SELECT name FROM pictures WHERE 1";
$result = mysql_query($qu ery);
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!
Comment