3

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?

1
  • @saty I tried your code but it was partially solving my problem. so using ' ".$roll_no." ' instead of '$roll_no' proved helpful. Further when I tried to store the variable using $_POST your solution wasn't getting the required info but the other one was.So that's why I went for that. BTW thank you for answering. Commented Apr 11, 2015 at 9:17

3 Answers 3

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."'");
Sign up to request clarification or add additional context in comments.

Comments

1

Do some changes like this:

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

Comments

1

Please try this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.