Json from Database

Hi

I’ve connected my Kirby site to a database successfully, but I’m trying to select my table now and get my datas.
This was my code before on a wordpress environnement, do you know how to get them with a ‘kirby language’ ?

$ConnexionBD = mysqli_connect("db5003421274.hosting-data.io", "dbu2612872", "ttDJeg]{p3dEGg4;", "dbs2775221") ;
		if (mysqli_connect_errno()) {
			echo "Connection to the Data Base Server not Possible : " . mysqli_connect_error();
			return;
		} 
	mysqli_set_charset($ConnexionBD, 'utf8mb4');

// ........................

$Requete_01 = "SELECT * from msgforum order by id";
$Requete_02 = "SELECT * from msgforum where name =' Master' order by id";

$Resultat = mysqli_query($ConnexionBD, $Requete_01);	

	if (!$Resultat) {
		   echo("Error : Invalid Query " . mysqli_error($ConnexionBD));
  		   return;  						
		} else {
		// echo "Messages stored in database 'Night Watch'";
		mysqli_close($ConnexionBD);
	    	}
			
 if(isset($_GET['api'])){
	header('Content-Type: application/json');
	 $msg = array();
	while ($data = mysqli_fetch_assoc ($Resultat)) {

	array_push($msg, $data);
		}
		echo json_encode($msg);
		return;
 }


....TO GET THE RESULTS...


while ($data = mysqli_fetch_array ($Resultat)) {
			
	echo '<tr>';
  		echo '<td>'.$data['id'];
  		echo '<td>'.$data['name'];
		echo '<td>'.$data['msg'];
	echo '</tr>';
		} 	

Are you connecting to the database via the Kirby database class?

The database guide has all the basics if you do: Database | Kirby CMS

Yep, I’m doing like :

'db' => [
        'host'     => 'db5003421274.hosting-data.io',
        'database' => 'dbs2775221',
        'user'     => 'dbu2612872',
        'password' => 'ttDJeg]{p3dEGg4;',
    ],

Sorry, I resolved it, as usual it’s so easy with kirby.
Do I maybe remove the thread ?
Thanks
Olivier

No, we want to keep this sentence for posterity :slight_smile:

1 Like