Sergi Khizanishvili Sergi Khizanishvili - 7 months ago 14
PHP Question

find in database table variable

I'm quit new in all this so can you help me?

I'm running wordpress site and I've got in my MySql database table named price_table and it has two columns: name, price

I need variable to be found in the table column name and if it exists there, return price values form that row.

Something like this:

$value = "com";
$sql = $wpdb->get_var( "SELECT * FROM $wpdb->price_table WHERE name = '$value'");

echo $sql->price;


Please help me how to do it right?

Thanks.

Answer

You can use get_row or get_results for getting records from database:

With get_row:

$sql = $wpdb->get_row( "SELECT * FROM $wpdb->price_table WHERE name = '$value'");

if($wpdb->num_rows > 0){
   echo $sql->price;
}

With get_results:

$sql = $wpdb->get_results( "SELECT * FROM $wpdb->price_table WHERE name = '$value'");
if($wpdb->num_rows > 0){
  foreach ($sql as $result) {
    echo $result->price;
  }  
}
Comments