Very strange issue with validating info from form and DB!

Here is the exact code that keeps breaking my script. I narrowed it down. I can’t seem to figure out what is wrong. I triple checked all the syntax but THIS specific piece of code makes the browser say “can’t handle this request”. I’m thoroughly confused and after several hours of not being able to fix it, I decided to ask for help! If you need the complete script, let me know. Thanks!!

		$stmt = $db_oldtts->prepare('SELECT COUNT(*) FROM xf_user WHERE username = ?');
		$stmt->bind_param('s', $olduser);
		$stmt->execute();
		$stmt->bind_result($count);
		$stmt->fetch();
	    if($count == 0) {
	  		$name_error = "<span class='error_msg'>TTS former username not found!</span>";
	  	}

What do your error logs say?

I would recommend you use PDO.
https://phpdelusions.net/pdo

1 Like

Here is my server error log:

[15-May-2022 18:50:41] WARNING: [pool zoldos.net] child 3148239 said into stderr: "Stack trace:"
[15-May-2022 18:50:41] WARNING: [pool zoldos.net] child 3148239 said into stderr: "#0 {main}"
[15-May-2022 18:50:41] WARNING: [pool zoldos.net] child 3148239 said into stderr: "  thrown in /var/www/vhosts/zoldos.net/httpdocs/xxx.php on line 39"
[15-May-2022 18:50:46] WARNING: [pool zoldos.net] child 3148350 said into stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to a member function bind_param() on bool in /var/www/vhosts/zoldos.net/httpdocs/xxx.php.php:39"
[15-May-2022 18:50:46] WARNING: [pool zoldos.net] child 3148350 said into stderr: "Stack trace:"
[15-May-2022 18:50:46] WARNING: [pool zoldos.net] child 3148350 said into stderr: "#0 {main}"
[15-May-2022 18:50:46] WARNING: [pool zoldos.net] child 3148350 said into stderr: "  thrown in /var/www/vhosts/zoldos.net/httpdocs/xxx.php on line 39"

Okay, so I narrowed it down even further. If the one snippet is commented out, the other then works. But, also vice versa! But if both are enabled, it errors out. So there seems to be a deeper issue. Here are both scripts:

        $stmt = $db_oldtts->prepare('SELECT COUNT(*) FROM xf_user WHERE username = ?');
		$stmt->bind_param('s', $olduser);
		$stmt->execute();
		$stmt->bind_result($count);
		$stmt->fetch();
	    if($count == 0) {
	  		$name_error = "<span class='error_msg'>TTS former username not found!</span>";
	  	}

	  	$stmt = $db_oldtts->prepare('SELECT COUNT(*) FROM xf_user WHERE email = ?');
		$stmt->bind_param('s', $oldemail);
		$stmt->execute();
		$stmt->bind_result($count);
		$stmt->fetch();
	    if($count == 0) {
	  		$mail_error3 = "<span class='error_msg'>Old e-mail not found!</span>";
	  	}

Any thoughts? Thanks!

The php errors you are getting are follow-on errors, because a database statement has failed, but you don’t have any error handling for them, and the code continued to run and tried to use the result from the failed database statement.

To get error handling for all the database statements that can fail - connection, query, prepare, and execute, add the following line of code before the point where you make the database connection, you will then find database statement errors in with the php errors -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
1 Like

It means that you previously executed a prepared query (which by default leaves the data on the database server) that returned a result set, but you didn’t fetch all the data from the result set, so the database connection is tied up waiting for you to fetch all the data, and cannot be used to execute another query.

In general, if you execute a query that matches data, it’s because you want that data and will be fetching it, otherwise you would have never selected the data in the first place.

1 Like

I fixed it. After some quick research, I added this:

mysqli_stmt_close($stmt);

Bam. It worked. :smile: Thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service