0

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

Luis
  • 15
  • 1
  • 6
  • `mysql_query()` doesn't return the number of rows, and `mysql_fetch_array()` only returns one row. – Barmar Jul 03 '13 at 23:46
  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/q0gwD). See the [red box](http://goo.gl/OWwr2)? Instead you should learn about [prepared statements](http://goo.gl/orrj0) and use either [PDO](http://goo.gl/TD3xh) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/YXyWL) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/b2ATO). Also see [Why shouldn't I use mysql functions in PHP?](http://goo.gl/J5jAo) – Dave Chen Jul 03 '13 at 23:49

3 Answers3

0

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

Kris
  • 6,094
  • 2
  • 31
  • 46
0

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.

Rob W
  • 9,134
  • 1
  • 30
  • 50
  • Thank you for your response Rob W. I updated the script to reflect your example but I am getting the error: **Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given ...**. I changed your "$query" to "$htmlquery" since that is what I have in my script, but either one gives the same error message. any ideas as that what else I am missing? – Luis Jul 04 '13 at 00:00
  • `$query` in my example should be the result of `mysql_query()` – Rob W Jul 04 '13 at 00:01
0

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);
    }
}
?>
Luis
  • 15
  • 1
  • 6
  • Glad you got it working but one comment, you are throwing aaway the first result because you are calling $htmlinfo = mysql_fetch_array($htmlresult); on line 12. You should remove this line – Kris Jul 04 '13 at 01:55