1,105,625 Community Members

mkdir problem

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HI

I am trying to create folders from database, but get this error as im trying to make folders named field1-field2-info

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 8

<?php
connection blar
connection blar
$query = "SELECT field1, field2 FROM Table";     
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$dirPath = "' . $row['field1'] . '-' . $row['field2'] . '-info";
$result = mkdir($dirPath, 0755);
if ($result == 1) {
    echo $dirPath . " has been created";
} else {
    echo $dirPath . " has NOT been created";
}
?>
Member Avatar
gavinflud
Light Poster
46 posts since Dec 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

It probably has something to do with accessing the row[field1] value using single quotes, which may be being read as the closing quotation mark to the original. Try this:

$dirPath = $row['field1'] . '-' . $row['field2'] . '-info';
Member Avatar
broj1
Posting Virtuoso
1,578 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

The double quotes are causing trouble since everything betwen the opening and the closing double quote is a string (array elements do not get parsed correctly and also no concatenation takes place). You can do it the way gavinflud suggested. On the other hand if you wish to use double quotes you have to enclose $row elements in curly braces to get them parsed correctly:

$dirPath = "{$row['field1']}-{$row['field2']}-info";

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Great thanks that now works but it get s to the first line in the database and creates that as a folder then you get this error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given on line 6

Member Avatar
broj1
Posting Virtuoso
1,578 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

mysql_query returns a resource on success or false on error. It seems that it returned false so an error must have happened when issuing a query. The error should be caught by the second part of the statement: or die(mysql_error(), but for soem reason it does not. You can add this code just after the line 5 to check for the error:

if(!result) {die( mysql_error()); }

which is essentialy what the or part should do.

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HI Thanks

It now has no error but still only creates 1 folder rather than about 200. I have tried my query seperately and it returns all of them?

Why does it stop at just making 1 folder please?

Member Avatar
broj1
Posting Virtuoso
1,578 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

In your code above the closing curly brace (belonging to the while loop) is missing.

Anyway, you can test it in such a way that instead of creating folders just echo the commands

while($row = mysql_fetch_array($result))
{
    echo "{$row['field1']}-{$row['field2']}-info<br />";
}

and check (or post) the output.

Member Avatar
broj1
Posting Virtuoso
1,578 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

Maybe there are characters in the directory names that are not allowed. Have you checked that? You can also post the output of the following select statement (the first 10 records):

'SELECT field1, field2 FROM Table LIMIT 10;
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article