Skip to main content
added 1 character in body; edited title
Source Link
butlerblog
  • 5.1k
  • 3
  • 26
  • 42

get Get random row from custom table

iI have custom table where iI store quotes and author:

function quote_install(){
global $wpdb;
global $quote_db_version;
$table_name = $wpdb->prefix . 'quote'; 

// create sql your table             
$sql = "CREATE TABLE " . $table_name . " (
  ID int(11) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,   
  quote text NOT NULL,
  author text NOT NULL,
  qtag ENUM('G', 'W', 'Z', 'H', 'M') NOT NULL default 'G',
  PRIMARY KEY  (ID)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql); }

then iThen I want to get single row with quote and author to display.

function read_single_Quote( $id=NULL ) {
global $wpdb;
 
 $table_name = $wpdb->prefix . 'quotes';
// random select
 if($id ==NULL){
    $sql = $wpdb->prepare( "
        SELECT *
        FROM {$wpdb->prefix}'quotes'
        ORDER BY RAND()
        LIMIT 1
    ");
 } //get the row id = $id
else {
    $sql = $wpdb->prepare( "
    SELECT *
    FROM {$wpdb->prefix}'quotes'
    WHERE ID = %d
    LIMIT 1
    ", $id );
 }

$result = $wpdb->get_results( $sql );

// databse error, return false
if ( ! $result ) { return false; }

// return first result
return $result[0];

}

whatWhat is wrong? whatWhat is the most efficient way to get random row from custom table?

get random row from custom table

i have custom table where i store quotes and author

function quote_install(){
global $wpdb;
global $quote_db_version;
$table_name = $wpdb->prefix . 'quote'; 

// create sql your table             
$sql = "CREATE TABLE " . $table_name . " (
  ID int(11) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,   
  quote text NOT NULL,
  author text NOT NULL,
  qtag ENUM('G', 'W', 'Z', 'H', 'M') NOT NULL default 'G',
  PRIMARY KEY  (ID)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql); }

then i want to get single row with quote and author to display.

function read_single_Quote( $id=NULL ) {
global $wpdb;
 
 $table_name = $wpdb->prefix . 'quotes';
// random select
 if($id ==NULL){
    $sql = $wpdb->prepare( "
        SELECT *
        FROM {$wpdb->prefix}'quotes'
        ORDER BY RAND()
        LIMIT 1
    ");
 } //get the row id = $id
else {
    $sql = $wpdb->prepare( "
    SELECT *
    FROM {$wpdb->prefix}'quotes'
    WHERE ID = %d
    LIMIT 1
    ", $id );
 }

$result = $wpdb->get_results( $sql );

// databse error, return false
if ( ! $result ) { return false; }

// return first result
return $result[0];

}

what is wrong? what is the most efficient way to get random row from custom table?

Get random row from custom table

I have custom table where I store quotes and author:

function quote_install(){
global $wpdb;
global $quote_db_version;
$table_name = $wpdb->prefix . 'quote'; 

// create sql your table             
$sql = "CREATE TABLE " . $table_name . " (
  ID int(11) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,   
  quote text NOT NULL,
  author text NOT NULL,
  qtag ENUM('G', 'W', 'Z', 'H', 'M') NOT NULL default 'G',
  PRIMARY KEY  (ID)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql); }

Then I want to get single row with quote and author to display.

function read_single_Quote( $id=NULL ) {
global $wpdb;
 
 $table_name = $wpdb->prefix . 'quotes';
// random select
 if($id ==NULL){
    $sql = $wpdb->prepare( "
        SELECT *
        FROM {$wpdb->prefix}'quotes'
        ORDER BY RAND()
        LIMIT 1
    ");
 } //get the row id = $id
else {
    $sql = $wpdb->prepare( "
    SELECT *
    FROM {$wpdb->prefix}'quotes'
    WHERE ID = %d
    LIMIT 1
    ", $id );
 }

$result = $wpdb->get_results( $sql );

// databse error, return false
if ( ! $result ) { return false; }

// return first result
return $result[0];

}

What is wrong? What is the most efficient way to get random row from custom table?

Bumped by Community user
Bumped by Community user
Source Link

get random row from custom table

i have custom table where i store quotes and author

function quote_install(){
global $wpdb;
global $quote_db_version;
$table_name = $wpdb->prefix . 'quote'; 

// create sql your table             
$sql = "CREATE TABLE " . $table_name . " (
  ID int(11) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,   
  quote text NOT NULL,
  author text NOT NULL,
  qtag ENUM('G', 'W', 'Z', 'H', 'M') NOT NULL default 'G',
  PRIMARY KEY  (ID)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql); }

then i want to get single row with quote and author to display.

function read_single_Quote( $id=NULL ) {
global $wpdb;
 
 $table_name = $wpdb->prefix . 'quotes';
// random select
 if($id ==NULL){
    $sql = $wpdb->prepare( "
        SELECT *
        FROM {$wpdb->prefix}'quotes'
        ORDER BY RAND()
        LIMIT 1
    ");
 } //get the row id = $id
else {
    $sql = $wpdb->prepare( "
    SELECT *
    FROM {$wpdb->prefix}'quotes'
    WHERE ID = %d
    LIMIT 1
    ", $id );
 }

$result = $wpdb->get_results( $sql );

// databse error, return false
if ( ! $result ) { return false; }

// return first result
return $result[0];

}

what is wrong? what is the most efficient way to get random row from custom table?