简体   繁体   中英

Storing a string in a variable in php and searching for that variable in mysql database?

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)

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?

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."'");

做一些这样的改变:

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

请试试这个。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM