使用php生成站点地图

时间:2014-12-03 11:59:47

标签: php mysql .htaccess

虽然使用php和mysql生成站点地图我完全坚持这里的错误,我的要求是如果url大于5000,则使用站点地图索引来查找多个文件站点地图但是我遵循的代码可以在这里找到:{{ 3}}完全困扰我的错误请帮助我在这方面将感谢你我的代码在这里

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'mysite');
define('DB_USER','mysite');
define('DB_PASSWORD','mysite');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$site_baseurl = "http://localhost/mysite/";
 // Table Names
include("config.php");
$buffer = array();
    $tables = array( 'tblusers', 'tblshares', 'tblreviews');
    $custom_pre = array( 'users/', 'shares/', 'reviews/');
    $pirorities = array( '0.8', '0.9', '1.0');


    // Iterate over $tables
    foreach($tables as $table and $custom_pre as $custom_pres and $pirorities as $piroritie)
    {
        // Build Query
        $query = "SELECT `seo_url`, `added` FROM $table" .
                 "ORDER BY added DESC";

        // Get Result
        $result = mysql_query( $query );

        // Iterate over Result
        while( $row = mysql_fetch_array($result) )
        {
            // Chop up the Date
            $date = substr($row['added'],0,4) . '-' .
                    substr($row['added'],5,2) . '-' .
                    substr($row['added'],8,2);

            // Add page details to $buffer
            $buffer[] = '<url>' .
                        '<loc>' . $site_baseurl . $custom_pres . $row['seo_url'] . '</loc>' .
                        '<lastmod>' . $date . '</lastmod>' .
                        '<changefreq>daily</changefreq>' .
                        '<priority>' . $piroritie . '</priority>' .
                        '</url>';
        }
        // Free MySQLi Result
        $result->close();
    }

    // Output the Buffer to view. Make sure it looks good.
    echo implode( "\r\n", $buffer );

    // Remove the echo above and uncomment below if it looks good.

    // if( ( $xml = fopen( 'sitemap.xml', "w" ) ) !== FALSE )
    // {
    //     fwrite( $xml, implode( "\r\n", $buffer ) );
    // }


?>

1 个答案:

答案 0 :(得分:0)

您的foreach声明错误。你不能通过3组。只有一组。

以下内容应该排除你。

foreach($tables as $index => $table) {
    $custom_pres = $custom_pre[$index];
    $piroritie = $pirorities[$index];
    //.. continue.
}

您正在使用mysql,但不是以面向对象的方式,因此您无法调用close()。使用the procedural style.

mysql_close($con);

强制性说明。

请不要继续使用mysql_*函数,因为它们已被弃用和删除。请考虑使用PDOMySQLi

enter image description here