$ wpdb-> get_row () restituisce solo una singola riga?
4 risposta
- voti
-
- 2011-04-08
Effettivamente,usa
get_row()
solo quandoprevedi di ottenere un risultato,altrimentipuoi usareget_results()
Indeed, use
get_row()
only when you expect to get one result, else you can useget_results()
-
Per riferimento vedere https://developer.wordpress.org/reference/classes/wpdb/get_row/ e 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
Esistonotremodiperestrarrei dati dal database.
1.
$wpdb->get_var
: usa questoper ottenere un singolo valore dallatabella del database. Come se volessi contareilnumerototale di commenti. Puoifarlonelmodo seguente:<?php $comment_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments;")); echo '<p>Total comments: ' . $comment_count . '</p>'; ?>
2.
$wpdb->get_row
:per recuperare un'intera riga dellatabellapuoi utilizzare questo.<"Esempio :"
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ) ); echo $thepost->post_title; ?>
OPPURE
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ), ARRAY_A ); print_r ($thepost); ?>
Usandoilparametro
ARRAY_A
in get_rowi dati deltuopost vengono restituiti come un array associativo. In alternativa,puoi utilizzareilparametroARRAY_N
per restituirei dati deltuo articoloin un arraynumerico.3.
$wpdb->get_results
: le querySELECT
standard dovrebbero utilizzare lafunzioneget_resultsper recuperarepiù righe di dati dal 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>'; } ?>
e haibisogno dell'ultimo,comepuoi aspettarti.
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.
-
Splendidiesempi di dettagli ..Wonderful detail examples..
- 1
- 2013-01-27
- pixelngrain
-
Sicuro!perchèno..Sure! why not..
- 0
- 2013-01-28
- pixelngrain
-
- 2011-09-01
$wpdb->get_row('query', output_type, row_offset);
row_offset (intero) La riga desiderata (0 è laprima).Il valorepredefinito è 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
lamia soluzione è semplice ..
<?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; }
Usa:
<?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();
-
Sarebbefantastico setupotessi anche spiegare cosafa questo codice oltre apubblicarlo.It would be great if you can also explain what this code does in addtion to posting it.
- 4
- 2016-11-16
- bravokeyl
-
Questo conta le righein unatabella,non è una risposta alla domanda dell'OP.This counts the rows in a table, it is not an answer to the OP's question.
- 0
- 2019-01-15
- alexg
Perché?Hoprovato la stessa querynella consolee ha restituitopiù righe.Ecco la query:
$this->wpdb->get_row("SELECT * FROM ".$this->wpdb->users." WHERE status = 'active'", ARRAY_A);
Continua a restituire la stessa singola riga quando ci sono diversi utenti attivi.Mimanca qualcosa?