Solved

Parse error: syntax error, unexpected '*', expecting ')' in /var/www/html/simeswiki/test.php on line 8

Posted on 2008-06-26
26
214 Views
Last Modified: 2008-06-26
Hi guys hope you are well.

Im having problems with my php file as follows and would love your help.

Im having issues with the line that reads..

foreach ( glob(*.php) as $filename ) {

Any help greatly appreciated.
Thanks guys.
<?php
 

// Make a MySQL Connection

mysql_connect("localhost", "root", "password") or die(mysql_error());

mysql_select_db("simeswiki") or die(mysql_error());
 
 

foreach ( glob(*.php) as $filename ) {

  $r = mysql_query ( "select count(*) from filename_fil where file_fil='$filename'" );

  list ( $cnt ) = mysql_fetch_row($r);

  if ( $cnt ) {

      // file exists in the database

      mysql_query ( "insert into filename_fil (...,file_fil,...) values (...,'$filename',...)" );

  }

  else {

     // file does not exist

  }

}

Open in new window

0
Comment
Question by:Simon336697
  • 16
  • 9
26 Comments
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
Try this:
 
foreach ( glob('*.php') as $filename ) {
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Hi gl,
Champion mate thank you.
Im now getting the following gl..

Call to undefined function mysql_connect() on line 4.
0
 
LVL 9

Assisted Solution

by:Rob Siklos
Rob Siklos earned 30 total points
Comment Utility
Check your php.ini file - make sure the MySQL extension is enabled.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Guys,

in my php.ini file, i have
 extension=mysql.so

[root@localhost simeswiki]# rpm -qa | grep php
php-common-5.1.6-5.el5
php-5.1.6-5.el5
php-cli-5.1.6-5.el5
php-pdo-5.1.6-5.el5
php-odbc-5.1.6-5.el5
php-ldap-5.1.6-5.el5
php-mysql-5.1.6-5.el5

Im still getting the same error when I restart apache and mysqld
0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
1. Ensure that the php.ini you found is the one that Apache is using. To do this, place 'phpinfo();' in a php script, and execute it. Examine the line that indicates the location of php.ini, and ensure that it matches the php.ini file you found.

2. Do a search on your system to determine where mysql.so is installed.

3. Check the php.ini file for the setting named 'extension_dir', and make sure it includes the directory found in step 2.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Okay got it guys...

Thank you so much...it was the following in my httpd.conf

    574 extension=mysql.so
    582 extension_dir=/usr/lib/php/modules

I had to do the above.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Guys what the php file is meant to do is to..

1) Poll all files in a folder with a .php extension, and then
2) Insert the filenames into a table called filename_fil in my mysql database called simeswiki.

When I execute the page, I dont get any errors, but when I then do a select * on filename_fil, there are no records inserted.

My table design is as follows for mysql..

create table filename_fil
(
id_fil INT NOT NULL AUTO_INCREMENT,
file_fil VARCHAR(255) NOT NULL,
PRIMARY KEY (id_fil),
UNIQUE (file_fil)
)

the php code again is as follows..

<?php

 

// Make a MySQL Connection

mysql_connect("localhost", "root", "password") or die(mysql_error());

mysql_select_db("simeswiki") or die(mysql_error());

 

 

foreach ( glob('*.php') as $filename ) {

  $r = mysql_query ( "select count(*) from filename_fil where file_fil='$filename'" );

  list ( $cnt ) = mysql_fetch_row($r);

  if ( $cnt ) {

      // file exists in the database

      mysql_query ( "insert into filename_fil (...,file_fil,...) values (...,'$filename',...)" );

  }

  else {

     // file does not exist

  }

}

Open in new window

0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
Each time you execute a MySQL query, you should check the results to see if the query failed. For example, the following line:

 $r = mysql_query ( "select count(*) from filename_fil where file_fil='$filename'" );

should become:

$query =  "select count(*) from filename_fil where file_fil='$filename'";
if (false == ($r = mysql_query($query)))
{
   die("Query failed: $query.<br />MySQL said: " . mysql_error());
}

This will reveal your error to you, and help you to determine whey the query is failing. This should be done each time you execute a MySQL query.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Thanks gl,

Ive used your mod...Im still not getting any output error messages...page is blank.
No records inserted.

The folder where i have placed this test.php script contains php files like the following...

menu4_it_virtual_vpc_scriptskb.php
menu4_it_virtual_vpc_scriptswk.php

What Im trying to do is to insert a record into my filename_fil table for all these php files.

So in my table, which has 2 fields, namely

id_fil
filename_fil

Im trying to get...

id_fil                     filename_fil
1                          menu4_it_virtual_vpc_scriptskb.php
2                          menu4_it_virtual_vpc_scriptswk.php
etc etc

So when I add any new php files to this folder, or delete any, a new insert would go into the database for the addition, or remove ones from the database that no longer exist on disk.
0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
Did you make the modification I suggested on each of your mysql queries? You will need to do it twice, according to the code you posted earlier.

Second, examine your logic. You query the database to determine if the file already exists in the table. If it does, you run your insert statement. If it does not exists, your insert statement never runs. Is this the way you want your code to work?

If that does not fix the problem, please post your code as it now exists.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
gl you are so helpful.
Sorry to be vague here.
This is what id like..

1) Query the database to see if the file already exists.
2) If it DOES NOT, then run the insert statement to insert it as a new record
3) If it DOES exist, then dont insert it

Here is my current code gl...thnku so much...
 <?php
 

 // Make a MySQL Connection

 mysql_connect("localhost", "root", "password") or die(mysql_error());

 mysql_select_db("simeswiki") or die(mysql_error());
 
 

 foreach ( glob('*.php') as $filename ) {

         $query =  "select count(*) from filename_fil where file_fil='$filename'";

                 if (false == ($r = mysql_query($query)))

                 {

                 die("Query failed: $query.<br />MySQL said: " . mysql_error());

         }

   list ( $cnt ) = mysql_fetch_row($r);

   if ( $cnt ) {

       // file exists in the database

      //  mysql_query ( "insert into filename_fil (...,file_fil,...) values (...,'$filename',...)" );

   }

   else {

      // file does not exist
 

       mysql_query ( "insert into filename_fil (...,file_fil,...) values (...,'$filename',...)" );

   }

 }

?>

Open in new window

0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
You are not running the check I suggested on your second query:

 mysql_query ( "insert into filename_fil (...,file_fil,...) values (...,'$filename',...)" );

Should be :

$query = "insert into filename_fil (...,file_fil,...) values (...,'$filename',...)";
if (false === mysql_query($query))
{
   die("The query failed: $query.<br />MySQL said: " . mysql_error());
}
0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
You need to be in the habit of checking each time you run a mysql_query(). Your code will act in an unexpected manner if you do not explicitly check for errors.

mysql_query() will return FALSE if the query fails. Therefore, check if the result of mysql_query() is false, and if so, examine the output of mysql_error(), which will contain the error message.

If you ever execute a mysql_query() and do not check the results, but rather just continue running your script, you will continue to have trouble determining the problem each time your script fails.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:Simon336697
Comment Utility
gl.....what you say is 100% right, and im going to start doing this religiously.

Here is the output after i have done what you said...

The query failed: insert into filename_fil (...,file_fil,...) values (...,'backup.menu4_it_messaging_outlook_scripts.php',...).
MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '..,file_fil,...) values (...,'backup.menu4_it_messaging_outlook_scripts.php',...' at line 1

Below is the code im using..
<?php
 

// Make a MySQL Connection

mysql_connect("localhost", "root", "password") or die(mysql_error());

mysql_select_db("simeswiki") or die(mysql_error());
 
 

foreach ( glob('*.php') as $filename ) {

        $query =  "select count(*) from filename_fil where file_fil='$filename'";

                if (false == ($r = mysql_query($query)))

                {

                die("Query failed: $query.<br />MySQL said: " . mysql_error());

        }

  list ( $cnt ) = mysql_fetch_row($r);

  if ( $cnt ) {

      // file exists in the database

  }

  else {

     // file does not exist
 

$query = "insert into filename_fil (file_fil) values ('$filename')";

if (false === mysql_query($query))

{

   die("The query failed: $query.<br />MySQL said: " . mysql_error());

}

  }

}

?>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
YOURE A CHAMPION!!

The above code now works thanks to you...

Sorry in the above i should have posted the error i had in the php file which was..

 "insert into filename_fil (...,file_fil,...) values (...,'$filename',...)" );


After I removed the ... in the (  ), it inserted the records...

gl, one last question if that is okay...

If i remove a file from disk, and then run the php page we've been working on, it does not remove that file from the database which is the last thing id ideally like this to do.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Would you use something like a case statement after

  if ( $cnt ) {
      // file exists in the database

            and if file does not exist on disk, then delete from database
            and if file does exist on disk, the do nothing
           
0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
Okay, to accomplish that, you will need to create a different section of code entirely. The current section examines the files on the disk, loops through them, and creates new records in the table based on those files. This loop knows nothing about records that exist in the table without matching filenames.

Therefore, you will need to create code that will loop through the records in the database, determine if the file exists on the disk, and act accordingly. In code, it will look something like this:

$query = "SELECT filenames FROM yourTable";
if (false === ($results = mysql_query($query))
{
   die("Query failed: $query. MySQL said: " . mysql_error());
}

while (false !== ($row = mysql_fetch_object($result)))
{
   if (!file_exists($row->filename))
   {
        $deleteQuery = "DELETE FROM yourTable WHERE filename=' . $row->filename . ' LIMIT 1;";
        if (false === mysql_query($deleteQuery))
        {
             die("Query failed: $query.<br />MySQL said: " . mysql_error());
        }
    }
}

This will check each of your records against the filename, and delete the record if the file does not exist. You will need to update this code with the correct table and field names, as well as ensuring that the correct path is checked in the 'file_exists()' line.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
ok....
Ive added the following code gl...

I added a ) to line 36 as i was getting..

Parse error: syntax error, unexpected '{' in /var/www/html/simeswiki/dome.php on line 36

Once I did this (with the below code), I am now getting...

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /var/www/html/simeswiki/dome.php on line 40

Line 40 in my code is..

 40 while (false !== ($row = mysql_fetch_object($result)))


 // Examine entries in the database --> while looking at each, delete the db record if it does not exist on disk.
 

 $query = "SELECT file_fil FROM filename_fil";

 if (false === ($results = mysql_query($query)))

 {

    die("Query failed: $query. MySQL said: " . mysql_error());

 }
 

 while (false !== ($row = mysql_fetch_object($result)))

 {

    if (!file_exists($row->filename))

    {

         $deleteQuery = "DELETE FROM filename_fil WHERE filename=' . $row->filename . ' LIMIT 1;";

         if (false === mysql_query($deleteQuery))

         {

              die("Query failed: $query.<br />MySQL said: " . mysql_error());

         }

     }

 }

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Ok, I changed it because i think $result in the above at line 9 is meant to be $results.

When I did this, im getting..

Query failed: SELECT file_fil FROM filename_fil.
MySQL said: Unknown column 'filename' in 'where clause'

In my table, i have 2 columns, namely...
id_fil
and
filename_fil
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Sorry in the above the second column is not filename_fil but file_fil
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Ok no errors this time.....but its not deleting entries from the database when there is no file on disk anymore...after Ive deleted the file from disk...

Current code...


 <?php
 

 // Make a MySQL Connection

 mysql_connect("localhost", "root", "password") or die(mysql_error());

 mysql_select_db("simeswiki") or die(mysql_error());
 
 

 // Examine the files on disk --> loop through them --> create records in filename_fil table.
 

 foreach ( glob('*.php') as $filename ) {

         $query =  "select count(*) from filename_fil where file_fil='$filename'";

                 if (false == ($r = mysql_query($query)))

                 {

                 die("Query failed: $query.<br />MySQL said: " . mysql_error());

         }

   list ( $cnt ) = mysql_fetch_row($r);

   if ( $cnt ) {

       // file exists in the database

   }

   else {

      // file does not exist
 

 $query = "insert into filename_fil (file_fil) values ('$filename')";

 if (false === mysql_query($query))

 {

    die("The query failed: $query.<br />MySQL said: " . mysql_error());

 }

   }

 }
 
 

 // Examine entries in the database --> while looking at each, delete the db record if it does not exist on disk.
 

 $query = "SELECT file_fil FROM filename_fil";

 if (false === ($results = mysql_query($query)))

 {

    die("Query failed: $query. MySQL said: " . mysql_error());

 }
 

 while (false !== ($row = mysql_fetch_object($results)))

 {

    if (!file_exists($row->filename))

    {

         $deleteQuery = "DELETE FROM filename_fil WHERE file_fil=' . $row->filename . ' LIMIT 1;";

         if (false === mysql_query($deleteQuery))

         {

              die("Query failed: $query.<br />MySQL said: " . mysql_error());

         }

     }

 }

?>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
gl,

I think im getting confused with the

if (!file_exists($row->filename))

When you say "....as well as ensuring that the correct path is checked in the 'file_exists()' line...

Im not sure what filename should be...
is $row->filename looking at the files on disk and not entries in the db?
If so, how do i say to look at the same folder location for these files eg.this php script runs from the same folder as the files im trying to examine.
Should filename be $filename?
0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
   if (!file_exists($row->filename))


should match your column name, so in this case, you can change it to:

    if (!file_exists($row->file_fil))

Then you will also want to change the query that deletes the file to use the same column name. $row contains the data returned from the previous query, so when you use it, it must match your table structure.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Im so sorry im a slowcoach...

IT'S WORKING YOU BEAUTY!!!

I dont know why, but i removed the  .'s to the left and right of $row_fil

$deleteQuery = "DELETE FROM filename_fil WHERE file_fil=' . $row->file_fil . ' LIMIT 1;";

to make it below..

 $deleteQuery = "DELETE FROM filename_fil WHERE file_fil='$row->file_fil' LIMIT 1;";

I CANNOT THANK YOU ENOUGH GL FOR YOUR PATIENCE WITH ME.
You are a true gentleman and a genius.
0
 
LVL 24

Accepted Solution

by:
glcummins earned 470 total points
Comment Utility
Ah, I see. That was my error. The filename needs to have single-quotes around it. So, your way will work, or:

$deleteQuery = "DELETE FROM filename_fil WHERE file_fil='" . $row->file_fil . "' LIMIT 1;";

I am glad it is working properly now, and wish you great success as this project continues!
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
gl,

im going to post a comment to Experts-Exchange management about you and some other people on here stating how wonderfully helpful and brilliant you guys are.
Thanks so much again :>)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now