Vahid
Vahid

Reputation: 3442

MySQL/PHP: How can we bind a parameter to both (?)s in prepared statements?

If this is my code:

    $mysqli = new mysqli("localhost", "user", "password", "mydb");

    $city = "Some";

    $q = "SELECT District FROM City WHERE (Name=? OR ? IS NULL)";

    if ($stmt = $mysqli->prepare($q)) {

        // How to Bind $city here?
    }

How can I bind $city to both ?s?

Or is there any better way to do this?

Upvotes: 3

Views: 526

Answers (1)

NullPoiиteя
NullPoiиteя

Reputation: 57322

you can do this by

$stmt -> bind_param("for first ?", 'for second ?');

or try like

   /* Bind our params */
    $stmt->bind_param('ss', $Name, $city);

    /* Set our params */
    $Name= "hello";
    $city= "city";

    /* Execute the prepared Statement */
    $stmt->execute();

The values can be:

i - Integer
d - Decimal
s - String
b - Blob (sent in packets)

Upvotes: 4

Related Questions