drol55885602 2015-11-07 06:39
浏览 65
已采纳

将数据从mysqli传递给javascript

I want to pass the results of a query to javascript (I want to make an interactive quiz about fish species). This seems to be easy with json_encode, but the structure of my php array makes it difficult to access? Even console.log does not work, so there is probably an error, though I have been checking this code more than i would like to admit. I am pretty sure the error is in the small javascript part though. The code (this is all in a .php file):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<?php
$con = new mysqli("localhost", "root", "", "marinefish");
$query = "SELECT Commonname, Scientificname FROM allseafish";
$result = $con->query($query);
while ($row = $result->fetch_array()){
       $rows[] = $row;
}

/*
Structure rows:  
array(544) {
  [0]=>
  array(4) {
    [0]=>
    string(7) "Allfish"
    ["Commonname"]=>
    string(7) "Allfish"
    [1]=>
    string(11) "Omni pisces"
    ["Scientificname"]=>
    string(11) "Omni pisces"
  }
*/

mysqli_free_result($result);
mysqli_close($con);
?>

<title>Untitled</title>
</head>
<body>

<script type="text/javascript">
var ars = <?php echo json_encode($rows) ?>;
console.log(ars);
alert(ars[0][0]);
</script>
</body>
</html>

Any suggestions, or easy-to-understand links/ tutorials are very welcome! I tried: http://www.dyn-web.com/tutorials/php-js/json/multidim-arrays.php which seems very clearly explained, but couldn't help me. I also have tried to simplify the structure of the php array, but without success. And I tried to change the echo json_encode($rows) into print(json_encode($rows)). As you may have noticed, I am new with this, so all basic tips will be a great help. Thank you!

  • 写回答

2条回答 默认 最新

  • dr897777 2015-11-07 06:50
    关注

    A better solution would be to separate your javascript from PHP through post/get requests by creating a new PHP page (e.g. Query.php) that is specific for Database response:

    ... MYSQL CODE ...
    echo json_encode($rows); // Echo everything into body
    

    And Using jQuery (or equivalent Ajax request):

    $.ajax({
      type: "GET",
      url: 'query.php',
      dataType: "json",
      success: function(your_php_reponse_as_json){
          console.log(your_php_reponse_as_json);
      }
    });
    

    Note that this code will be essentially asynchronous as the request for the MYSQL Query is sent in another page using async JavaScript. Additionally you can customize your query reponse with $_POST & $_GET:

    PHP Code:

    $get1 = $_GET['js_sent'];
    // Alternatively
    $post1 = $_POST['js_sent'];
    

    Edit: You'll need to serialized data before sending. For the complete jQuery solution refer here jQuery Ajax POST example with PHP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作