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
https://forums.phpfreaks.com/topic/151245-using-database-values-with-fopen/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.