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";
}
?>

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';

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";

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

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.

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?

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.

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.