gpt4 book ai didi

php - 在 PHP 和 Javascript 二维数组之间传输数据库数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:39 25 4
gpt4 key购买 nike

我正在尝试使用 PHP 从 MySQL 数据库获取所有数据,初始化一个 2D java 数组并用 PHP 数据填充它。

我在 PHP 中嵌入 JS 时遇到问题。我已经标记了哪些有效,哪些无效。

正如您将看到的,一些嵌入式 java 可以工作,但不是全部。

有什么想法吗?

<body>

<?php
$con = mysql_connect("XXXXXX.COM","guest","password");
mysql_select_db("HHG", $con);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
$result = mysql_query("SELECT * FROM articles", $con);
$numrows = mysql_num_rows($result);
echo "DB connection OK <br/>";
echo "Found ";
echo $numrows;
echo " records <br/><br/>";
} // EVERYTHING WORKS UP TO HERE
?>


<script type="text/javascript">

document.write("THIS IS THE FISRT JS DOING SOMETHING"); // THIS DOES NOTHING
numrows = <?php echo $numrows; ?>; // THIS DOES NOTHING
string [][] hhgdata = new string[numrows][4]; // THIS DOES NOTHING
document.write("Records = " + numrows + "<br/>"); // THIS DOES NOTHING

</script>

<?
$counter = 1;
while ($row = mysql_fetch_assoc($result))
{
echo $row["idimg"]; echo "<br/>"; //THIS WORKS
$hhgtitle = $row["hhgtitle"]; //THIS WORKS
echo $hhgtitle; echo "<br/>"; //THIS WORKS

?>

<script type="text/javascript"> //THIS WORKS
counter = <?php echo $counter; ?>; //THIS WORKS
document.write("counter = " + counter + "<br/><br/>"); //THIS WORKS

hhgtitle = <?php echo $hhgtitle; ?>; // THIS DOES NOTHING
document.write("Title: "); // THIS DOES NOTHING
hhgdata[counter][1]= hhgtitle; // THIS DOES NOTHING
document.write(hhgdata[counter][1]); // THIS DOES NOTHING
</script>

<?
$counter++; // THIS WORKS
}
?>

</body>

最佳答案

您混淆了 Java 和 JavaScript。例如,这是 Java 语法,您不能在只应包含 JavaScript 的脚本标记中编写:

string [][] hhgdata = new string[numrows][4];

JavaScript 数组是动态的,这应该足够了:

var hhgdata = [];

当您想向其中添加另一个数组时,就像您稍后在代码中所做的那样,只需执行以下操作:

hhgdata[counter] = [];

然后赋值给内部数组:

hhgdata[counter][1] = hhgtitle;

您还创建了多个将未加引号的字符串文字分配给变量的方法(假设 $hhgtitle 包含一个字符串):

hhgtitle = <?php echo $hhgtitle; ?>;

应该是这样的:

hhgtitle = <?php echo '"' . $hhgtitle .'"'; ?>;

最后,虽然没有错,但您的 PHP while 循环在您的 HTML 中创建了多个 script 元素。

编辑

我已经进行了上述更改以及评论中的更改,请准确复制粘贴并查看结果如何:

<body>

<?php
$con = mysql_connect("XXXXXX.COM","guest","password");
mysql_select_db("HHG", $con);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
$result = mysql_query("SELECT * FROM articles", $con);
$numrows = mysql_num_rows($result);
echo "DB connection OK <br/>";
echo "Found ";
echo $numrows;
echo " records <br/><br/>";
} // EVERYTHING WORKS UP TO HERE
?>

<script type="text/javascript">
document.write("THIS IS THE FISRT JS DOING SOMETHING"); // THIS DOES NOTHING
numrows = <?php echo $numrows; ?>; // THIS DOES NOTHING
var hhgdata = new Array(numrows); // THIS DOES NOTHING
document.write("Records = " + numrows + "<br/>"); // THIS DOES NOTHING
</script>

<?php
$counter = 1;
while ($row = mysql_fetch_assoc($result))
{
echo $row["idimg"]; echo "<br/>"; //THIS WORKS
$hhgtitle = $row["hhgtitle"]; //THIS WORKS
echo $hhgtitle; echo "<br/>"; //THIS WORKS
?>
<script type="text/javascript"> //THIS WORKS
var counter = <?php echo $counter; ?>; //THIS WORKS
document.write("counter = " + counter); //THIS WORKS
hhgtitle = <?php echo '"' . $hhgtitle . '"'; ?>; // THIS DOES NOTHING
document.write("Title: "); // THIS DOES NOTHING
hhgdata[counter] = [];
hhgdata[counter][1]= hhgtitle; // THIS DOES NOTHING
document.write("<br />hhgdata[counter][1]: " + hhgdata[counter][1]); // THIS DOES NOTHING
</script>
<?php
$counter++; // THIS WORKS
}
?>

</body>

关于php - 在 PHP 和 Javascript 二维数组之间传输数据库数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8873370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com