$ wpdb-> get_row () solo devuelve una sola fila?
4 respuestas
- votos
-
- 2011-04-08
De hecho,use
get_row()
solo cuandoespere obtener un resultado; de lo contrario,puede usarget_results()
Indeed, use
get_row()
only when you expect to get one result, else you can useget_results()
-
Para referencia ver https://developer.wordpress.org/reference/classes/wpdb/get_row/ y https://developer.wordpress.org/reference/classes/wpdb/get_results/For reference see https://developer.wordpress.org/reference/classes/wpdb/get_row/ and https://developer.wordpress.org/reference/classes/wpdb/get_results/
- 2
- 2017-06-21
- user51928
-
- 2011-04-08
Haytresformas deextraer datos de labase de datos.
1.
$wpdb->get_var
: useestopara obtener un valor único de latabla de labase de datos. Como si quisieras contarelnúmerototal de comentarios. Puedes hacerlo de la siguientemanera:<?php $comment_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments;")); echo '<p>Total comments: ' . $comment_count . '</p>'; ?>
2.
$wpdb->get_row
:para recuperar unafila completa de latabla,puede usaresto.<"Ejemplo:×
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ) ); echo $thepost->post_title; ?>
O
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ), ARRAY_A ); print_r ($thepost); ?>
Al usarelparámetro
ARRAY_A
en get_row,los datos de lapublicación se devuelven como unamatriz asociativa. Alternativamente,puede usarelparámetroARRAY_N
para devolver los datos de supublicaciónen unamatrizindexadanuméricamente.3.
$wpdb->get_results
: las consultasSELECT
estándar deben usar lafunciónget_resultspara recuperarmúltiplesfilas de datos de labase de datos.<?php global $wpdb; $allposts = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish'") ); foreach ($allposts as $singlepost) { echo '<p>' .$singlepost->post_title. '</p>'; } ?>
ynecesitael último,comopuedeesperar.
There are three ways to pull data from the database.
1.
$wpdb->get_var
:use this to get a single value from the database table. Like if you want to count the total number of comments. You can do it in following way:<?php $comment_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments;")); echo '<p>Total comments: ' . $comment_count . '</p>'; ?>
2.
$wpdb->get_row
: To retrieve an entire table row you can use this.Example:
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ) ); echo $thepost->post_title; ?>
OR
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ), ARRAY_A ); print_r ($thepost); ?>
By using the
ARRAY_A
parameter in get_row your post data is returned as an associative array. Alternatively, you could use theARRAY_N
parameter to return your post data in a numerically indexed array.3.
$wpdb->get_results
:StandardSELECT
queries should use the get_results function for retrieving multiple rows of data from the database.<?php global $wpdb; $allposts = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish'") ); foreach ($allposts as $singlepost) { echo '<p>' .$singlepost->post_title. '</p>'; } ?>
and you need the last one, as you can expect.
-
Maravillososejemplos de detalles.Wonderful detail examples..
- 1
- 2013-01-27
- pixelngrain
-
¡Por supuesto! Por quéno..Sure! why not..
- 0
- 2013-01-28
- pixelngrain
-
- 2011-09-01
$wpdb->get_row('query', output_type, row_offset);
row_offset (entero) Lafila deseada (siendo 0 laprimera).El valorpredeterminadoes 0.
$wpdb->get_row('query', output_type, row_offset);
row_offset (integer) The desired row (0 being the first). Defaults to 0.
-
- 2016-11-16
mi soluciónes simple ..
<?php function count_results() { # use the data base global $wpdb; # Query to count all results from one table $sql_count_results = ' SELECT count(*) as count FROM `YOUR_TABLE`;'; # Ejecute function $results = $wpdb->get_row( $sql_count_results , OBJECT ); # Return results return $results->count; }
Uso:
<?php echo count_results();
my solution is simple..
<?php function count_results() { # use the data base global $wpdb; # Query to count all results from one table $sql_count_results = ' SELECT count(*) as count FROM `YOUR_TABLE`;'; # Ejecute function $results = $wpdb->get_row( $sql_count_results , OBJECT ); # Return results return $results->count; }
Use:
<?php echo count_results();
-
Seríagenial sitambiénpudieraexplicar qué haceeste código además depublicarlo.It would be great if you can also explain what this code does in addtion to posting it.
- 4
- 2016-11-16
- bravokeyl
-
Esto cuenta lasfilasen unatabla,noes una respuesta a lapregunta del OP.This counts the rows in a table, it is not an answer to the OP's question.
- 0
- 2019-01-15
- alexg
¿Por quées?Intenté lamisma consultaen la consola y devolvió variasfilas.Aquíestá la consulta:
$this->wpdb->get_row("SELECT * FROM ".$this->wpdb->users." WHERE status = 'active'", ARRAY_A);
Siguemostrando lamismafila cuando hay varios usuarios activos.¿Mefalta algo?