在php中存储字符串并在mysql数据库中搜索该变量?

时间:2023-01-05 22:26:30

If I search for a string directly it works properly but whenever I store that string in a variable and try to search that it gives me:

如果我直接搜索一个字符串它可以正常工作但是每当我把这个字符串存储在一个变量中并试图搜索它就会给我:

bool(false)

bool(假)

here is my code

这是我的代码

<?php
 $mysqli = new mysqli("localhost", "root", "password", "database");
 $roll_no='9999-SO-12';
 echo $roll_no;
 $res = $mysqli->query("SELECT name,title FROM student_data WHERE roll_no=$roll_no");

 var_dump($res);
  ?>

But whenever I do it directly It works fine for example like this

但是无论我什么时候直接做它都可以,比如像这样

<?php
 $mysqli = new mysqli("localhost", "root", "password", "database");
 $roll_no='9999-SO-12';
 $res = $mysqli->query("SELECT name,title FROM student_data WHERE roll_no='9999-SO-12'");

 var_dump($res);
  ?>

So what am I doing wrong? What's the solution?

我做错了什么?解决方案是什么?

3 个解决方案

#1


1  

You are passing the parameter as integer though it is string. Change your query to:

您将参数作为整数传递,尽管它是字符串。改变你的查询:

$mysqli->query("SELECT name,title FROM student_data WHERE roll_no='".$roll_no."'");

#2


1  

Do some changes like this:

做一些像这样的改变:

$res = $mysqli->query("SELECT name,title FROM student_data WHERE roll_no='".$roll_no."'");

#3


1  

Please try this.

请试试这个。

 $res = $mysqli->query("SELECT name,title FROM student_data WHERE roll_no='".$roll_no."'");

#1


1  

You are passing the parameter as integer though it is string. Change your query to:

您将参数作为整数传递,尽管它是字符串。改变你的查询:

$mysqli->query("SELECT name,title FROM student_data WHERE roll_no='".$roll_no."'");

#2


1  

Do some changes like this:

做一些像这样的改变:

$res = $mysqli->query("SELECT name,title FROM student_data WHERE roll_no='".$roll_no."'");

#3


1  

Please try this.

请试试这个。

 $res = $mysqli->query("SELECT name,title FROM student_data WHERE roll_no='".$roll_no."'");