dqx13503925528 2015-10-21 01:17
浏览 12
已采纳

动态MySQLi语句

Forgive me as this is probably a 101 question but I'm having no luck wrapping my head around it.

I'm working with a mysqli->prepare statement and

"Select * FROM someTable WHERE someTable.column = 1" - Works beautifully

We can make it dynamic by passing the variable with the use of a ? and a bind param. "Select * FROM someTable WHERE someTable.column = ?" - Works beautifully

My desire was to build a statement that let me pass "someTable.column = 1" dynamically

"Select * FROM someTable WHERE ?"

I figured the method was a total bust but tried testing it by passing on the variable "1 = 1" and it worked?!

So if I can pass "1=1", then why couldn't I pass "someTable.column = 1"

I've done this with MySQL in the past by building the statement and inserting it all at one time. I was just hoping there was another way.

  • 写回答

1条回答 默认 最新

  • dtpyvb1873 2015-10-21 01:36
    关注

    Forgive me as my reputation is not enough to post a comment, but why not just using string concat in your case?

    Such as $query="Select * FROM someTable WHERE 1=1 "; and then

    if($additionalInfo==true){ $query.= "AND someTable.column=1 "; }
    

    Please inform me if I misunderstand your question, and I will remove this possible misleading post.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!