mysql - Correct way of putting an SQL SELECT statement into a PHP variabe with another PHP variable as the WHERE
Get the solution ↓↓↓I'm trying to add in a PHP variable into a select statement. The SQL is:
SELECT Nichename
from niche
INNER JOIN broker on broker.Niche_NicheID = niche.NicheID
WHERE BrokerID= (another variable)
It works in PHPMyAdmin as a select when I enter theBrokerID
, the correctNichename
shows up. It's a PHP page inside a Wordpress site so I used this code:
$nichename = $wpdb->get_results('
SELECT Nichename
from niche
INNER JOIN broker on broker.Niche_NicheID = niche.NicheID
WHERE BrokerID
')==$broker_id;
There is no output when I put this:
<?php echo $nichename ?>
It would like theNichename
(e.g. "Travel Insurance") but I get nothing.
I've verified if I do an<?php echo $broker_id ?>
I get the correctBrokerID
so I don't know what I am doing wrong.
Answer
Solution:
I fixed it thanks to @waterloomatt for pointing me in the right direction, it was a Wordpress specific format I was looking for.
global $wpdb;
$nichename = $wpdb->get_var('
SELECT Nichename
from niche
INNER JOIN broker on broker.Niche_NicheID = niche.NicheID
WHERE BrokerID = ' . $broker_id
);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.