简体   繁体   中英

Getting value from database with PHP

Trying to get a single value of data from database. This code works but returns the top meta_value of the database with correct post_id . In order to get the correct value it I also need meta_key column to be equal to _shipping_phone

<?php
global $wpdb,$table_prefix;
$order_id = $order->get_id();
$shipping_phone = $wpdb->get_var("
    SELECT meta_value 
    FROM {$table_prefix}postmeta 
    WHERE post_id = {$order_id}
");
?>          
<div class="shipping-phone"><?php echo $shipping_phone; ?></div>

My try at this that doesnt work and returns nothing:

<?php
global $wpdb,$table_prefix;
$order_id = $order->get_id();
$shipping_phone = $wpdb->get_var("
    SELECT meta_value 
    FROM {$table_prefix}postmeta 
    WHERE post_id = {$order_id}
    AND meta_key = shipping_phone
");
?>              
<div class="shipping-phone"><?php echo $shipping_phone; ?></div>

SOLVED by @Laurence Cherone

<?php
    global $wpdb,$table_prefix;
    $order_id = $order->get_id();
    $shipping_phone = $wpdb->get_var( $wpdb->prepare( 'SELECT meta_value FROM '.$table_prefix.'postmeta WHERE post_id = %d AND meta_key = "_shipping_phone"', $order_id ) );
?>              
<div class="shipping-phone"><?php echo $shipping_phone; ?></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM