Pixel-Picasso 03-26-2009, 03:06 AM Hello,
I am fairly new to PHP, so while I think I am on the right track, I probably am not. I am attempting to make a script that runs through a database and creates new files for each id using the values of the variables. The variables themselves set the file path and the name of the file. The ID is auto incrementing, and I know the values are in the database.
I will breifly explain my project. Im using the Google Maps API, and I populate my map with markers that get their data from a database. Each marker on the map is populated by the following javascript code:
var html = '<b>' + name + '</b> <br/>' + address + '<br />' + '<a href=\"/locations/' + state + '/' + city + '/' + name + '\">View Profile</a>'
As you can see a URL path is set by the variables. I now need to run through the same database to actually create those files the markers are linking to.
My code so far is the following
<?php
$contents = 'Testing to see if this makes it onto the page';
include ('mysql_connect.php');
$query = "SELECT id, name, address, city, state, as sd FROM markers";
$result = @mysql_query($query);
if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/locations/'.$state.'/'.$city.'/'.$name.'.php', "w");
$lock = flock($handle, LOCK_EX);
if ($lock){
fputs($handle, $contents);
flock($handle, LOCK_UN);
}
fclose($handle);
}
}
?>
I worked through all the errors, and now I get a blank page when I run it so I would assume it should be working. However, no files are created in the locations.
Any ideas on what I am doing wrong? And am I on the right path towards what I am working towards?
Thanks
oesxyl 03-26-2009, 04:27 AM add this:
ini_set('display_errors',1);
error_reporting(E_ALL);
to the top of the file. This will probably show what's wrong in case you still have errors.
I guess the problem is that you don't have write permission to that location.
other thing, you can miss one or more directory on that path
best regards
Pixel-Picasso 03-26-2009, 04:51 AM Thanks for the response oesxyl.
I added that code, and still get a blank page, so I am fairly sure now that the function itself works, but my connection to MySQL or my variable path is the problem.
By removing the MySQL and the variables from the database, I got a code that successfully created a new file and added the value of $contents.
<?php
$contents = 'Testing to see if this makes it onto the page';
$handle = fopen(test.'.'.txt, "w");
$lock = flock($handle, LOCK_EX);
if ($lock){
fputs($handle, $contents);
flock($handle, LOCK_UN);
}
fclose($handle);
?>
I think at this point I will rebuild the code step by step and see if I can find the problem now that I have somewhere to start.
If anyone has any ideas still, they are greatly apreciated.
Inigoesdr 03-26-2009, 05:20 AM Your file path is incorrect. The variables you're referencing don't exist in the code you posted. You need to use $row['state'], $row['city'], $row['name'] instead of $state, $city, $name. This is because the current result from your query is stored in an array called $row in this case. By the by, you can use mysql_fetch_assoc (http://php.net/mysql_fetch_assoc)($result) vs. mysql_fetch_array($result, MYSQL_ASSOC).
oesxyl 03-26-2009, 05:50 AM Thanks for the response oesxyl.
I added that code, and still get a blank page, so I am fairly sure now that the function itself works, but my connection to MySQL or my variable path is the problem.
By removing the MySQL and the variables from the database, I got a code that successfully created a new file and added the value of $contents.
<?php
$contents = 'Testing to see if this makes it onto the page';
$handle = fopen(test.'.'.txt, "w");
$lock = flock($handle, LOCK_EX);
if ($lock){
fputs($handle, $contents);
flock($handle, LOCK_UN);
}
fclose($handle);
?>
I think at this point I will rebuild the code step by step and see if I can find the problem now that I have somewhere to start.
If anyone has any ideas still, they are greatly apreciated.
Inigoesdr is right, I didn't see that, :)
btw before writing to the file, you could echo the content to check if is what you expect.
best regards
Pixel-Picasso 03-26-2009, 06:16 AM Hmm I changed the code to include the $row but I still cannot get it to create the file path.
If I remove the variables it works fine, so I guess it just is not getting the values of the variables stored in the database. Do you by chance see anything that looks wrong? Should I include .$row['id']. amywhere? Something just has to be off somewhere that relates to actually getting the values of the variables.
My current code is:
<?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)) {
$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/locations/'.$row['state'].'/'.$row['city'].'/'.$row['name'].'.php', "w");
$lock = flock($handle, LOCK_EX);
if ($lock){
fputs($handle, $contents);
flock($handle, LOCK_UN);
}
fclose($handle);
}
}
?>
And thanks again for the help to everyone helping out, I really do apreciate it. I spent about 7 hours going through the archives and learned a ton of PHP here today.
*edit*
When I ran it without database variables for the path, my result was http://davasso.com/locations/test.php, so at least that part works.
oesxyl 03-26-2009, 06:28 AM $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
}
}
also you must add some checking and error message to see what's wrong if don't work.
best regards
Pixel-Picasso 03-26-2009, 05:42 PM Added that code, unfortuanately same results so I think I will just have to toy around with it. It simply is not getting the values from the database, though it is connecting.
As for the error reporting, what kind of error reporting should I use in this case? When I first created the code I had like 4 or 5 errors, and then fixed them and it quit telling me errors.
Is there a simple method to see if my script is pulling the values?
My current setup is
<?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($mydir.$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);
}}
}}
?>
I added or die("Could Not Open File"); to see if it couldn't create the file, but apparently it thinks it is creating the file. I have never really had problems pulling simple data from a database before, usually seems to be fairly straight forward.
oesxyl 03-26-2009, 11:50 PM change what is commented:
<?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
// next line is wrong because state, city and name is already in mydir
// $handle = fopen($mydir.$row['state'].'/'.$row['city'].'/'.$row['name'].'.php', "w") or die("Could Not Open File");
// must be:
$handle = fopen($mydir, "w") or die("Could Not Open File");
$lock = flock($handle, LOCK_EX);
if ($lock){
fputs($handle, $contents);
flock($handle, LOCK_UN);
}
fclose($handle);
}}
}}
?>
best regards
Pixel-Picasso 03-27-2009, 02:13 AM Hey thanks for the help oesxyl,
Again no luck however, it simply absolutely refuses to pull those values from the database I guess. With all the different variations I have tried I think I'm going to have to throw in the towel on this one. I have tried finding references or examples that also use database values to create a path, however it seems that none exist that I can find.
I was looking through the archives though, and came across http://www.codingforums.com/archive/index.php/t-107879.html. I dont exactly know how this works, however I kind of understand the concept. I couldnt find anymore topics that cover it, so I was wondering if anyone could possibly point me in the right direction to where I could learn a little more about the process and how it works. Or what keywords I should use to search for it.
Thanks again.
oesxyl 03-27-2009, 02:18 AM Hey thanks for the help oesxyl,
Again no luck however, it simply absolutely refuses to pull those values from the database I guess. With all the different variations I have tried I think I'm going to have to throw in the towel on this one. I have tried finding references or examples that also use database values to create a path, however it seems that none exist that I can find.
I was looking through the archives though, and came across http://www.codingforums.com/archive/index.php/t-107879.html. I dont exactly know how this works, however I kind of understand the concept. I couldnt find anymore topics that cover it, so I was wondering if anyone could possibly point me in the right direction to where I could learn a little more about the process and how it works. Or what keywords I should use to search for it.
Thanks again.
try this:
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
check state, city and name to be not empty.
best regards
Pixel-Picasso 03-27-2009, 02:40 AM Ah thanks, since the page is still blank and doesnt output the values, im guessing the problem lies somewhere in the MySQL?
My code looks like
<?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)) {
print_r($row);
}
}
?>
oesxyl 03-27-2009, 03:20 AM Ah thanks, since the page is still blank and doesnt output the values, im guessing the problem lies somewhere in the MySQL?
My code looks like
<?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)) {
print_r($row);
}
}
?>
try this:
if($result){
// code....
}else{
print '<pre>'.$query.'</pre>';
print '<pre>'.mysql_error().'</pre>';
}
best regards
|
|