如何从mysql

时间:2017-10-22 14:05:35

标签: javascript php

<button onlick="myFunction()">Click me</button>

<?php
   function myFunction() {
        $con = mysqli_connect("mysql.hostinger.no", "u452849516_altge", "password", "u452849516_altge"); 
        $query = "SELECT * FROM altbruker ORDER BY RAND() LIMIT 1";
        mysqli_query($con, $query);
        $results = mysqli_query($query);
        var_dump($results); 
    }
?>

2 个答案:

答案 0 :(得分:2)

你搞砸了! 从html元素onclick属性你不能解雇php函数! 你应该通过ajax向服务器发出请求,并在响应中从mysql发送数据 例如 : 在index.html中向server.php发出ajax请求 在server.php中连接数据库并返回数据 简单的ajax示例: https://stackoverflow.com/a/5298448/2210325

答案 1 :(得分:0)

js on click cant调用php代码你可以使用AJAX来执行以下操作

<button>Click me</button>

$("button").click(function(){
    $.ajax({url: "the_php_page.php", success: function(result){
        alert(result);
    }});
});
<_>在the_php_page.php中执行查询

$con = mysqli_connect("mysql.hostinger.no", "u452849516_altge", "password", "u452849516_altge"); 
        $query = "SELECT * FROM altbruker ORDER BY RAND() LIMIT 1";
        mysqli_query($con, $query);
        $results = mysqli_query($query);
        var_dump($results);