0

How can I concatenate two columns in where clause

this is my normal query:

$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());

I want the stud_fname and stud_lname to be concatenated.

here is my full code:

<?php   
error_reporting(0);
$link = mysql_pconnect("localhost", "root", "") or die("Could not connect");
mysql_select_db("dbmobile_class_record") or die("Could not select database");

// array for JSON response
$response = array();

$instructor_id=$_GET['instructor_id'];
$description=$_GET['description'];
$stud_fname=$_GET['stud_fname'];
$stud_lname=$_GET['stud_lname'];
// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());

if (mysql_num_rows($result) > 0) {

    $response["student"] = array();

    while ($row = mysql_fetch_array($result)) {
            // temp user array
            $student = array();
            $student["stud_id"] = $row["stud_id"];

            // push ordered items into response array
            array_push($response["student"], $student);
           }
      // success
     $response["success"] = 1;
}
else {
    // order is empty
      $response["success"] = 0;
      $response["message"] = "No Records Found";
}
// echoing JSON response
echo json_encode($response);

?>
3
  • Can you explain more? Do you mean the variables like $concat = $stud_fname.$stud_lname? To do what? Commented Jan 17, 2016 at 11:44
  • It's quite unclear what are you trying to achieve but if you want to get concated stud_fname and stud_lname in $result you need to use CONCAT eg SELECT stud_id, CONCAT_WS(' ', stud_fname, stud_lname) as stud_fullname FROM ... dev.mysql.com/doc/refman/5.7/en/… Commented Jan 17, 2016 at 11:48
  • concatinate fname(John) and lname(Cruz) as a fullname and the result in the url is like this -> localhost/MobileClassRecord/getID.php?instructor_id=INST-20131296&description=ADBMS&fullname=John Cruz Commented Jan 17, 2016 at 11:50

2 Answers 2

2
$instructor_id = $_GET['instructor_id'];
$description = $_GET['description'];
$fullname = $_GET['fullname'];

// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND CONCAT(stud_fname, ' ', stud_lname) = '$fullname'") or die(mysql_error());
Sign up to request clarification or add additional context in comments.

Comments

0

Use this one

$result = mysql_query("SELECT stud_id, CONCAT_WS('', stud_fname, stud_lname) AS stud_fullname FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());

stud_fullname should contain what you want.

Comments

Your Answer

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