Using mysqli to pass a constant in bind_param

Is there a way to pass a constant to the bind_param function in mysqli prepared statements? When I try to pass a variable, it works fine:

 $stmt->bind_param("i", $id);

However, when I attempt to pass a constant, it does not work. For example:

$stmt->bind_param("i", ID);

This results in the following error:

Fatal error: Cannot pass parameter 2 by reference in...

Any advice on how to resolve this issue would be greatly appreciated. Thank you.

Answer №1

When working with MySQLi, it's important to note that the arguments following the type strings are passed by reference, not by value. This means you can only pass a variable by reference, allowing you to assign and reassign variables as needed before executing statements. For example:

$id = ID;
$stmt->bind_param("i", $id);

If this limitation is causing issues for you, consider using PDO's MySQL driver instead of MySQLi. With PDO, you have the option to bind a value instead of a variable to a query parameter, making it easier to work with prepared statements that will only be executed once.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating a CSV script that truncates values to 0 in PHP

After implementing a particular script, I encountered an issue where the 01523 values in my CSV file were being written as 1523. $file = fopen('demosaved.csv', 'w'); // save the column headers fputcsv($file, array('Column 1' ...

Handling Website Downtime with PHP Simple HTML DOM Parser

I have been extracting data from a government website for health updates in Turkey. However, if the site experiences downtime or fails to load, my own website stops displaying any content after fetching and parsing the news. Is there a way to optimize th ...

Optimal approach for handling Many_to_Many relationships

Instead of using foreach or a for loop in my SQL code to fix this, I am looking for a single SQL code solution. I stumbled upon this answer on Stack Overflow, which explains how to retrieve students or courses based on a given student ID or course ID. How ...

Obtain the authenticated user's information through the use of AuthSub in Zend

Currently, I am utilizing Zend Gdata to establish connections between users and a shared Google Docs spreadsheet. This spreadsheet functions as the backend for a custom interface. My objective is to keep track of the user who most recently modified the va ...

The function does not generate an image, but it will display the one that is

It looks like there may be an issue within the function that is preventing the image from being created, or possibly in the script itself which is not passing the image to the function properly. Below is the upload script (please note: this script is inje ...

Custom code for the WordPress excerpt

Is there a way to set a universal hard-coded Excerpt in WordPress for all posts and pages? I've seen suggestions to modify the index.php and archive.php files, but I'm uncertain about the best approach. Is it possible to create a function for th ...

"Embrace the power of Ajax and JSON with no regrets

function register() { hideshow('loading', 1); //error(0); $.ajax({ type: 'POST', dataType: 'json', url: 'submit.php', data: $('#regForm').serialize(), su ...

Prevent JavaScript from being entered into the form

I'm currently developing an "HTML editor" for one of my webpages. Right now, I want to ensure that the editor only allows input of HTML and CSS elements without any Javascript (or Jquery). I've been attempting to prevent the use of <script> ...

Efficiently managing multiple database updates with PHP and JQuery

Having trouble processing multiple mySQL updates simultaneously? I have 4 select/option boxes fetching data from a db table and I want to update the database onChange using JQuery. It's working with one select module, but adding more causes issues. Th ...

When sending an AJAX request to a URL, can I verify in the PHP page whether it is a request or if the page has been accessed?

In order to enhance security measures, I need to prevent users from accessing or interacting with the php pages that will be utilized for ajax functionality. Is there a method available to determine if a page has been accessed through an ajax request or b ...

Creating a centralized controllers folder for a yii2 project

I am looking to integrate a shared controller directory into my Yii2 project. Inside my frontend/config/main.php file, I have the following setting: 'controllerNamespace' => 'frontend\controllers', My goal is to include both ...

After logging in, I am unable to redirect to another PHP page as the login form simply reloads on the same page

Lately, I've encountered an issue that has persisted for a few days. The login validation is functioning flawlessly. However, the problem arises when attempting to redirect to another page, specifically 'index.php', after logging in. Instead ...

json is failing to retrieve the correct information from the database array

I ran a query and received two rows of data, after testing it in phpadmin. However, when I checked Firebug, I could only view the information from one row. What might be causing this discrepancy? $data = mysql_fetch_assoc($r); } } head ...

Contrasting readline and fread/fgets functions in PHP

Throughout my experience, I've consistently relied on the readline function for handling console input. However, recently I stumbled upon the fread and fgets functions and it left me wondering: what sets these two methods apart? // Code example using ...

Having trouble passing a selected item from a listview to another activity using JSON in an Android application?

As a newcomer to android development, I am facing an issue with my TypeMenu Activity. In this activity, all items are fetched from the server and displayed in a ListView. I also have another class called SubMenu activity where items along with images are f ...

Encountering Issues While Setting Up Magento on WAMP Server

An error occurred while attempting to process your request Issue with file: "C:\wamp\www\magento\app\code\core\Mage\SalesRule\sql\salesrule_setup\upgrade-1.6.0.0-1.6.0.1.php" - SQLSTATE[42000]: Syntax ...

The upsert functionality in Laravel Eloquent does not properly handle UUID primary keys, causing it to incorrectly generate an insert statement

Currently, I am engaged in a project that involves the following code: Schema Schema::create('form_pages', function (Blueprint $table) { $table->uuid('id')->primary(); $table->foreignUuid('form_id') -& ...

PHP Ajax search fails to load live results

While working on my project to create an ajax live search feature, I encountered an error stating 'undefined index = q'. Below is the jQuery code I used: <script> $(document).ready(function(e){ $("#search").keyup(function(){ ...

Is it possible to delete all <br /> tags except for one on each line?

Is there a way for me to transform the following code: ...<br /> <br /> ...<br /> ...<br /> <br /> <br /> ...<br /> Using PHP to achieve this result: ...<br /> ...<br /> ...<br /> ... I ne ...

Creating a custom output using PHP, MySQL, and JSON

When I retrieve and print data from a MySQL database using the json method, it looks like this: $ret = array(); $fileName = Access::FetchDB("SELECT name FROM tags"); $ret[] = $fileName; echo json_encode($ret); The current output is: [[{"name":"test1"},{ ...