Are any results returned

  $sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}my_table_name WHERE FileName = %s", $FileName);
  if (current_user_can('administrator'))
    $wpdb->show_errors();
  $Results = $wpdb->get_results($sql, ARRAY_A);
  if (count($Results) == 0)
    return(False);
  else
    return($Results);

Loop through all row results

  $sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}my_table_name WHERE FileName = %s", $FileName);
  if (current_user_can('administrator'))
    $wpdb->show_errors();
  $Results = $wpdb->get_results($sql, ARRAY_A);
  foreach ($Results as $Result)
  {
    $MyValue = $Result['MyColumnName'];
  }

Get single result

  $sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}my_table_name WHERE FileName = %s", $FileName);
  if (current_user_can('administrator'))
    $wpdb->show_errors();
  $Results = $wpdb->get_results($sql, ARRAY_A);
  if (count($Results) > 0)
  {
    $Result = $Results[0];       //Get the first row of the results
    $MyValue = $Result['MyColumnName'];
  }

Is a result returned?

 if (!isset($Result['MyColumnName']))
    return;

Select From Row + Value Array

/*
  $FieldNamesArray = array('ColumnName1', 'ColumnName2');
  $Results = db_user_read_fields(bp_loggedin_user_id(), $FieldNamesArray);
  $ColumnName1 = ['ColumnName1'];
  $ColumnName2 = ['ColumnName2'];
*/
function db_user_read_fields($user_id, $FieldNamesArray)
{
  global $wpdb;

  $sql = "SELECT ";

  $FirstField = true;
  foreach ($FieldNamesArray as $key)
  {
    if ($FirstField)
      $sql .= " $key ";
    else
      $sql .= ", $key ";
    $FirstField = false;
  }
  
  $sql .= $wpdb->prepare(" FROM {$wpdb->prefix}tbl_my_table WHERE user_id = %d", $user_id);

  $Results = $wpdb->get_results($sql, ARRAY_A);
  $Results = $Results[0];       //Get the first row of the results

  return ($Results);
}

Read SQL Server Time

//******************************************
//******************************************
//********** READ SQL SERVER TIME **********
//******************************************
//******************************************
function db_read_server_time ()
{
  global $wpdb;
  
  $sql ="SELECT NOW() as SqlServerDateTimeNow";
  if (current_user_can('administrator'))
    $wpdb->show_errors();
  $Results = $wpdb->get_results($sql, ARRAY_A);
  if (count($Results) > 0)
    return($Results[0]['SqlServerDateTimeNow']);
  else
    return("");     
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.