Jump to content

Using Database Values with fopen


Mtowns Finest

Recommended Posts

Hello -

 

I'm new to PHP, only been learning it for about a month, but I think I am starting to get the hang of it. I am working on a project that involves the Google Maps API which uses markers that are populated by values from a MySQL database.

 

The javascript code that populates the map markers is the following.

var html = '<b>' + name + '</b> <br/>' + address + '<br />' + '<a href=\"/locations/' + state + '/' + city + '/' + name + '\">View Profile</a>';

 

As you can see it creates a file path using the values for state, city, and name.

 

I am now trying to write a script that runs through my database and creates the file for each row in my database. The script works when I remove the database variables, but does not work when I have it the way it currently is. I already went through the error removing process, and I now just get a black screen when I run the script, which should mean its working. Unfortunately however, the files are not created.

 

Here is my current code.

<?php  

ini_set('display_errors',1);
error_reporting(E_ALL);  


$contents = 'Testing to see if this makes it onto the page'; 


include ('mysql_connect.php');
$query = "SELECT id, name, state, city, as sd FROM markers";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_assoc($result)) {

$mydir = $_SERVER['DOCUMENT_ROOT'].'/locations/';
if(!empty($row['state']) && !empty($row['city']) && !empty($row['name'])){
   $mydir .= $row['state'].'/'.$row['city'].'/';
   if(file_exists($mydir) && is_writable($mydir)){
      $mydir .= $row['name'].'.php';
      // put the rest of the code to write the file here



$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/locations/'.$row['state'].'/'.$row['city'].'/'.$row['name'].'.php', 

"w") or die("Could Not Open File");  
  

$lock = flock($handle, LOCK_EX);  

if ($lock){ 
fputs($handle, $contents);  
flock($handle, LOCK_UN);  
}  
fclose($handle); 
}
} 
}
}  

?> 

 

Any help is greatly appreciated. I have looked everywhere for guides or examples that use variables in a similar way but I have had no luck.

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.