0

i Have This PHP and JS Code on my web page.

<?php
include 'connect1.php';
$query = "SELECT URL FROM remontee_nlf ORDER BY URL ASC";
$result = mysql_query($query) or die (mysql_error());;
$counter = 0;
// write the values from the database into the javascript array
    echo "<script type='text/javascript'>";
    echo "this.styleListArray = new Array();";
    if ($result) {
        while($row = mysql_fetch_array($result)) {
            echo("this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " .          $row['user_fname'] . "';"); // displays 'lname, fname'
            $counter += 1;
        }
    }
echo("</script>");

?>

The Problem is that when i execute the page containing the code, a part of this code doesn't get executed and it just shows on the page as a simple text :

"); echo "this.styleListArray = new Array();"; if ($result) { while($row =   mysql_fetch_array($result)) { echo("this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';"); // displays 'lname, fname' $counter += 1; } } echo(""); ?> 

I tried to figure it out, but i couldn't get it, if you can help brothers, that would be wonderful.

4

3 回答 3

0

尝试像这样重写您的代码:

include 'connect1.php';
$query = "SELECT URL FROM remontee_nlf ORDER BY URL ASC";
$result = mysql_query($query) or die (mysql_error());
$counter = 0;
// write the values from the database into the javascript array

echo <<<HTML
<script type='text/javascript'>
this.styleListArray = new Array();
HTML;

$strLine = '';

if ($result) {
    while($row = mysql_fetch_array($result)) {
        $strLine.= "this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';"; 
        $counter += 1;
    }
}

echo $strLine;

echo("</script>");
于 2013-03-08T11:03:04.327 回答
0

首先将您的查询更改为使用 mysqli 或 pdo 连接其次尝试以下代码

$sql = 'SELECT URL FROM remontee_nlf ORDER BY URL ASC';
$res = mysql_query($sql, $con);
$rows = array();
while ($row = mysql_fetch_assoc($res))
    $rows[] = $row['URL'];

$str = implode('", "', $rows);
$data = '["'.trim($str).'"]';

echo '<script type="text/javascript">';
echo "var data = $data;";
echo 'console.log(data)';
echo '</script>';

检查你的控制台日志。

在此处输入图像描述

于 2013-03-08T11:10:48.393 回答
-1

您已将$result = mysql_query($query) or die (mysql_error());;其更改为

$result = mysql_query($query) or die (mysql_error());

还要确保$row['URL']并且$row['user_fname']可用。

于 2013-03-08T10:46:45.613 回答