如何编写简单的数据库查询?

时间:2022-01-20 04:31:57

I am starting to get the hang of getting data from the database, not as hard as it looks, but writing DB queries and echoing them can get a little confusing...

我开始掌握从数据库中获取数据的诀窍,虽然不像看上去的那么难,但是编写DB查询并对它们进行响应可能会有点让人困惑……

Here is how the table is setup:

以下是该表的设置方式:

如何编写简单的数据库查询?

I am trying to query the DB and get the value of meta_value WHERE meta_key = '_moon_sortable_content'

我正在查询DB并获取meta_value的值,其中meta_key = '_moon_sortable_content'

    // Get WPDB Object
    global $wpdb;

    // Table name
    $table_name = $wpdb->prefix . "postmeta";

    // My Query
    $bulls = $wpdb->get_results( "SELECT * FROM $table_name
                                        WHERE meta_key = '_moon_sortable_content'" );

Here I am trying to get the values...

我在这里试着获取价值……

    foreach($bulls as $key => $value ) {
      echo '<li>'.$item.'</li>';
    }

Here is the HTML output:

这里是HTML输出:

<li>0</li>
<li>1</li>
<li>2</li>

More Details: The value inside meta_value is pixels, there are three rows with the meta_key '_moon_sortable_content', so I am hoping to get the HTML output to be...

更多细节:meta_value内的值是像素,有三行有meta_key '_moon_sortable_content',所以我希望HTML输出是……

<li>297px</li>
<li>783px</li>
<li>this should actually be data from a textfield, so text for the result :)</li>

Update: I did var_dump on $value and it returns

更新:我在$value上做了var_dump,它返回

object(stdClass)#282 (1) { ["meta_value"]=> string(5) "Array" }
object(stdClass)#283 (1) { ["meta_value"]=> string(5) "498px" }
object(stdClass)#284 (1) { ["meta_value"]=> string(20) "154.00001525878906px" } 

How do I clean that up into a variable?

如何把它变成一个变量呢?

2 个解决方案

#1


1  

item is the key index string is the value

项是键索引字符串是值

foreach ($bull as $key=>$value)

#2


1  

SELECT meta_value FROM $table_name WHERE meta_key = '_moon_sortable_content'  

#1


1  

item is the key index string is the value

项是键索引字符串是值

foreach ($bull as $key=>$value)

#2


1  

SELECT meta_value FROM $table_name WHERE meta_key = '_moon_sortable_content'