Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据库信息从php传输到javascript-mysql_fetch_数组未按预期运行_Php_Javascript_Ajax_Arrays_Associative - Fatal编程技术网

将数据库信息从php传输到javascript-mysql_fetch_数组未按预期运行

将数据库信息从php传输到javascript-mysql_fetch_数组未按预期运行,php,javascript,ajax,arrays,associative,Php,Javascript,Ajax,Arrays,Associative,如果有人知道我的问题有一个可用的答案,我表示歉意。然而,经过一周的网上搜索和试验,我想我会问专家 目标: 通过服务器端代码(在本例中为php)查询SQL数据库,然后将此数据库信息传递给网页上的javascript,以便动态生成按钮。注意,将有多行从数据库返回。一旦按钮被生成,它们将调用php代码来更改数据库中的值,然而这是我目前正在努力解决的第一个问题 当前解决方案(不太有效): 我认识到这段代码需要优化,但由于我仍在努力解决问题,我将在稍后自己回顾这些细节,不需要详细说明 概述: PHP通过m

如果有人知道我的问题有一个可用的答案,我表示歉意。然而,经过一周的网上搜索和试验,我想我会问专家

目标: 通过服务器端代码(在本例中为php)查询SQL数据库,然后将此数据库信息传递给网页上的javascript,以便动态生成按钮。注意,将有多行从数据库返回。一旦按钮被生成,它们将调用php代码来更改数据库中的值,然而这是我目前正在努力解决的第一个问题

当前解决方案(不太有效): 我认识到这段代码需要优化,但由于我仍在努力解决问题,我将在稍后自己回顾这些细节,不需要详细说明

概述: PHP通过mysql_查询发送一个mysql查询,从数据库中获取信息。AJAX然后与json_encode一起使用,在php中对信息进行编码,然后使用echo打印出javascript的答案

PHP代码:


PHP注释: 一旦解决了这个问题,我将使用while循环遍历数据库结果的每一行,并将它们添加到$total_数组中。目前只设置了两个数组索引来突出我的问题$total_数组[0]正在存储mysql查询的答案,$total_数组[1]正在生成一个关联数组(就像我从mysql_数组中获取的一样)

Javascript代码:


编写(“好你的开始javascript”);
var btn_num=0;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=function()//从php返回结果时调用的函数
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
var response_json=json.parse(xmlhttp.responseText);
document.getElementById(“txtHint”).innerHTML=“Get Response”+Response\u json[0]['user\u name']+”“+Response\u json[1]['user\u name'];
}else if(xmlhttp.status==404){
document.getElementById(“txtHint”).innerHTML=“未找到”;
}else if(xmlhttp.readyState==0){
document.getElementById(“txtHint”).innerHTML=“Response为0”;
}否则{
document.getElementById(“txtHint”).innerHTML=“未完成响应”;
}
}
open(“POST”,“DatabasePhp/getUsers.php”,true);
setRequestHeader(“内容类型”,“应用程序/json;字符集=UTF-8”);
xmlhttp.send(空);
文件。填写(“结果是……”);
Javascript注释: 代码基本上基于使用的浏览器创建一个XMLHttpRequest类型的类,然后通过xmlhttp.open发送POST请求。响应是通过xmlhttp.onreadystatechange=function()处理的,即只有在确认答案完成后才写入答案,即当xmlhttp.readyState==4&&xmlhttp.status==200时

问题: 我无法将mysql_fetch_数组数据传递到javascript。我之所以将php关联数组放入$total_数组[1],是因为我想证明我可以传递变量。代码成功地将该数组传递到另一个数组中,我可以通过response_json[1]['user_name']在javascript中进行访问。不幸的是,无论我如何尝试从mysql_fetch_数组(设置为返回关联数组)获取关联数组,在response_json[0]['user_name']执行的当前代码中,我都没有得到答案。在我的测试过程中,当我尝试在[0]获取aa关联数组时,返回了一个混合的'null',通常当我尝试访问mysql返回的关联数组的['user_name']时,我的代码没有完成代码

所以问题是:我是否遗漏了mysql_fetch_数组(设置为通过mysql_ASSOC返回关联数组)返回的关联数组的某些内容,它的响应与手动创建的关联数组不同(在javascript中一次—在php中正常工作),我不明白为什么。有人知道我如何转换/确保mysql阵列像我的手动阵列一样工作吗?

另外:sql并没有问题,因为当我将php包含到我的网页中而不是传递给javascript时,写入网页的回音响应是“[{”user_name:“Frank”,“user_id:“1”,“join_date:“2013-05-15”},{”user_name:“bb”,“day:“5”}”。因此,它成功地从数据库中检索信息,对我来说,这看起来像是一个内部带有子关联数组的数组,这就是我所追求的,但我只能检索“bb”的用户名,无法在javascript中获得Frank。PS我意识到这两个阵列只有一个共同的用户名,但这是我目前唯一尝试访问的用户名

抱歉发了这么长的帖子,我只是想把我所有的想法都记下来

来自建议的结果: mysql_fetch_assoc–Orangepill;我试过这个,没变。从我可以在web上查看的所有文档中,我了解到它返回的结果与mysql_fetch_数组相同($result,mysql_ASSOC)

移动closeDatabase.php-Michel Feldheim;移动此选项对输出没有影响

console.log(response_json)–Orangepill;谢谢你,我不知道我可以这样复习。当我询问XMLHttpRequest时,通过控制台返回以下内容: XMLHttpRequest{statusText:“OK”,状态:200,响应:“[null,{“用户名”:“bb”,“日期”:“5”}]”,响应类型:”,响应XML:null…}。从这里我可以看到mysql_fetch_数组应答被传输为null,这是由我收到的错误“uncaught error:InvalidStateError:DOM Exception 11”引起的。我似乎不知道这是什么
    <?php //start php code
include 'DatabasePhp/openDatabase.php'; //includes my mysql_connect code to connect to the database. Works.
$query = 'SELECT `users`.`user_name`, `users`.`user_id`, `users`.`join_date`'
. ' FROM users'
. ' LIMIT 0, 30 ';
$result= mysql_query($query);
include 'DatabasePhp/closeDatabase.php';
$aa = mysql_fetch_array($result, MYSQL_ASSOC);
$total_array[0] = $aa;
$total_array[1] = array('user_name' => 'bb','day' => '5');
$formatted_variable_pass = json_encode($total_array);
echo $formatted_variable_pass;
?>
    <script type="text/javascript" language="JavaScript">
document.write("Well your starting javascript");
var btn_num = 0;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function() // the function called when a result is returned from the php
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
var responce_json = JSON.parse(xmlhttp.responseText);
    document.getElementById("txtHint").innerHTML="got responce "+responce_json[0]['user_name']+"  "+responce_json[1]['user_name'];
}else if(xmlhttp.status==404){
    document.getElementById("txtHint").innerHTML="Not found";
}else if(xmlhttp.readyState==0){
    document.getElementById("txtHint").innerHTML="Responce was 0";
}else{
    document.getElementById("txtHint").innerHTML="Not completed the responce";
}
}
xmlhttp.open("POST","DatabasePhp/getUsers.php",true);
xmlhttp.setRequestHeader("Content-type","application/json;charset=UTF-8");
xmlhttp.send(null);

document.write("and the results are .... ");

</script>