Click to See Complete Forum and Search --> : parsing error


miteshpandey
January 19th, 2008, 08:44 AM
I have the following php script:


$query_db_95 = "INSERT INTO `node_revisions` (`nid`, `vid`, `uid`, `title`, `body`, `teaser`, `log`, `timestamp`, `format`) VALUES
(451, 451, 1, 'Import model images', '<?php\r\n\r\n// Written by Victor Kane - victorkane at awebfactory dot com dot ar\r\n// *** This script affects your database\r\n//\r\n// Edits by Jeff Vogelsang - jeffvogelsang at pqa dot com\r\n\r\n// CONFIGURATION\r\n\r\n// Change the value below to TRUE when you want to run the script After running,\r\n// immediately change back to FALSE in order to prevent accidentally executing\r\n// this script twice.\r\n// NOTE: I find the best way to do this is to leave it false. When you are ready\r\n// to execute, set to this variable to true, and then click Preview. The script\r\n// will execute, but will not be saved with the value set to true. This prevents\r\n// accidentally saving the script with the value set to true. Also, it prevents\r\n// the unfortunate situation where the cron job runs while it''s set to true,\r\n// and you get double imports.\r\n$active = false;\r\n$process_folder = ''import'';\r\n\r\nset_time_limit (120);\r\n\r\n// Bootstrap Drupal\r\ninclude_once 'includes/bootstrap.inc';\r\ninclude_once('includes/common.inc');\r\ndrupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);\r\n\r\n// Check for the script being enabled, and for appropriate permissions.\r\nif ($active) {\r\n insert_content($process_folder);\r\n} else {\r\n print 'Variable active is set to false.';\r\n}\r\n\r\nfunction insert_content ($process_folder) {\r\n $folders = scandir($process_folder); \r\n foreach($folders as $folder){\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n // Make sure it is a directory and not a file\r\n if (is_dir($folder_path) && $folder != '.' && $folder != '..') {\r\n print ''<ul><li>found directory: '' . $folder . ''</ul>'';\r\n // Check if there is a node for the model \r\n // The node''s title should be the model name which then is the same as the directory\r\n if($node = node_load(array(title => $folder))){\r\n insert_images($process_folder,$folder, $node);\r\n }\r\n }\r\n }\r\n}\r\n \r\nfunction insert_images ($process_folder, $folder, $node) {\r\n $output = ''<ul>'';\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n if ($handle = opendir($folder_path)) {\r\n\r\n // Set the destination directory\r\n $dest = _node_images_get_directory($node);\r\n\r\n // Create the destination folders\r\n $directories = explode('/', $dest);\r\n $dirs = array();\r\n foreach($directories as $directory) {\r\n $dirs[] = $directory;\r\n if (!file_check_directory(implode('/', $dirs), 1)) {\r\n mkdir(implode('/', $dirs));\r\n }\r\n }\r\n\r\n $output .= ''<li>'' . $folder . '' ('' . $node->nid . '')<ul>'';\r\n\r\n // Counter for number of new images\r\n $i = 0;\r\n // Counter for number of total images\r\n $n = 0;\r\n // Go through each file in that folder\r\n while (false !== ($infile = readdir($handle))) { \r\n // Validate file extension\r\n $extensions = variable_get(''node_images_extensions'', ''jpg jpeg gif png'');\r\n $regex = ''/\\.(''. ereg_replace('' +'', ''|'', preg_quote($extensions)) .'')$/i'';\r\n // Make sure it is a file and with the right extension\r\n if ($infile != '.' && $infile != '..' && preg_match($regex, $infile)) {\r\n \r\n // Check if the file exists\r\n $result = db_query(''SELECT filename FROM {node_images} WHERE filename = ''' . $infile . ''' AND nid = ''' . $node->nid .''''');\r\n $obj = db_fetch_object($result);\r\n $n++;\r\n if(empty($obj->filename)){\r\n $file = new stdClass();\r\n $file->filename = $infile;\r\n \r\n // Create temporary name/path for newly uploaded files.\r\n $file->filepath = $folder_path . ''/'' . $file->filename;\r\n \r\n // Scale image\r\n $file = _upload_image($file);\r\n \r\n // Validation from upload.module\r\n $user = 1;\r\n $fid = ''upload_''.$user;\r\n $node->files = array($fid => $file);\r\n _upload_validate($node);\r\n if (!isset($node->files[$fid])) return FALSE;\r\n \r\n if (file_move($file, $dest)) {\r\n// if (file_copy($file, $dest)) {\r\n $i++;\r\n $output .= ''<li>Added: '' . $file->filename;\r\n $thumb = _node_images_create_thumbnail($file->filepath);\r\n $preview = _node_images_create_preview($file->filepath);\r\n $file->filesize = filesize($file->filepath);\r\n db_query('INSERT INTO {node_images} (nid, uid, filename, filepath, filemime, filesize, thumbpath, thumbsize, previewpath, previewsize, weight, description, page, position)\r\n VALUES (%d, %d, ''%s'', ''%s'', ''%s'', %d, ''%s'', %d, ''%s'', %d, %d, ''%s'', %d, %d)',\r\n $node->nid, 1, $file->filename, $file->filepath, $file->filemime, $file->filesize,\r\n $thumb->filepath, $thumb->filesize, $preview->filepath, $preview->filesize, 0, '''', 0,0);\r\n }\r\n }\r\n } \r\n }\r\n $output .= ''<li>out of a total of '' . $n . '' images, '' . $i . '' was added new for: '' . $folder . '', nid: '' . $node->nid;\r\n $output .= ''</ul>''; \r\n //mkdir($dest.''/''.$folder);\r\n closedir($handle);\r\n }\r\n $output .= ''</ul>'';\r\n print $output;\r\n}\r\n\r\n?>\r\n', '<?php\r\n\r\n// Written by Victor Kane - victorkane at awebfactory dot com dot ar\r\n// *** This script affects your database\r\n//\r\n// Edits by Jeff Vogelsang - jeffvogelsang at pqa dot com\r\n\r\n// CONFIGURATION\r\n\r\n// Change the value below to TRUE when you want to run the script After running,\r\n// immediately change back to FALSE in order to prevent accidentally executing\r\n// this script twice.\r\n// NOTE: I find the best way to do this is to leave it false. When you are ready\r\n// to execute, set to this variable to true, and then click Preview. The script\r\n// will execute, but will not be saved with the value set to true. This prevents\r\n// accidentally saving the script with the value set to true. Also, it prevents\r\n// the unfortunate situation where the cron job runs while it''s set to true,\r\n// and you get double imports.\r\n$active = false;\r\n$process_folder = ''import'';\r\n\r\nset_time_limit (120);\r\n\r\n// Bootstrap Drupal\r\ninclude_once 'includes/bootstrap.inc';\r\ninclude_once('includes/common.inc');\r\ndrupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);\r\n\r\n// Check for the script being enabled, and for appropriate permissions.\r\nif ($active) {\r\n insert_content($process_folder);\r\n} else {\r\n print 'Variable active is set to false.';\r\n}\r\n\r\nfunction insert_content ($process_folder) {\r\n $folders = scandir($process_folder); \r\n foreach($folders as $folder){\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n // Make sure it is a directory and not a file\r\n if (is_dir($folder_path) && $folder != '.' && $folder != '..') {\r\n print ''<ul><li>found directory: '' . $folder . ''</ul>'';\r\n // Check if there is a node for the model \r\n // The node''s title should be the model name which then is the same as the directory\r\n if($node = node_load(array(title => $folder))){\r\n insert_images($process_folder,$folder, $node);\r\n }\r\n }\r\n }\r\n}\r\n \r\nfunction insert_images ($process_folder, $folder, $node) {\r\n $output = ''<ul>'';\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n if ($handle = opendir($folder_path)) {\r\n\r\n // Set the destination directory\r\n $dest = _node_images_get_directory($node);\r\n\r\n // Create the destination folders\r\n $directories = explode('/', $dest);\r\n $dirs = array();\r\n foreach($directories as $directory) {\r\n $dirs[] = $directory;\r\n if (!file_check_directory(implode('/', $dirs), 1)) {\r\n mkdir(implode('/', $dirs));\r\n }\r\n }\r\n\r\n $output .= ''<li>'' . $folder . '' ('' . $node->nid . '')<ul>'';\r\n\r\n // Counter for number of new images\r\n $i = 0;\r\n // Counter for number of total images\r\n $n = 0;\r\n // Go through each file in that folder\r\n while (false !== ($infile = readdir($handle))) { \r\n // Validate file extension\r\n $extensions = variable_get(''node_images_extensions'', ''jpg jpeg gif png'');\r\n $regex = ''/\\.(''. ereg_replace('' +'', ''|'', preg_quote($extensions)) .'')$/i'';\r\n // Make sure it is a file and with the right extension\r\n if ($infile != '.' && $infile != '..' && preg_match($regex, $infile)) {\r\n \r\n // Check if the file exists\r\n $result = db_query(''SELECT filename FROM {node_images} WHERE filename = ''' . $infile . ''' AND nid = ''' . $node->nid .''''');\r\n $obj = db_fetch_object($result);\r\n $n++;\r\n if(empty($obj->filename)){\r\n $file = new stdClass();\r\n $file->filename = $infile;\r\n \r\n // Create temporary name/path for newly uploaded files.\r\n $file->filepath = $folder_path . ''/'' . $file->filename;\r\n \r\n // Scale image\r\n $file = _upload_image($file);\r\n \r\n // Validation from upload.module\r\n $user = 1;\r\n $fid = ''upload_''.$user;\r\n $node->files = array($fid => $file);\r\n _upload_validate($node);\r\n if (!isset($node->files[$fid])) return FALSE;\r\n \r\n if (file_move($file, $dest)) {\r\n// if (file_copy($file, $dest)) {\r\n $i++;\r\n $output .= ''<li>Added: '' . $file->filename;\r\n $thumb = _node_images_create_thumbnail($file->filepath);\r\n $preview = _node_images_create_preview($file->filepath);\r\n $file->filesize = filesize($file->filepath);\r\n db_query('INSERT INTO {node_images} (nid, uid, filename, filepath, filemime, filesize, thumbpath, thumbsize, previewpath, previewsize, weight, description, page, position)\r\n VALUES (%d, %d, ''%s'', ''%s'', ''%s'', %d, ''%s'', %d, ''%s'', %d, %d, ''%s'', %d, %d)',\r\n $node->nid, 1, $file->filename, $file->filepath, $file->filemime, $file->filesize,\r\n $thumb->filepath, $thumb->filesize, $preview->filepath, $preview->filesize, 0, '''', 0,0);\r\n }\r\n }\r\n } \r\n }\r\n $output .= ''<li>out of a total of '' . $n . '' images, '' . $i . '' was added new for: '' . $folder . '', nid: '' . $node->nid;\r\n $output .= ''</ul>''; \r\n //mkdir($dest.''/''.$folder);\r\n closedir($handle);\r\n }\r\n $output .= ''</ul>'';\r\n print $output;\r\n}\r\n\r\n?>\r\n', '', 1175798783, 2),


It is giving me the following error. What could be the reason for the error. Please Help. I am stuck here.


Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in g:\program files\apache group\apache\htdocs\capelondon.com\html\create.php on line 15187

Thanks in advance

miteshpandey
January 19th, 2008, 08:48 AM
I also tried the double quoted version. Still the parse error. But the error is a little different now.


(451, 451, 1, 'Import model images', '<?php\r\n\r\n// Written by Victor Kane - victorkane at awebfactory dot com dot ar\r\n// *** This script affects your database\r\n//\r\n// Edits by Jeff Vogelsang - jeffvogelsang at pqa dot com\r\n\r\n// CONFIGURATION\r\n\r\n// Change the value below to TRUE when you want to run the script After running,\r\n// immediately change back to FALSE in order to prevent accidentally executing\r\n// this script twice.\r\n// NOTE: I find the best way to do this is to leave it false. When you are ready\r\n// to execute, set to this variable to true, and then click Preview. The script\r\n// will execute, but will not be saved with the value set to true. This prevents\r\n// accidentally saving the script with the value set to true. Also, it prevents\r\n// the unfortunate situation where the cron job runs while it''s set to true,\r\n// and you get double imports.\r\n$active = false;\r\n$process_folder = ''import'';\r\n\r\nset_time_limit (120);\r\n\r\n// Bootstrap Drupal\r\ninclude_once "includes/bootstrap.inc";\r\ninclude_once("includes/common.inc");\r\ndrupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);\r\n\r\n// Check for the script being enabled, and for appropriate permissions.\r\nif ($active) {\r\n insert_content($process_folder);\r\n} else {\r\n print "Variable active is set to false.";\r\n}\r\n\r\nfunction insert_content ($process_folder) {\r\n $folders = scandir($process_folder); \r\n foreach($folders as $folder){\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n // Make sure it is a directory and not a file\r\n if (is_dir($folder_path) && $folder != "." && $folder != "..") {\r\n print ''<ul><li>found directory: '' . $folder . ''</ul>'';\r\n // Check if there is a node for the model \r\n // The node''s title should be the model name which then is the same as the directory\r\n if($node = node_load(array(title => $folder))){\r\n insert_images($process_folder,$folder, $node);\r\n }\r\n }\r\n }\r\n}\r\n \r\nfunction insert_images ($process_folder, $folder, $node) {\r\n $output = ''<ul>'';\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n if ($handle = opendir($folder_path)) {\r\n\r\n // Set the destination directory\r\n $dest = _node_images_get_directory($node);\r\n\r\n // Create the destination folders\r\n $directories = explode("/", $dest);\r\n $dirs = array();\r\n foreach($directories as $directory) {\r\n $dirs[] = $directory;\r\n if (!file_check_directory(implode("/", $dirs), 1)) {\r\n mkdir(implode("/", $dirs));\r\n }\r\n }\r\n\r\n $output .= ''<li>'' . $folder . '' ('' . $node->nid . '')<ul>'';\r\n\r\n // Counter for number of new images\r\n $i = 0;\r\n // Counter for number of total images\r\n $n = 0;\r\n // Go through each file in that folder\r\n while (false !== ($infile = readdir($handle))) { \r\n // Validate file extension\r\n $extensions = variable_get(''node_images_extensions'', ''jpg jpeg gif png'');\r\n $regex = ''/\\.(''. ereg_replace('' +'', ''|'', preg_quote($extensions)) .'')$/i'';\r\n // Make sure it is a file and with the right extension\r\n if ($infile != "." && $infile != ".." && preg_match($regex, $infile)) {\r\n \r\n // Check if the file exists\r\n $result = db_query(''SELECT filename FROM {node_images} WHERE filename = "'' . $infile . ''" AND nid = "'' . $node->nid .''"'');\r\n $obj = db_fetch_object($result);\r\n $n++;\r\n if(empty($obj->filename)){\r\n $file = new stdClass();\r\n $file->filename = $infile;\r\n \r\n // Create temporary name/path for newly uploaded files.\r\n $file->filepath = $folder_path . ''/'' . $file->filename;\r\n \r\n // Scale image\r\n $file = _upload_image($file);\r\n \r\n // Validation from upload.module\r\n $user = 1;\r\n $fid = ''upload_''.$user;\r\n $node->files = array($fid => $file);\r\n _upload_validate($node);\r\n if (!isset($node->files[$fid])) return FALSE;\r\n \r\n if (file_move($file, $dest)) {\r\n// if (file_copy($file, $dest)) {\r\n $i++;\r\n $output .= ''<li>Added: '' . $file->filename;\r\n $thumb = _node_images_create_thumbnail($file->filepath);\r\n $preview = _node_images_create_preview($file->filepath);\r\n $file->filesize = filesize($file->filepath);\r\n db_query("INSERT INTO {node_images} (nid, uid, filename, filepath, filemime, filesize, thumbpath, thumbsize, previewpath, previewsize, weight, description, page, position)\r\n VALUES (%d, %d, ''%s'', ''%s'', ''%s'', %d, ''%s'', %d, ''%s'', %d, %d, ''%s'', %d, %d)",\r\n $node->nid, 1, $file->filename, $file->filepath, $file->filemime, $file->filesize,\r\n $thumb->filepath, $thumb->filesize, $preview->filepath, $preview->filesize, 0, '''', 0,0);\r\n }\r\n }\r\n } \r\n }\r\n $output .= ''<li>out of a total of '' . $n . '' images, '' . $i . '' was added new for: '' . $folder . '', nid: '' . $node->nid;\r\n $output .= ''</ul>''; \r\n //mkdir($dest.''/''.$folder);\r\n closedir($handle);\r\n }\r\n $output .= ''</ul>'';\r\n print $output;\r\n}\r\n\r\n?>\r\n', '<?php\r\n\r\n// Written by Victor Kane - victorkane at awebfactory dot com dot ar\r\n// *** This script affects your database\r\n//\r\n// Edits by Jeff Vogelsang - jeffvogelsang at pqa dot com\r\n\r\n// CONFIGURATION\r\n\r\n// Change the value below to TRUE when you want to run the script After running,\r\n// immediately change back to FALSE in order to prevent accidentally executing\r\n// this script twice.\r\n// NOTE: I find the best way to do this is to leave it false. When you are ready\r\n// to execute, set to this variable to true, and then click Preview. The script\r\n// will execute, but will not be saved with the value set to true. This prevents\r\n// accidentally saving the script with the value set to true. Also, it prevents\r\n// the unfortunate situation where the cron job runs while it''s set to true,\r\n// and you get double imports.\r\n$active = false;\r\n$process_folder = ''import'';\r\n\r\nset_time_limit (120);\r\n\r\n// Bootstrap Drupal\r\ninclude_once "includes/bootstrap.inc";\r\ninclude_once("includes/common.inc");\r\ndrupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);\r\n\r\n// Check for the script being enabled, and for appropriate permissions.\r\nif ($active) {\r\n insert_content($process_folder);\r\n} else {\r\n print "Variable active is set to false.";\r\n}\r\n\r\nfunction insert_content ($process_folder) {\r\n $folders = scandir($process_folder); \r\n foreach($folders as $folder){\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n // Make sure it is a directory and not a file\r\n if (is_dir($folder_path) && $folder != "." && $folder != "..") {\r\n print ''<ul><li>found directory: '' . $folder . ''</ul>'';\r\n // Check if there is a node for the model \r\n // The node''s title should be the model name which then is the same as the directory\r\n if($node = node_load(array(title => $folder))){\r\n insert_images($process_folder,$folder, $node);\r\n }\r\n }\r\n }\r\n}\r\n \r\nfunction insert_images ($process_folder, $folder, $node) {\r\n $output = ''<ul>'';\r\n $folder_path = $process_folder . ''/'' . $folder;\r\n if ($handle = opendir($folder_path)) {\r\n\r\n // Set the destination directory\r\n $dest = _node_images_get_directory($node);\r\n\r\n // Create the destination folders\r\n $directories = explode("/", $dest);\r\n $dirs = array();\r\n foreach($directories as $directory) {\r\n $dirs[] = $directory;\r\n if (!file_check_directory(implode("/", $dirs), 1)) {\r\n mkdir(implode("/", $dirs));\r\n }\r\n }\r\n\r\n $output .= ''<li>'' . $folder . '' ('' . $node->nid . '')<ul>'';\r\n\r\n // Counter for number of new images\r\n $i = 0;\r\n // Counter for number of total images\r\n $n = 0;\r\n // Go through each file in that folder\r\n while (false !== ($infile = readdir($handle))) { \r\n // Validate file extension\r\n $extensions = variable_get(''node_images_extensions'', ''jpg jpeg gif png'');\r\n $regex = ''/\\.(''. ereg_replace('' +'', ''|'', preg_quote($extensions)) .'')$/i'';\r\n // Make sure it is a file and with the right extension\r\n if ($infile != "." && $infile != ".." && preg_match($regex, $infile)) {\r\n \r\n // Check if the file exists\r\n $result = db_query(''SELECT filename FROM {node_images} WHERE filename = "'' . $infile . ''" AND nid = "'' . $node->nid .''"'');\r\n $obj = db_fetch_object($result);\r\n $n++;\r\n if(empty($obj->filename)){\r\n $file = new stdClass();\r\n $file->filename = $infile;\r\n \r\n // Create temporary name/path for newly uploaded files.\r\n $file->filepath = $folder_path . ''/'' . $file->filename;\r\n \r\n // Scale image\r\n $file = _upload_image($file);\r\n \r\n // Validation from upload.module\r\n $user = 1;\r\n $fid = ''upload_''.$user;\r\n $node->files = array($fid => $file);\r\n _upload_validate($node);\r\n if (!isset($node->files[$fid])) return FALSE;\r\n \r\n if (file_move($file, $dest)) {\r\n// if (file_copy($file, $dest)) {\r\n $i++;\r\n $output .= ''<li>Added: '' . $file->filename;\r\n $thumb = _node_images_create_thumbnail($file->filepath);\r\n $preview = _node_images_create_preview($file->filepath);\r\n $file->filesize = filesize($file->filepath);\r\n db_query("INSERT INTO {node_images} (nid, uid, filename, filepath, filemime, filesize, thumbpath, thumbsize, previewpath, previewsize, weight, description, page, position)\r\n VALUES (%d, %d, ''%s'', ''%s'', ''%s'', %d, ''%s'', %d, ''%s'', %d, %d, ''%s'', %d, %d)",\r\n $node->nid, 1, $file->filename, $file->filepath, $file->filemime, $file->filesize,\r\n $thumb->filepath, $thumb->filesize, $preview->filepath, $preview->filesize, 0, '''', 0,0);\r\n }\r\n }\r\n } \r\n }\r\n $output .= ''<li>out of a total of '' . $n . '' images, '' . $i . '' was added new for: '' . $folder . '', nid: '' . $node->nid;\r\n $output .= ''</ul>''; \r\n //mkdir($dest.''/''.$folder);\r\n closedir($handle);\r\n }\r\n $output .= ''</ul>'';\r\n print $output;\r\n}\r\n\r\n?>\r\n', '', 1175798783, 2),



The error is as following:


Parse error: parse error in g:\program files\apache group\apache\htdocs\capelondon.com\html\create.php on line 15187

PeejAvery
January 19th, 2008, 11:06 AM
You have PHP tags in your SQL query. That will cause PHP to parse those as other script. Why don't you use &lt; for < and &gt; for >?

miteshpandey
January 19th, 2008, 10:59 PM
Hi PeejAvery,

Tried it but doesn't work. The same error is generated.

I think the problem is mismatch of "" and ' '.

At first I use " as in the following


"INSERT INTO


Then inside the above string I use:


'<?php\r\n\r\n//.........................


Can you tell me where the mismatch is occurring?

PeejAvery
January 19th, 2008, 11:42 PM
Don't you have an editor you can use to follow the coloring? Either way, you will have change the symbols or else it will try to parse those as actual PHP tags.

Also, why do you end with a comma instead of a semicolon?

miteshpandey
January 20th, 2008, 05:36 AM
Hi PeejAvery,

Sorry for the comma at the end. Actually I have a lot of data and each data is being separated by the comma, I have the (") ending after the last set of data.

I have applied &lt; and &gt; for < and > but the same errors persist.

Some more information:

There are some php variables used which you can see yourself in my earlier post. For example $active and $process_folder and these are php variables. These variable strings are colored brown which denotes that they are actually variables. Actually they should have appeared as green which makes them as text rather than variables. So my question is how can I make the variables appear as strings (i.e. green color)?

Btw, thanks for your help

PeejAvery
January 20th, 2008, 09:01 AM
Remember that single-quotes cannot echo variables. You can do one of the following to correct that.

echo "$variable";
echo 'sometext' . $variable . 'moretext';
However, I think your problem is in your escape strings. To escape a single quote, you need to use \' instead of ''. Don't forget, if you have characters that need to be escaped within a variable you can use mysql_real_escape_string().

Also, it might help if you used [php] tags when posting instead of [code].