Looking up data in PHP from a set number of rows

I manage a patient table containing over 1000 patient records, each created by one of the 5 admins. Patient records are sorted using the admin_id column, so that when admin "A" logs in, they can only see patients they have created. Now, I want to search for a specific patient from the table, with the search results limited to only those patients created by admin "A".

MySQL query:

SELECT 
    *
FROM
    patient
WHERE
    admin_id = 36 AND fname LIKE '%test%'
        OR email LIKE '%test%'
        OR mobile LIKE '%test%'
ORDER BY ID DESC 

(Despite receiving search results from all records, I am specifically looking for results where admin_id = 36.)

Answer №1

Ensure to add parentheses () around the OR conditions to make sure that the expression is only true when admin_id is 36 and all other conditions are also met.

SELECT 
    *
FROM
    patient
WHERE
    admin_id = 36 AND (fname LIKE '%test%'
        OR email LIKE '%test%'
        OR mobile LIKE '%test%')
ORDER BY ID DESC 

Answer №2

Here is a suggestion for your query:

SELECT 
    *
FROM
    patient
WHERE
    (admin_id = 36 AND fname LIKE '%test%')
        OR (email LIKE '%test%')
        OR (mobile LIKE '%test%')
ORDER BY ID DESC 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize AJAX and jQuery to seamlessly upload files through the PHP API

I have code in the following format. PHP file : <form action="http://clientwebapi.com/createEvent" id="form_createEvent" method="post" enctype="multipart/form-data"> <input type="text" name="image_title" /> <input type="file" name="media" ...

Displaying Alert with PHP Query

I have recently set up my Drupal website and I am currently in the process of integrating an API from a control panel. One of the requirements of the API is to define a logout link mywebsite.com/logout that informs the user about their successful logout. ...

Engaging with a JSON data structure

I have experience using ajax for writing to sessions and performing inserts, but now I need to receive a response. While I've managed to get it working somewhat by going through examples, I'm currently stuck at a certain point. I am in search of ...

Creating new accounts with SQL in HTML code

I am working on a query to input any information entered into a form into the database. I have written some code, but I believe there is an issue with the query itself as I am unsure how to include variables in a SQL query. If anyone could assist me with t ...

Encountering issues when trying to connect Android to MySQL using PHP and establish a connection

I have been working on an app that displays MySQL table data in a card layout within an Android app. Here is the connection code snippet from my MainActivity.java: private void load_data_from_server(final int id) { AsyncTask<Integer,Void,Void> ...

Laravel Tutorial: Passing a Specific Shop ID in a Foreach Loop to a Controller

I am trying to extract the specific shop id when the button is clicked. I have a foreach loop that displays the name of each shop and its products. What I'm aiming for is to pass the $shop->id value to the controller after clicking the x button. Once ...

What is the best way to understand and interpret the decoded JSON array data?

I'm finding it challenging to interpret this JSON decoded array. It's more complex than what I usually work with, so any assistance would be greatly appreciated. Thank you in advance. array(1){ [0]=> object(stdClass)#1 (11){ ["id ...

Transferring a variable value between functions using autocomplete and AJAX communication

I am facing difficulties with implementing autocomplete jQuery along with AJAX call. The issue arises when a user enters text in the input field, triggering an AJAX POST request to the controller which retrieves values from the database and sends them back ...

Utilizing PHP to send arrays through AJAX and JSON

-I am facing a challenge with an array in my PHP file that contains nested arrays. I am trying to pass an integer variable (and may have to handle something different later on). -My objective is to make the PHP file return an array based on the incoming v ...

Converting Phone Numbers, Fax Numbers, and Zip Codes for Input into a Database

I'm facing an issue with masked inputs for Phone, Fax, and Zip Code on my webpage. The Phone and Fax numbers are currently formatted as (999) 999-9999, while the Zip Code is in the format 99999-9999. However, when I submit the page, the database requi ...

Difficulties encountered while implementing session management and validation in CodeIgniter using PHP

Currently I am working with the CodeIgniter Framework. Utilizing the session library has been smooth sailing, fetching data like so $this->session->usserdata('IdUsuario') without any issues. However, things take a turn when implementing fo ...

Use regular expressions to extract information enclosed within quotation marks

Here is the string we have: feature name="osp" We want to extract specific parts of this string and create a new string. The word "feature" may vary, as well as the content inside the quotes, so our solution needs to be flexible enough to capture any var ...

Learn how to subtract time in PHP while accounting for AM/PM

I need assistance with time formatting. Currently, I have: $start_time; (e.g. 1:30 PM) $system_time; (e.g. 8:20 AM) echo $time_left_for_discussion = $start_time - $system_time; When trying to subtract $start_time - $system_time, the result is not ...

There has been no change in Vue2 compatibility with Laravel 5.4

I'm currently experimenting with Vue within a Laravel project. However, I've encountered an issue where after modifying the example.vue file and adding it to the view, the changes are not reflected in the view. The code snippet below showcases th ...

What is your preferred approach to arranging files that are being echoed from a specific directory?

I have implemented a code that displays files in a directory if they are songs. However, these songs appear in a list without any specific order. I would like to organize them based on their file names. Interestingly, when I check the same files in FileZil ...

Encountering PHP encoding problems while utilizing http_request2

I've been attempting to make a request to a website using the HTTP_REQUEST2 package. My try catch statement is as follows: try { return $this->_HttpObject->send();//->getBody(); } catch (Exception $exc) { echo $exc-> ...

Creating global variables in PHP using programming techniques

Getting straight to the point - I'm curious if it's possible to dynamically declare global variables within a function in PHP. To clarify, could you define globals from an array of strings containing variable names? ...

Using regular expressions in PHP to capture text located between two specified strings

If we have a string like this: <img src="/a.jpg">, how can we remove the /a.jpg part? This pattern is not correct for extraction: #<img src="(.*)[^"]"># ...

PHP is not receiving data from the JQuery Form Plugin

I am currently utilizing the JQuery Form Plugin as an AJAX file uploader. The HTML form in question is being generated dynamically, and it has the following structure: <form id="formUpload" action="fileReceiver.php" method="post" enctype="multipart/fo ...

I'm puzzled as to why the hidden input field consistently returns the same value every time

As a beginner in php, I am facing an issue where I cannot trace the correct value of the hidden input which represents the ID of the image in the products table. Every time I try to delete an image, it always returns the ID of the last image in the product ...