PHP and SQL: query for name using connection

long time lurker, first time poster. I am facing an issue where my only query in PHP for my mySQL database returns nothing. The query works when using mySQL database and I am assuming in my php file, but not printing anything and returning as false. Any help or tips / tricks you can give me is appreciated, I have not actively used SQL in about a year and I am trying to learn PHP as much as possible in a very limited time, so my syntax is probably not correct. I know my database is connected.

The following code works in the program below.

  $patIDQuery=("SELECT ID FROM patients WHERE usernam3='$myusername'");
  $patientID=mysqli_query($db, $patIDQuery);
  $row=mysqli_fetch_assoc($patientID);
  $user = $row['ID'];
  $_SESSION['myUserID']=$user;

      

In mySQL, this code works and I am trying to get the same result.

SELECT medications.CLINNAME FROM medications RIGHT JOIN patientData ON patientData.medID=medications.medID WHERE patientData.ID='*the actual patient id*';

      

Code with problem

<?php
        $clinNameQ=("SELECT * FROM medications RIGHT JOIN patientData ON 'patientData.medID'='medications.medID' WHERE patientData.ID='$user'");
        $clinName=mysqli_query($db,$clinNameQ);
        $rowClin=mysqli_fetch_assoc($clinName);
        $clinicalName=$rowClin['CLINNAME'];
        echo $clinicalName;
 ?>

      

I fiddled with "and" but nothing seemed to work. I am wondering if there is a backslash issue, or if it is (as it seems to me) a fetch issue.

Help is so much appreciated!

+3


source to share


2 answers


Seems to be a problem with quotes fooobar.com/questions/17323 / ...

Try



    $clinNameQ=("
         SELECT * 
         FROM medications RIGHT JOIN patientData 
              ON `patientData.medID`=`medications.medID` 
         WHERE patientData.ID='$user'
    ");

      

0


source


You can remove the quote from the query string:



$clinNameQ = "SELECT * 
   FROM medications 
   RIGHT JOIN patientData ON patientData.medID=medications.medID
   WHERE patientData.ID='$user'";

      

0


source







All Articles