Domanda

The previous solutions working with MS Access did not pan out so I am trying this time with php.

I have this php file that opens a database, reads a list of records and creates an html file for each record, in their respective folder name (folder names also found in the record's fields)

The code seems to work but it doesn't go past the first record. I don't get any type of error message at all, so I am confused as to what the problem would be. I created the code out of many posts found here. The only thing I am wondering myself is whether the open and write functions (or whatever they are called) are in the correct sequence in the script. Perhaps the cause is something totally different.

Basically, what I am trying to do is for the script to create a "configuration" php file for each domain in its respective folder. The only difference between all the configuration files is the domainid field.

The table in the dbase is named domains. The fields are domainid which is an unique number; domain, which contains the domain name - e.g. domain.com - and it is used as the domain folder; and domaingroup, which is used as the "category" folder.

I changed all values for security purposes but the db connection works fine.

<?php
$db_name = "dbname"; 
$dbusername = "dbname"; 
$dbpassword = "password"; 
$server = "dbname.blahblahbla.hosted.com";

$connection = mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = mysql_select_db($db_name,$connection)or die(mysql_error());

    $htmlquery = "select * from domains ORDER BY domain";
    $htmlresult = mysql_query($htmlquery,$connection) or die(mysql_error());
    $htmlinfo = mysql_fetch_array($htmlresult);

    if ($htmlresult == 0) { 
        echo "<p>No Recourds Found</p>";
    } else {

    for ($i=0; $i <$htmlresult; $i++) { 

$p = "<?php \n"; 
$p.= " //LS \n";
$p.= " define('Disable_Ads', 'No'); //Yes or No \n";
$p.= " define('Site_ID', ".$htmlinfo['domainid']."); \n";
$p.= " define('Short_Paragraph_Size',500);\n";
$p.= " define('Long_Paragraph_Size',1000);\n";
$p.= " ?> \n";

    $htmlfolder = strtolower($htmlinfo['domaingroup']);
    $htmldomain = strtolower($htmlinfo['domain']);  
    $a = fopen($htmlfolder."/".$htmldomain."/admin_config.php", 'w');
    fwrite($a, $p);
    echo $htmldomain." Completed <br />"; // TEMP - To try to see the looping of domain names
    fclose($a);
    }
}
?>

Thanks

È stato utile?

Soluzione

Replace your for loop with a while loop

while($htmlinfo = mysql_fetch_assoc($htmlresult) {
   $p = "<?php \n"; 
   $p.= " //LS \n";
   $p.= " define('Disable_Ads', 'No'); //Yes or No \n";
   //.....
   $htmlfolder = strtolower($htmlinfo['domaingroup']);
   $htmldomain = strtolower($htmlinfo['domain']);  
   //...
}

Right now you are only fetching one row (you should also be calling mysql_fetch_assoc instead of mysql_fetch_array) so you are writing the same file x row times

Also please at very least upgrade to mysqli or preferably PDO as the mysql_* extension is deprecated

Altri suggerimenti

You need to iterate over all the rows.

After you query the database, you can do:

while($row = mysql_fetch_assoc($query)) {
  $domain = $row['domain'];
  $domaingroup = $row['domaingroup'];
  // etc...
}

However, we don't recommend using mysql_* functions. Instead, use MySQLi at the very least, or PDO.

Based on the responses from both Kris and Rob, this is the corrected working code for those that may be seeking to do something similar (since I am not familiar with php nor mysql and this is just a temporary solution, others may look into what Kris and Rob suggested as far as "mysqli" and "PDO"). This for me worked perfectly. Thank you guys!

Kudos to @Kris since he replied with code example that was related to my post. He specifically used variables from my code, making it a lot easier to understand and troubleshoot; thus my point to his answer. ( I appreciate both of your input though)

<?php
$db_name = "dbname"; 
$dbusername = "dbname"; 
$dbpassword = "password"; 
$server = "dbname.blahblahbla.hosted.com";

$connection = mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = mysql_select_db($db_name,$connection)or die(mysql_error());

    $htmlquery = "select * from domains ORDER BY domain";
    $htmlresult = mysql_query($htmlquery,$connection) or die(mysql_error());
    $htmlinfo = mysql_fetch_array($htmlresult);

    if ($htmlresult == 0) { 
        echo "<p>No Recourds Found</p>";
    } else {

    while($htmlinfo = mysql_fetch_assoc($htmlresult) {

$p = "<?php \n"; 
$p.= " //LS \n";
$p.= " define('Disable_Ads', 'No'); //Yes or No \n";
$p.= " define('Site_ID', ".$htmlinfo['domainid']."); \n";
$p.= " define('Short_Paragraph_Size',500);\n";
$p.= " define('Long_Paragraph_Size',1000);\n";
$p.= " ?> \n";

    $htmlfolder = strtolower($htmlinfo['domaingroup']);
    $htmldomain = strtolower($htmlinfo['domain']);  
    $a = fopen($htmlfolder."/".$htmldomain."/admin_config.php", 'w');
    fwrite($a, $p);
    echo $htmldomain." Completed <br />"; // TEMP - To try to see the looping of domain names
    fclose($a);
    }
}
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top