We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

mkdir problem

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";
}
?>
3
Contributors
7
Replies
1 Hour
Discussion Span
5 Months Ago
Last Updated
8
Views
mpc123
Junior Poster
112 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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';
gavinflud
Light Poster
46 posts since Dec 2012
Reputation Points: 14
Solved Threads: 9
Skill Endorsements: 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";

broj1
Nearly a Posting Virtuoso
1,210 posts since Jan 2011
Reputation Points: 167
Solved Threads: 163
Skill Endorsements: 13

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

mpc123
Junior Poster
112 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

broj1
Nearly a Posting Virtuoso
1,210 posts since Jan 2011
Reputation Points: 167
Solved Threads: 163
Skill Endorsements: 13

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?

mpc123
Junior Poster
112 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

broj1
Nearly a Posting Virtuoso
1,210 posts since Jan 2011
Reputation Points: 167
Solved Threads: 163
Skill Endorsements: 13

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;
broj1
Nearly a Posting Virtuoso
1,210 posts since Jan 2011
Reputation Points: 167
Solved Threads: 163
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1007 seconds using 2.69MB