WebsiteBaker Community Forum

WebsiteBaker Support (2.12.x) => General Help & Support => Topic started by: sixis on March 19, 2020, 09:32:57 PM

Title: Database access
Post by: sixis on March 19, 2020, 09:32:57 PM
After updating to 2.12.2 i cant access the database in the code section as before (2.8.1)

Maybe someone has in idea whats wrong with my code.

mysqli_query ("UPDATE tablename SET column ='$xxxxxxx' WHERE ID=1") or die ("MySQL-Fehler: " . mysqli_error());

old code that worked:

mysql_query ("UPDATE tablename SET column ='$xxxxxxx' WHERE ID=1") or die ("MySQL-Fehler: " . mysql_error());
Title: Re: Database access
Post by: Ruud on March 19, 2020, 10:31:50 PM
WB2.8.1 used MySQL, where the current WB versions (and PHP) use MySQLi.
mysqli_query() needs the handle that was created when the connection to the database was made.

WB has the $database class you can use. The correct way would be:
Code: [Select]
<?php 
$database
->query("YOUR QUERY");
// if you want to crash on errors (not recommended)
if($database->is_error()) die("Error: ".$database->get_error();

Title: Re: Database access
Post by: sixis on March 20, 2020, 09:37:55 AM
You are my hero  :-D
Thank you

Updating works nice :-)

I realize i have to learn a lot e.g. how to readout the table

$database->query("SELECT * FROM wb_users WHERE username = test");
while ($data = mysqli_fetch_assoc($database))
{$mailadress=$data[email];
echo $mailadress;
}
Title: Re: Database access
Post by: dbs on March 20, 2020, 10:01:46 AM
You should have a look into core modules (like news, form, code) to learn.
E.g. to read a single result you can use
Code: [Select]
$database->get_one('SELECT ...
Title: Re: Database access
Post by: sixis on March 20, 2020, 10:56:23 AM
yeaaaa

from the code module section i succeeded whith this:

//Auslesen
$query = "SELECT * FROM tablename WHERE ID = 1";
$get_content = $database->query($query);

$content = $get_content->fetchRow(MYSQLI_ASSOC);
$content = ($content['email']);  // \htmlspecialchars

echo $content;


THANK YOU ALL