Mysql database doesn't connnect

My website is sitethijsvries.epizy.com and the relevant pages are sitethijsvries.epizy.com/form.html and /verwerk.php. I’m 100% sure I used the right host, password and database name. Can someone please help me or tell me why this doesn’t work. The code is as following:

 if($correct){
    $db = new PDO('mysql:host=(got this right in my code but only allowed to put 2 links in this post)

;dbname=epiz_24665691_reacties', 'epiz_24665691', 'HIDDEN BY MOD');  

        	
 $query = "SELECT id FROM personen WHERE naam = ? AND email = ?";
        	$stmt = $db->prepare($query);
        	$stmt->execute(array( $naam, $email));	

 if ($stmt->rowCount() > 0){
        		$row = $stmt->fetch(PDO::FETCH_ASSOC);
        		$persoon_id	= $row['id'];
        	} else { 
        		$query = "INSERT INTO personen(naam, email) VALUES (?, ?)";
        		$stmt = $db->prepare($query);
        		$stmt->execute(array( $naam, $email));	
        		
 //vraagt de id van de nieuwe persoon op
        		$persoon_id = $db->lastInsertId();
        	}
        	$query = "INSERT INTO reacties(persoon_id, reactie, datum) VALUES (?, ?, ?)";
        	$stmt = $db->prepare($query);
        	$stmt->execute(array( $persoon_id, $reactie, date('Y-m-d H:i:s')));	

echo "<br /><br />Bovenstaande informatie is opgeslagen!<br />\n";
        	echo 'Zie hier de  <a href="reacties.php">reacties</a>.<br />';
        } else {
        	echo "<br /><br />Er is een foute waarde ingevoerd, <a href=\"javascript:history.back();\">ga terug</a>.<br />\n";

Wondering why did you hide your host name BUT NOT YOUR PASSWORD?

Also what’s the error message?

Also stmt->execute is Well incorrect. It should be this:
For top one:

$stmt = $db->prepare($query);
$stmt->bind_param("ss" , $naam, $email));
$stmt->execute();

And for the second query.

$stmt->bind_param("iss", $persoon_id, $reactie, date('Y-m-d H:i:s'));
$stmt->execute();

And you should learn how to execute on:

1 Like

Try to follow this article to show errors on your website, so that we will know the problem and try to fix it:

https://infinityfree.net/support/http-error-500/

2 Likes

On here:

$stmt->bind_param("ss" , $naam, $email));

you need to remove the other closed parenthesis, like this:

$stmt->bind_param("ss" , $naam, $email);
1 Like

You need to prepare the statement for that one too, with this code:

$stmt = $db->prepare($query);
$stmt->bind_param("iss", $persoon_id, $reactie, date('Y-m-d H:i:s'));
$stmt->execute();

under the already defined $query variable.

1 Like

Uh oh! Don’t share passwords (especially your hosting account password) on a public place, like this forum! Change it to something else now, because that one is now compromised.

Also, seen that the codes above work with MySQLi Object-Oriented, try to connect to the database with this code:

$servername = "your server name"; // replace with your server host
$username = "your username"; // replace with your hosting account username
$password = "your password"; // replace with your hosting account password; also note, don't share your password on a public place!
$dbname = "your database name"; // replace with your database name

$db = new mysqli($servername, $username, $password, $dbname);
if(mysqli_connect_error()) {
    echo "Error connecting to the database: ".mysqli_connect_error();
    exit;
}

following the instructions on the comments.

1 Like

Did you easily ignore my warning? You did not change your password after i said you to change it! :face_with_raised_eyebrow:

Or change it or someone will hack your Account!!!

1 Like
rowCount()?

correct is

$result = $stmt->result;
count($result)
//or 
$result->num_rows.

@goat123, maybe change rowCount() with num_rows. Also, before that, use $stmt->store_result to store the results from the prepared statement on the memory before calling that function.

@anon19508339, also, I don’t think $result->num_rows will work, as it will only work with an element that is at least a mysqli_stmt.

1 Like

($stmt->num_rows > 0) not ($stmt->num_rows. > 0)

My bad. It is what i’m using and that works well to me ¯\_(ツ)_/¯

1 Like

Also, did you forget something before the if statement on the other part of code? Here is what it goes right before the if statement:

$stmt->store_result();
1 Like

Try to use what @anon19508339 written to you, but first remove $stmt->fetch(PDO::FETCH_ASSOC).

1 Like

use $stmt->fetch_assoc();

edit: ninja’d part 3

Then try this:

$result = $stmt->get_result();
$row = $result->fetch_assoc();
1 Like

yes.

Why do not i get similar error on my host!?

edit: nvm i’m careless. do @Ergastolator1’s solution

1 Like
Fatal error : Uncaught Error: Call to a member function fetch_assoc() on bool in /home/vol15_8/epizy.com/epiz_24665691/htdocs/verwerk.php:106 Stack trace: #0 {main} thrown in /home/vol15_8/epizy.com/epiz_24665691/htdocs/verwerk.php on line 106

Line 106 is this now:
$row = $result->fetch_assoc();

Well. To continue this coding conversation. you should come our discord group if @Ergastolator1 allows?

We can help you there too.

It looks that $result is null. would you add a var_dump($result)?

then post the returned result here:

1 Like

If he has Discord, BTW. And BTW, it’s not a group, but a server.

1 Like

Telegram group confused me.

1 Like

to line 105 and 106.

is semicolon forgotten? add it near that function!

1 Like