0

if i put

$sql1 = 'Select * from follow WHERE followmak = $usid';

it does not work

and whenever I put

$sql1 = 'Select * from follow WHERE followmak = 1';

1 or any cardinal number it works out. I tries to echo $usid and it works and but I wonder why it does not work in sql statement , please help me I am noob in PHP

My Full Code is given below :

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

     $sql = 'SELECT * from follow';


    $q = $pdo->query($sql);

    $q->setFetchMode(PDO::FETCH_ASSOC);
     $usid = ($row7['userID']);
     $sql1 = 'Select * from follow WHERE followmak = $usid';



     $q1 = $pdo->prepare($sql1);
    $q1->execute([$usid]);
    $q1->setFetchMode(PDO::FETCH_ASSOC);






} catch (PDOException $e) {
    die("Could not connect to the database $dbname :" . $e->getMessage());
}

?> 
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • In `$q1->execute([$usid]);` you make it look as if you intended to use a prepared statement and bind the parameter this way. This is a good approach. You should replace the parameter with a `?` in your query to use it, ie `$sql1 = 'Select * from follow WHERE followmak = ?';` – Matt Raines Jun 11 '16 at 15:12
  • Can you tell me one more thing please ? i want to create a relation between tbl_users with follow , can you please tell me the code for it ? in PDO , please – Kumar Akshay Jun 11 '16 at 15:15
  • Afraid I don't understand exactly what you are asking for. The best approach would be to write another question about it, so that other users can help you. Have a read of [How do I ask a good question](http://stackoverflow.com/help/how-to-ask) for some pointers on how to structure it. – Matt Raines Jun 11 '16 at 15:17

3 Answers3

0

Try using double quote,

$sql1 = "Select * from follow WHERE followmak = $usid";

There's a difference between using single quot (') and double quot ("). using single quot, php will interpret it as string while using double quot php will display the value of the variable into a string.

Fil
  • 8,225
  • 14
  • 59
  • 85
  • Thanks alot , it worked – Kumar Akshay Jun 11 '16 at 14:59
  • Can you tell me one more thing please ? i want to create a relation between tbl_users with follow , can you please tell me the code for it ? in PDO , please – Kumar Akshay Jun 11 '16 at 15:08
  • 1
    in mysql if you want to create many to many relation or one to many relation then just create a table named `tble_users_follow_mapping` with two columns `user_id` and `follow_id` and when you insert any new record, update users_follow_mapping table as well – Iftikhar Ali Ansari Jun 11 '16 at 15:11
  • @KumarAkshay, why did you accept a similar answer sent later than his one? How was that better than this? – Lajos Arpad Jun 11 '16 at 15:16
0

The PHP '(single quote) and "(double quote) works like this. if you put a variable in single quote it will treat it as normal string and if you put a variable in double quote it will treat it as a variable. check this link

so for your example

$sql1 = 'Select * from follow WHERE followmak = $usid'; 

$userid is not variable here instead it is normal string and your correct query should be

$sql1 = "Select * from follow WHERE followmak = $usid";
Community
  • 1
  • 1
Iftikhar Ali Ansari
  • 1,650
  • 1
  • 17
  • 27
0

Try this

$sql1 = "Select * from follow WHERE followmak = '$usid'";
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29