Wesley Brooks
Wesley Brooks

Reputation: 1

Custom SQL Queries in PHP with wordpress

I'm working on writing a SQL query in PHP for a wordpress rest API, using the UniRest wordpress API (it's for Unity and uses .Net). I'm having problems doing a simple SQL query here. This is what i've been trying:

global $wpdb;
$userID = $URInput["userID"];

$user_data = $wpdb->get_results( "SELECT * FROM wpdb->user_data WHERE unity_id = $userID" );
$result = array();
$result["userID"] = $user_data[0]["current_session"];
$UROutput["data"] = $result;
$UROutput["result"] = "OK";

I've already tested userID to make sure it's the expected value and it is, same with the output of result.userID (if I set result.userID = 5 manually, it works fine). It looks like the problem can only be in the select statement, but I've followed the documentation pretty closely and when I try to access $user_data[0]["current_session"] I always get 0, when I am expecting to get 4. Not sure what's going on so any help would be appreciated.

I've changed the select statement a few different times. My first change was to:

SELECT * FROM {wpdb->prefix}user_data WHERE unity_id = $userID;

Second change: SELECT * FROM user_data WHERE unity_id = $userID

And lastly I changed how I accessed user_data such as: $result["userID"] = $user_data["current_session"]; None of the changes here worked, and I always get the value of userID being equal to 0.

UPDATE:

Here's an update of the table structure in question. The table is named user_data and has the following attributes: id (int), unity_id (int), session_1(int), session_2(int), session_3(int), session_4(int), session_5(int), session_6(int), session_7(int), session_8(int), session_9(int), session_10(int), school(int), current_session(int).There are currently the following tuples in the table:

(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
(3, 13, -1, 1, 4, -1, -1, -1, -1, -1, -1, -1, 0, 4)

Currently I am trying to use the select statement to access the second tuple I listed above

UPDATE 2:

I figured out by using wpdb->check_connection() that the script isn't properly connected to the database. I tired connecting it using a different wpdb function, but it still isnt working. Any help on how to connect it would be greatly appreciated.

Upvotes: 0

Views: 222

Answers (0)

Related Questions