58977

inner join wont work with mysqli prepared statement in php

Question:

I can't seem to get this statement or statements alike to work with prepared queries, the code works just fine below:

$DBH = getDBH(); $stmt = $DBH->prepare("SELECT a.id, a.title, a.photo FROM tag t INNER JOIN tag_reference atx ON t.tag_id = atx.tag_id INNER JOIN articles a ON atx.article_id = a.id WHERE t.tag_name = 'example'"); $stmt->execute(); $stmt->bind_result($id,$title,$photo); $stmt->fetch();

but when I change t.tag_name = '?' it gives me an error that the amount of parameters do not match. This is the statement that does not work.

$DBH = getDBH(); $stmt = $DBH->prepare("SELECT a.id, a.title, a.photo FROM tag t INNER JOIN tag_reference atx ON t.tag_id = atx.tag_id INNER JOIN articles a ON atx.article_id = a.id WHERE t.tag_name = '?'"); $stmt->bind_param('s',$example); $stmt->execute(); $stmt->bind_result($id,$title,$photo); $stmt->fetch();

Can anyone please help?

Answer1:

The placeholder ? does not work if enclosed in single quotes. In this case the SQL tokenizer will catch it as literal string.

Change it to:

WHERE t.tag_name = ? ");

Answer2:

When using placeholders, do you need to use quotes? Most placeholder languages I've used don't.

WHERE t.tag_name = ?"

Recommend

  • Creating a filterable list using Laravel and Eloquent queries [closed]
  • Overwriting/Adding an ActiveRecord association dynamically using a singleton class
  • PHP PDO Update prepared statement problem
  • Valums Ajax file Upload handle the up. file?
  • Argument 5: cannot convert from 'System.Drawing.Image' to 'string' - calling cla
  • Inserting NULL/empty string using libpqxx library
  • Save image as is in photo album using swift
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • IE6 changes DOCTYPE to a bad one
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • JSON encode and decode on PHP
  • NHibernate manually control fetching
  • Android device acting as an accessory
  • Git describe fails to return most recent annotated tag
  • NSScanner Loop Question
  • How to render a blob on a canvas element?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Ajax Loaded meta Tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Get one-time binding to work for ng-if
  • ilmerge with a PFX file
  • Display issues when we change from one jquery mobile page to another in firefox
  • Volley JsonObjectRequest send headers in GET Request
  • Importing jscolor library in angular 2
  • To display the title for the current loaction in map in iphone
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Delete MySQLi record without showing the id in the URL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • How do you join a server to an Active Directory (domain)?
  • How to stop GridView from loading again when I press back button?
  • How does Linux kernel interrupt the application?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?