So analysieren Sie Zeilenergebnisse aus $ wpdb -> get_results
4 Antworten
- Stimmen
-
- 2011-07-19
foreach( $wpdb->get_results("SELECT * FROM your_table_name WHERE id LIKE' . $id . ';") as $key => $row) { // each column in your row will be accessible like this $my_column = $row->column_name;}
Weitere Informationen hier
foreach( $wpdb->get_results("SELECT * FROM your_table_name WHERE id LIKE' . $id . ';") as $key => $row) { // each column in your row will be accessible like this $my_column = $row->column_name;}
More info here
-
Ichbin mirnicht sicher,ob dies der richtige Wegist.Ich denke,man sollte das Ergebnisin eine Variable umwandeln undforeach verwenden,um sicher zugehen.Z.B.$ results=$ wpdb->get_results ($ sql);und verwenden Sie dannforeach ($ results als $ value).not sure if this is the right way though. I think one should get the result to a variable and use foreach on that, to be safe. E.g. $results = $wpdb->get_results($sql); and then use foreach($results as $value).
- 2
- 2013-09-13
- Gogol
-
solltein diesem Fallnicht wirklich wichtig sein,daes Array,Objekt oder Null zurückgibt,solltees kein Risikogeben,eine "schleifenunfreundliche" Ressource zuerhalten.Das heißt,Siemöchten sie vielleichtnocheinmalfüretwas anderes durchlaufen und wennja,aufjeden Fall speichern.nicht zweimal abfragenshouldn't really matter in this instance since it returns array, object or null, there shouldn't be any risk of getting a "loop unfriendly" resource. that said you may want to loop through them again for something else, and if so definitely store it. don't query twice
- 0
- 2017-10-29
- Garet Claborn
-
- 2011-07-19
Probieren Sieimmer den WordPress-Codex aus: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
Bei der Standardsyntaxist die Variable $ row hierim Wesentlichenein Objekt,das Ihre Ergebnisseenthält.Sie können alternativ den TYP des Ergebnisses angeben (numerisches Array,assoziatives Array).
Angenommen,nurein Ergebnis,dann sollten $ row->id und $ row->name die Informationen liefern.
Wenn Siemehr alsein Ergebnis zurückerhalten,möchten Sie die Einträgeim Objekt durchlaufen.
Wenn Sienureine Zeile zurückerwarten,verwenden Sie $ wpdb->get_row http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Row
Always Try the WordPress Codex: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
Essentially given the default syntax, the variable $row here is an object containing your results. You could alternately specify the TYPE of result (numeric array, associative array).
Assuming just one result, then $row->id and $row->name should give you the information.
If you get back more than one result, you'd want to loop over the entries in the object.
If you are expecting just one row back, then try using $wpdb->get_row http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Row
-
- 2017-10-30
So verwenden Siees als assoziatives Array:
$obj=[]; $rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_A); foreach($rows as $row){ $obj=$row; break; } // $obj is now the selected row if a match was found
Verwendung
$something = $obj['column_name']; foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL . '<br />';
Um andere Formate zuerhalten,ändern Sieeinfach
ARRAY_A
basierend auf dem Dokumentationfür$wpdb->get_results()
. Pippins Antwortistfür diemeisten Objektegeeignet.So verwenden Sieeine Zeile alsnumerischindiziertes Array
$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_N); foreach($rows as $row){ $obj=$row; break; } //Usage foreach($obj as $col_value) echo $col_value . ' ';
So verwenden Sieeine Zeilein einem Array,dessen Schlüssel der Primärschlüssel Ihrer Datenbank sind (häufigeine
id
-Spalte). Möglicherweiseeffizienter als die assoziative Array-Methode.$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , OBJECT_K); $obj = $rows[ $obj_id ]; //Usage $something = $obj->column_name; //Remember you can loop over objects too foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL;
To use as an associative array:
$obj=[]; $rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_A); foreach($rows as $row){ $obj=$row; break; } // $obj is now the selected row if a match was found
Usage
$something = $obj['column_name']; foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL . '<br />';
To get other formats, simply change
ARRAY_A
based on the documentation for$wpdb->get_results()
. Pippin's answer is appropriate for most object use.To use one row as an numerically indexed array
$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_N); foreach($rows as $row){ $obj=$row; break; } //Usage foreach($obj as $col_value) echo $col_value . ' ';
To use one row in an array whose keys are the primary key from your database(often an
id
column). Possibly more efficient than the associative array method.$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , OBJECT_K); $obj = $rows[ $obj_id ]; //Usage $something = $obj->column_name; //Remember you can loop over objects too foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL;
-
- 2018-03-15
Dieser Codefunktioniertperfektfürmich:
global $wpdb; $table_name = "my_table_name"; $myrows = $wpdb->get_results( "SELECT `id`, `name` FROM ".$table_name); foreach ($myrows as $details) { echo $details->id; echo $details->name;}
This code work perfect for me:
global $wpdb; $table_name = "my_table_name"; $myrows = $wpdb->get_results( "SELECT `id`, `name` FROM ".$table_name); foreach ($myrows as $details) { echo $details->id; echo $details->name;}
Ich habe Folgendes:
Wieerhalteich die Spalten 'id' und 'name' aus $ row?