# Help: Fatal error: Uncaught Error: Call to a member function bind\_param() on bool!

**URL:** <https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851>\
**Category:** General PHP Help\
**Created:** [June 29, 2022, 11:53pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851 "2022-06-29T23:53:07Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [June 29, 2022, 11:53pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/1 "2022-06-29T23:53:07Z")

</div>

I am having some issues with a bit code I wrote. Here is the code:

```
require "code/core.php";

$ip = $_POST['ip'];

function getIPAddress() {  
        if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  
            $ip = $_SERVER['HTTP_CLIENT_IP'];  
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
        } else {  
            $ip = $_SERVER['REMOTE_ADDR'];  
        }  
        return $ip;  
    }  
    $ip = getIPAddress();

$stmt = $db_core->prepare('SELECT COUNT(*) FROM downloads WHERE ip = ?');
$stmt->bind_param('s', $ip);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();

if($count == 0) {
    $stmt = $db_core->prepare('INSERT INTO downloads (ip) VALUES (?)');    
    $stmt->bind_param('s', $ip);
    $stmt->execute();      
} else {        
   $error = "<span class='error_msg'>You can only download once every 24 hours per IP</span>"; 
   echo $error;
   exit;
}

```

Okay, basically I am getting the user’s IP. Then checking the database to see if it already exists, and throw an error if it does. That part sort of works, except I can’t get the error message to display properly and it thinks the error var is a URL and prints it in the address bar. I have no idea why…

Then if the IP does NOT exist in the database, then insert it into the “downloads” table. But this throws the following error:

```
Fatal error: Uncaught Error: Call to a member function bind_param() on bool in 
/var/www/vhosts/xxx.net/httpdocs/xx/xx/down.php:34 Stack trace: #0 {main} thrown in 
/var/www/vhosts/xxx.net/httpdocs/xxx/down.php on line 34

```

What the heck is going on? The insert command works if I leave out the IP exists check. Is it because I have two identical “bind\_param” commands? Any help/tips would be most appreciated!!

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [June 30, 2022, 1:17am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/2 "2022-06-30T01:17:57Z")

</div>

The error that is preventing the prepare() call from working is probably due to something higher up in the code (there’s not even 34 lines in the posted code), such as a query where all the data wasn’t fetched.

You **always** need error handling for statements that can fail. For database statements that can fail - connection, query, prepare, and execute, the simplest way of adding error handling, without adding code at each statement, is to use exceptions for errors and in most cases simply let php catch and handle the exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.)

To enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -

```auto
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
```

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [June 30, 2022, 6:49am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/3 "2022-06-30T06:49:53Z")

</div>

This is the function with the error, I indicated line 34:

```
if($count == 0) {
    $stmt = $db_core->prepare('INSERT INTO downloads (ip) VALUES (?)');    
   **line 34** --> $stmt->bind_param('s', $ip);
    $stmt->execute();      
} else {        
   $error = "<span class='error_msg'>You can only download once every 24 hours per IP</span>"; 
   echo $error;
   exit;
}

```

I added the code you showed me. Now I get an error on the line above 34:

```
$stmt = $db_core->prepare('INSERT INTO downloads (ip) VALUES (?)');

```

And the new error:

```
**Fatal error** : Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now in /var/www/vhosts/xxxx.net/httpdocs/xxxx/down.php:35 Stack trace: #0 /var/www/vhosts/xxx.net/httpdocs/xxx//down.php(35): mysqli->prepare() #1 {main} thrown in **/var/www/vhosts/xxx.net/httpdocs/xx/down.php** on line **35**

```

? 🕶

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [June 30, 2022, 7:21am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/4 "2022-06-30T07:21:18Z")

</div>

Just reviewed your previous threads. This is the same issue as here - [Very strange issue with validating info from form and DB!](https://www.phphelp.com/t/very-strange-issue-with-validating-info-from-form-and-db/34758)

The line of code that enables exceptions for errors for the mysqli extension, leave that in, so that you will ALWAYS have error handling for the database statements that can fail, you will save a ton of time.

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [June 30, 2022, 8:42am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/5 "2022-06-30T08:42:47Z")

</div>

Okay, weird. I’m getting the echoed error if the IP is NOT in the DB. This should be the opposite, as the point is if the IP _does_ exist, then produce the error. Not if IP doesn’t exist in DB. Subsequent tries, if the IP already exists, it errors out, except the error is somehow converted to a URL and shown in the browser’s address bar, not echoed as usual.

What it is supposed to do is if IP does NOT exist, add it to DB table, then fetch file. If the IP exists, then produce and echo the error, then exit.

Here is the full code for reference:

```
require "code/core.php";

$ip = $_POST['ip'];

function getIPAddress() {  
        if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  
            $ip = $_SERVER['HTTP_CLIENT_IP'];  
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
        } else {  
            $ip = $_SERVER['REMOTE_ADDR'];  
        }  
        return $ip;  
 }  
 $ip = getIPAddress();

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$stmt = $db_core->prepare('SELECT COUNT(*) FROM downloads WHERE ip = ?');
$stmt->bind_param('s', $ip);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
mysqli_stmt_close($stmt);

if($count == 0) {
    $stmt = $db_core->prepare('INSERT INTO downloads (ip) VALUES (?)');
    $stmt->bind_param('s', $ip);
    $stmt->execute();
} else {        
   $error = "You can only download once every 24 hours per IP!"; 
   echo $error;
   exit;
}

$zip = new ZipArchive();
$filename = "xxx.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}

$dir = 'xxxx/';

if (is_dir($dir)){

    if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){    
            if (is_file($dir.$file)) {
                if($file != '' && $file != '.' && $file != '..'){
                    $zip->addFile($dir.$file);
                }
            }
                
        }
        closedir($dh);
    }
}

$zip->close();

echo $filename;

```

Something must be out of order. I’m stuck. Further assistance is most appreciated!

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [June 30, 2022, 10:44am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/6 "2022-06-30T10:44:30Z")

</div>

Add `var_dump($count);` right before the `if($count == 0) {` line to see what exactly is in $count, both for the initial symptom and the later symptom.

For second symptom, how exactly are you requesting this page and is there anything in core.php besides the database connection code?

Edit: also, what php version are you using, because php8 changed how a loose == string comparison with 0 works?

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [June 30, 2022, 12:52pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/7 "2022-06-30T12:52:50Z")

</div>

The var is “int(1)” **plus the error message**. Is that strange?

I have a form inside the file download.php:

```auto
<form method="post" action="down.php" id='download'>
<input type='image' src='images/cooltext414192685910710.png'>
</form>

```

If the validation checks out (their IP has NOT been used to download), then a file transfer takes place. Later I’m going to add a time limit where they can only download like once every 24-48 hours. But I want to get the base code working first. There is also some JS in that file:

```auto
<script src="code/jquery-3.2.1.min.js"></script>
      <script >
            $(document).ready(function(){
                $('#download').click(function(){
                    $.ajax({
                        url: 'down.php',
                        type: 'post',
                        success: function(response){
                            window.location = response;
                        }
                    });
                });
            });
        </script>

```

I’m using PHP 7.4.30

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [June 30, 2022, 2:43pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/8 "2022-06-30T14:43:47Z")

</div>

> [@zoldos](#):
>
> ```auto
> success: function(response){
> window.location = response;
> 
> ```

This is the reason for the address bar changing to the error message, this code is trying to “go to” wherever the server-side code is telling it.

To handle the case of an error vs the download redirect, you would output a json encoded value from the server-side code, such as error.the error message here or success.the file name here. The javascript would test the return value. If it’s the error… value, display it. If it’s the succcess… value, perform the redirect.

As to the rest of the symptoms, the type=‘image’ input is a submit button. Since the javascript is not preventing the default action, the ajax code is submitting to the server-side code and the form is submitting a second time to the server side code. In the javascript you need to prevent the default form action.

An alternative for the image would be to just output it as an \<img tag, with that id, and the click event should (untested) still trigger the javascript code.

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [June 30, 2022, 2:57pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/9 "2022-06-30T14:57:42Z")

</div>

Hmmm…this seems beyond my skills. I think I’ll just revert the code back to a simple download function. ☹

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [June 30, 2022, 3:40pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/10 "2022-06-30T15:40:04Z")

</div>

The form page would need to be -

```auto
<form method="post" action="down.php" id='download'>
<input type='image' src='images/cooltext414192685910710.png'>
</form>

<div id='response'></div>

<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
<script >
$(document).ready(function(){
	$('#download').click(function(){
		$.ajax({
			url: 'down.php',
			type: 'post',
			dataType: 'json',
			success: function(response){
				if(response.error){
					$("#response").html(response.error);
				}
				if(response.success){
					window.location = response.success;
				}
			}
		});
		// prevent default form action
		return false;
	});
});
</script>
```

At the point of echoing the error, do this -

```auto
   $error = "You can only download once every 24 hours per IP!"; 
   echo json_encode(['error'=>$error]);
   exit;
```

At the point of echoing the filename -

```auto
echo json_encode(['success'=>$filename]);
```

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [July 2, 2022, 4:39am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/13 "2022-07-02T04:39:57Z")

</div>

All I really need now is a simple CRUD to delete ALL entries from a particular column in a table. Can you assist? Thanks!

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [July 2, 2022, 1:54pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/14 "2022-07-02T13:54:34Z")

</div>

BTW, I **really** appreciate your polite and very helpful assistance! Besides the basic CRUD, is there any chance to show a “thank you” page after the user clicks the download button and has no errors (and the transfer begins)?

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [July 2, 2022, 4:28pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/15 "2022-07-02T16:28:36Z")

</div>

There are countless delete query examples to be found on the web for you to examine.

The code would -

1. Need to be secured by a login system so that only a logged in user, with administrative permissions, can see or perform the delete operation.
2. Use a post method form. A lot of examples you are likely to find are incorrectly using a link to delete data.

> [@zoldos](#):
>
> is there any chance to show a “thank you” page after the user clicks the download button

Nope. The http(s) request to the download file and the response of outputting the download file is the only thing that can occur for that request.

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [July 2, 2022, 8:43pm UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/16 "2022-07-02T20:43:02Z")

</div>

> [@phdr](#):
>
> Nope. The http(s) request to the download file and the response of outputting the download file is the only thing that can occur for that request.

Yeah I figured so, but just thought I’d ask! 😄

> [@phdr](#):
>
> There are countless delete query examples to be found on the web for you to examine.
> 
> The code would -
> 
> 1. Need to be secured by a login system so that only a logged in user, with administrative permissions, can see or perform the delete operation.
> 2. Use a post method form. A lot of examples you are likely to find are incorrectly using a link to delete data

Great, thanks! I have some working examples from other scripts on my site. I’ll check it out.

Again, I really appreciate your help!! 💯

---

<div class="post-metadata">

**Author:** ![WildFire2000](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@WildFire2000](https://www.phphelp.com/u/WildFire2000)\
**Post date:** [July 9, 2022, 3:39am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/17 "2022-07-09T03:39:24Z")

</div>

This IP checking to download a file is pointless because people who uses VPN will bypass the download limitation due the IP address being changed every time they change location on the VPN.

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [July 9, 2022, 4:03am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/18 "2022-07-09T04:03:45Z")

</div>

Yup, I realized that. I think I’ll remove that and maybe add something else like an e-mail that is saved in the DB and then compared, Not much better I suppose though.

Do you have another suggestion?

---

<div class="post-metadata">

**Author:** ![WildFire2000](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@WildFire2000](https://www.phphelp.com/u/WildFire2000)\
**Post date:** [March 1, 2023, 4:55am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/19 "2023-03-01T04:55:47Z")

</div>

Sorry for late reply, PC decided to go south on me, had to build a new system and recover all my files back.

The only thing that I can think of is to have something that verifies the Email address vs Initial IP address and saves the IP address to the user database and when the download limitation reaches, it sets a timer, just the way you have it until he/she download limitation is reached. However, if the user decides to use a VPN within that hour or (day “depends how you want it set up”) and he/she logs using their email, it will verify the IP address that it saved in the database and compare it with current IP address. Tthen it will see that the IP has changed and that user will not be able to download until the initial IP address is reset or removed from the database on a set timer.

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [March 1, 2023, 8:15am UTC](https://www.phphelp.com/t/help-fatal-error-uncaught-error-call-to-a-member-function-bind-param-on-bool/34851/20 "2023-03-01T08:15:57Z")

</div>

I actually abandoned this particular project and went back to my invite system. 😃

Thanks!
