通过PHP文件从mySQL数据库中的记录中写入PHP文件

通过PHP文件从mySQL数据库中的记录中写入PHP文件

问题描述:

以前使用MS Access的解决方案没有完成,因此我这次尝试使用php。通过PHP文件从mySQL数据库中的记录中写入PHP文件

我有打开数据库的PHP文件,读取记录列表,并为每个记录一个HTML文件,在各自的文件名(文件夹名称在记录的字段中找到)

代码似乎工作但它不会超过第一个记录。我根本没有得到任何类型的错误信息,所以我对这个问题会是什么感到困惑。我从这里找到的很多帖子中创建了代码。我唯一想知道的是,在脚本中是否以正确的顺序打开和写入函数(或称为它们)。也许原因是完全不同的。

基本上,我想要做的是脚本为其各自的文件夹中的每个域创建一个“配置”php文件。所有配置文件之间的唯一区别是domainid字段。

dbase中的表被命名为domains。这些字段是一个唯一编号的domainid;域,其中包含域名 - 例如domain.com - 并将其用作域文件夹;和domaingroup,它被用作“类别”文件夹。

为安全起见,我更改了所有值,但db连接正常工作。

<?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); 
    } 
} 
?> 

感谢

+0

'mysql_query()'不返回行数,'mysql_fetch_array()'只返回一行。 – Barmar

+0

请不要使用'mysql_ *'函数来编写新的代码。他们不再被维护,社区已经开始[弃用流程](http://goo.gl/q0gwD)。看到[红色框](http://goo.gl/OWwr2)?相反,您应该了解[准备好的语句](http://goo.gl/orrj0)并使用[PDO](http://goo.gl/TD3xh)或[MySQLi](http://php.net/ mysqli的)。如果你不能决定哪些,[这篇文章](http://goo.gl/YXyWL)会帮助你。如果你选择PDO,[这里是很好的教程](http://goo.gl/b2ATO)。另请参阅[为什么不应该在PHP中使用mysql函数?](http://goo.gl/J5jAo) –

更换你的for循环while循环

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']); 
    //... 
} 

现在你只取一排(你也应该调用mysql_fetch_assoc,而不是mysql_fetch_array),所以你写相同的文件×行时间

也请至少升级到mysqli或最好是PDO作为mys不推荐使用ql_ *扩展名

您需要迭代所有行。

后您查询数据库,你可以这样做:

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

但是,我们不建议使用mysql_*功能。相反,至少使用MySQLi或PDO。

+0

感谢您的回复Rob W. I更新了脚本以反映您的示例,但我收到错误消息: **警告:mysql_fetch_assoc()期望参数1是资源,字符串给定... **。我将“$ query”更改为“$ htmlquery”,因为这是我的脚本中的内容,但是任何一个都会给出相同的错误消息。任何想法,因为我还缺少什么? – Luis

+0

'$ query'在我的例子中应该是'mysql_query()' –

基于Kris和Rob的回应,这是针对那些可能想要做类似事情的人修正的工作代码(因为我不熟悉php和mysql,这只是一个临时解决方案,其他人可能看看克里斯和罗布建议的“mysqli”和“PDO”)。这对我来说是完美的。感谢你们!

对@Kris的赞赏,因为他回复了与我的帖子相关的代码示例。他专门使用我的代码中的变量,使其更易于理解和排除故障;因此我指出了他的回答。 (我欣赏你的输入虽然)

<?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); 
    } 
} 
?> 
+0

的结果很高兴你得到它的工作,但一个评论,你抛出第一个结果,因为你调用$ htmlinfo = mysql_fetch_array($ htmlresult);在第12行。你应该删除这一行 – Kris