I am using the following SQL query to pull table date from another database.
$query = "SELECT option_value FROM db_name.wpm_options where option_name like 'options_go_tracking_%_tracking_pixel'";
$results = $wpdb->get_results( $query );
Everything is connecting fine and the results of the query return as expected. I am a little unsure as to how I can now loop through the results of the query, I think a foreach function is required but being a novice with sql I am having trouble proceeding.
The name of the field is "option_value", would this be used in the foreach?
My attempt to far is below, the echo is being output 10 times and there are 10 records in the sql query so I must be close, just need to output the data within the field.
$query = "SELECT option_value FROM db_nameprivate.wpm_options where option_name like 'options_go_tracking_%_tracking_pixel'";
$results = $wpdb->get_results( $query );
if($results):
foreach($results as $row): setup_postdata($row);
echo "output";
// Trying to output the results of query here, the content of each "option_value" field
endforeach;
else: '.echo "No Records Found".';
endif;
In WordPress, the $wpdb->get_results($query)
returns an array of objects with properties that represent their associated column names. When iterating over a result-set, you can simply access the column by its associated property, just like this:
echo $row->option_value
Try this?
$query = "SELECT option_value FROM db_name.wpm_options where option_name like 'options_go_tracking_%_tracking_pixel'";
$results = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($results) {
echo $row['option_value'];
}
$query = "SELECT option_value FROM db_nameprivate.wpm_options where option_name like 'options_go_tracking_%_tracking_pixel'";
$results = $wpdb->get_results( $query );
if($results){
foreach($results as $row) {
echo $row['option_value'];
// Trying to output the results of query here, the content of each "option_value" field
}
}