Home › Forums › Backend Issues (wp-admin) › Custom phpMyAdmin database with ACF
Hi, I have created custom database in phpMyAdmin which I will use together with ACF.
My idea is that the website user make chices with ACF and this info shows in the frontend, but I would like to use ACF to query the custom made database.
So far this is my code:
global $wpdb;
$varA = get_field('city');
$varB = $wpdb->get_var( "SELECT number FROM database WHERE town = '" .$varA. "'", 0, 1)
echo $varB;
In this case, it seems that the SQL can’t find anything since the $varA is the problem. If I echo the variable $varA I get the value from custom field (the value in this case is “Valencia”).
I have tried the SQL query with following code and it worked:
$varB = $wpdb->get_var( "SELECT number FROM database WHERE town = Valencia", 0, 1)
echo $varB;
For me it seems that the SQL don’t see the the “city” variable $varA as a text. Does someone has an idea how to solve this?
Hi @xperiap
Could you please var_dump() the $varA
variable? Could you please try the following code?
global $wpdb;
$varA = get_field('city');
var_dump($varA);
echo "\n";
$varB = $wpdb->get_var($wpdb->prepare(
"
SELECT number
FROM database
WHERE town = %s
",
$varA
));
echo $varB;
Thanks 🙂
Hi James,
Thanks for your help but still no luck with this.
I got echoed string(10) ” Valencia” in my webpage but the querry still doesn’t return the value. Tried to set varA variable to the following and them it works:
$varA="Valencia";
Do you maybe have some more ideas?
Thanks for your help!!
Hi @xperiap
That’s weird. I’ve just tested it on my end and wpdb class was working with the get_field()
result. Maybe there’s something on your site that modifies the query. Could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Sixteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.
It’s also possible that there’s something wrong with your custom table. Could you please try to get the entries from the wp_postmeta
table like this:
global $wpdb;
$varA = get_field('city');
$varB = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE meta_value = %s
",
$varA
));
print_r( $varB );
Thanks 🙂
The topic ‘Custom phpMyAdmin database with ACF’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.