user1419634 user1419634 - 2 months ago 6
MySQL Question

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

i have this code in a theme-functions.php file on wordpress, im using this script and when needed im running a

echo
followed 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.

Any ideas where im going wrong?

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";
}
}


`

UPDATE - RESOLVED:

Thanks for all the help! here is the code in case anyone else has the same issue

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.

Thanks!

Answer

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