Return Results or False if none
/*
$Results = MyFunctionName();
if ($Results !== False)
{
foreach ($Results as $Result)
{
= $Result['MyColumnName'];
}
}
*/
$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("");
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.