Javascript 将MySQL查询作为已单击的特定表行的数组返回
我已经为此工作了大约3天,但仍然不知道如何做到这一点,即使我所有的搜索 以下是我已经完成的工作: 我在Javascript 将MySQL查询作为已单击的特定表行的数组返回,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我已经为此工作了大约3天,但仍然不知道如何做到这一点,即使我所有的搜索 以下是我已经完成的工作: 我在index.php中有一个表,它从MySQL数据库中获取数据。当用户单击表中的任何给定行时,我希望打开eventDetail.php页面 以下是我还没有弄明白的: 然后我需要eventDetail.php来运行一个MySQL查询,该查询获取上一个index.php页面上单击的表行的数据,并将其存储在一个数组中,这样我就可以在需要的地方使用eventDetail.php页面上的数据 以下是打开我的
index.php
中有一个表,它从MySQL数据库中获取数据。当用户单击表中的任何给定行时,我希望打开eventDetail.php
页面
以下是我还没有弄明白的:
然后我需要eventDetail.php
来运行一个MySQL查询,该查询获取上一个index.php
页面上单击的表行的数据,并将其存储在一个数组中,这样我就可以在需要的地方使用eventDetail.php
页面上的数据
以下是打开我的eventDetail.php
页面的index.php
页面上的代码:
index.php
<script>
$(document).ready(function onClickRow() {
$('.clickDetail').click(function(){
var str=$(this).attr("value"); /* Find out which button is clicked, stores
its value in variable 'str'*/
$(window.location = 'eventDetail.php').load('eventDetail.php?str='); /* To
collect data from database */
})
})
</script>
<?php
$db_host = '127.0.0.1'; // Server Name
$db_user = 'root'; // Username
$db_pass = 'password'; // Password
$db_name = 'mydb'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$str=$_GET['str']; // collect the row id
$queryRow = ("SELECT id, eventName, description FROM mytable WHERE
id=:id");
我需要将上一页单击的行中的
id、eventName、description
返回到一个数组中,以便我可以实际使用该页上的数据。多亏了@BobRodes,我终于解决了这个问题
这是您在index.php
中需要的脚本:
<script>
$(document).ready(function onClickRow() {
$('.clickDetail').click(function(){
var str=$(this).attr('value'); // Find out which button is clicked, stores its value in variable 'str'
$(window.location = 'eventDetail.php?str='+str).load('eventDetail.php?str='+'/'+str); // To collect data from database
})
})
</script>
<?php
$db_host = '127.0.0.1'; // Server Name
$db_user = 'root'; // Username
$db_pass = 'password'; // Password
$db_name = 'mydb'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$str = $_GET['str']; // collect the row id
$queryRow = ("SELECT id, eventName, description FROM mytable WHERE id='$str'"); // SQL Statement
$result = mysqli_query($conn, $queryRow); // Execute SQL Query
$items = array(); // Create an array and store in a variable called $items
// Statement below fetches the row that was clicked and stores the data in the array $items
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$items[] = $row;
}
}
// Statement below separates each item in the array and converts it to a string and stores in variable named $item
foreach ($items as $item) {
echo $item['id'];
echo $item['eventName'];
echo $item['description'];
}
mysqli_close($conn);
?>
这将打开
eventDetail.php
,并返回在url中单击的数据库中的表行。然后,您可以编辑页面其余部分的代码,从该行返回您在页面上喜欢的数据。一方面,您混合了不同的mysql API其中id=:id
是PDO语法(占位符方法),可能是它不适用于您的原因。您尚未将字符串值传递到URL:$(window.location='eventDetail.php').load('eventDetail.php?str='+str)
然后在查询中,您需要在WHERE
子句中插入id
值的字符串:WHERE id='“$str.”“
@BobRodes谢谢这使我的SQL语句起作用。现在我要创建数组:$items=array();$result=mysqli\u查询($conn,$queryRow);而(($row=mysqli_fetch_assoc($result)){$items[$row['id']]=$row['eventName'];}echo$result;
但它引发以下错误:可恢复的致命错误:类mysqli_result的对象无法转换为string@FunkFortyNiner不知道为什么我没有立即回复$items[$row['id']=$row['eventName'];试试$items[]=$row;
。