Jump to content

Problem creating folder with id number


insejn

Recommended Posts

I cant see what Im doing wrong here, I thought it would work but something simple must be wrong.. I'm trying to create a simple script that creates a new folder that has the same name as the latest id from my database table.

 

I thought this code should do it but it doesn't, I guess the problem is when the folder is supposed to be created with the code:

if (file_exists("images/$row['article_id']"))

 

This is the existing code now, is there a right way to do this?


<?
$host="localhost"; // Host name
$username="..."; // Mysql username
$password="..."; // Mysql password
$db_name="..."; // Database name
$tbl_name="articles_table"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Query to get the latest ID (article_id)
$result = mysql_query("SELECT * FROM articles_table ORDER BY article_id DESC LIMIT 1") 
or die(mysql_error());

//Make a while loop and display the right id number..
while($row = mysql_fetch_array( $result )) {

echo "Latest id is: ".$row['article_id']."<br>";

}

// Create a new folder with the latest id number...
if (file_exists("images/$row['article_id']")) {

  print("Folder already exists!!\n");
} else {
  mkdir("images/$row['article_id']");
  
//If Success then print out this..
  echo ("The new folder has been created for you!\n");
}

?>

 

..thanks in advance!

Link to comment
Share on other sites

..sorry its not that either. Is there a way to make a new variable containing the $row['article_id'] ?

Like for example:

 

$new_folder_name = $row['article_id'];

 

..but I dont seem to get that to work either. Or am I writing the code wrong? should I for example write:

$new_folder_name = "$row['article_id']";

or

$new_folder_name = ("$row['article_id']");

 

..I think as I said the problem is when I try to write the code

if (file_exists("images/$row['article_id']")) {

I think that way of integrating $row['article_id'] is wrong somehow..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.