user1419634
user1419634

Reputation: 5

PHP & MYSQL script inside wordpress theme-functions.php to pull & echo data

I have this code in a theme-functions.php file on wordpress, I'm using this script and when needed I'm running a echofollowed by the string to replicate the data.

currently when I use this I just get 0 results, echo on the $userlogin is working so I know the filter in the mysql select is fine.

function calliod() {
    global $wpdb;

$user = new WP_User(get_current_user_id());
$userlogin = $user->user_login;
echo $userlogin;

$iodoutput = "SELECT * FROM $wpdb->iod WHERE wp_iod.USERID ='". $userlogin. "'";
$result1 = $iodoutput;

if ($result1->num_rows > 0) {

while($row = $result1->fetch_assoc()) {

$USERID= "" . $row["USERID"]. "";
$CurrentPayment= "" . $row["CurrentPayment"]. "";
$LastMonthPayment= "" . $row["LastMonthPayment"]. "";
}
} else {
echo "0 results";
}
}

RESOLVED:

Here is the code

Inside Functions.php

function calliod(){

global $wpdb;
$user = new WP_User(get_current_user_id());
$userlogin = $user->user_login;

foreach( $wpdb->get_results("SELECT * FROM wp_iod WHERE wp_iod.USERID ='". $userlogin. "'") as $key => $row) {

global $USERID;
$USERID = $row->USERID;
}
}

On WP Page

<?php 
calliod(); 
echo $GLOBALS['USERID']
; ?>

Now i can pull any column from the table mentioned as long as i define it in the first script.

Upvotes: 0

Views: 1528

Answers (1)

Greg Burkett
Greg Burkett

Reputation: 1945

Your query that you're building is never actually executed. See the documentation for the WPDB class, but basically you need a line that says this:

$result1 = $wpdb->get_results( "SELECT * FROM wp_iod WHERE wp_iod.USERID ='". $userlogin. "'" );

More info here: https://codex.wordpress.org/Class_Reference/wpdb

Upvotes: 2

Related Questions