-2
<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); 
    }
?>
DarkBee
  • 16,592
  • 6
  • 46
  • 58

2 Answers2

2

you mixed up ! from html element onclick attribute you can't fire php function ! you should make request via ajax to server and in response send data from mysql for example : in index.html make ajax request to server.php in server.php connect to database and return data simple ajax example : https://stackoverflow.com/a/5298448/2210325

Farhad
  • 1,873
  • 17
  • 28
0

js on click cant call a php code you can use AJAX to do that as the following

<button>Click me</button>

$("button").click(function(){
    $.ajax({url: "the_php_page.php", success: function(result){
        alert(result);
    }});
});

in the the_php_page.php do the query

$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);