Jump to content

Help me with Wordpress MYSQL Query Data Posting


axepex

Recommended Posts

logopng.png

 

So, as the title states;

I have a Minecraft server, and a Wordpress website.

Both server players and wordpress users have matching names.

 

I would like a code that will use the player's wordpress username to retrieve same-name player data from other tables on the server.

These other tables are created by Minecraft.

 

I  am having some trouble with adding the current user's login_name to the SQL query. They are identical, but I cannot figure out how to get the current user's info to be placed into the SQL query.

 

This is the base type of script I am using: Any help for my issue? Thanks!
 

 

 

//Sample Database Connection Script
//Setup connection variables, such as database username
//and password
$hostname="localhost";
$username="REDACTED";
$password="REDACTED!!";
$dbname="mineassa_money";
$dbtable="moneysql";
//Connect to the database
$connection = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $connection);
//Setup our query
$query = "SELECT money FROM `moneysql` WHERE `last_known_name`>=$user_login;";
//Run the Query   //Run the Query
$result = mysql_query($query);
$data=mysql_fetch_assoc($result);
echo $data['total'];
//If the query returned results, loop through
// each result
if($result)
{
while($row = mysql_fetch_array($result))
{
$name = $row["$yourfield"];
echo "Name: " . $name;
}
}
Edited by axepex
Link to comment
Share on other sites

Firstly : You really shouldn't be using mysql_ any more - it has been depreciated for years and is written out of the new PHP7 and does not work at all.

 

Second: where is the $user_login coming from and does it contain what it should?

 

Third: I really don't think you should have a > conditional in the select statement.

 

Thank you for your interest in my thread. Yes, I do need some serious help in understanding what I did wrong. Do know though, I am not a PHP programmer by nature, so I am using what I can from past knowledge.

 

The $user_login is my main issue. It's a wordpress function variable I believe.

 

Essentially, I need the "$user_login" to input the users username into the search query. I do not even know where to start. This script was a copy/paste job that I modified a bit from an online repository.

 

Any suggestions in making it work? Appreciate your feedback and suggestions. You may have to write it tho as I am a rock when it comes to PHP.

Link to comment
Share on other sites

$user_login is apparently a global returned by the WordPress function get_currentuserinfo() (see here). You'll need to call the function and declare the variables global in order to use it. However, there are issues beyond that in what you've got.

 

First off, Muddy_Funster is more than likely correct in that you don't want the greater-than comparator in your query. $user_login will return the current user name, but trying to get anything greater than or equal to that value is probably going to give you some odd results. I say 'more than likely' and 'pobably' because I don't have a full understanding of the business logic, so I could be wrong there.

 

Secondly, you're moving the internal pointer past the first returned row of the result set before you start to loop through the results by using mysql_fetch_assoc() to try retrieving the 'total' value, which is a value you don't even select in your query. Which means it's not going to be there.

 

Third, when you do attempt to loop through what should now be an empty record set (see the first part of the previous comment), you attempt to get the value using $yourfield, which isn't set anywhere. You know the column you're attempting to retrieve (money), because that's the only column you've selected - use that index ($row['money']).

 

Finally, the mysql* functions have been removed from PHP. As soon as your hosting provider updates, everything you're writing will break. It's best to use PDO or MySQLi, unless the 'moneysql' table is in WordPress database. In which case, it may be easier to simply use the WordPress WP_Query object. At least that will handle some of the low-level stuff behind the scenes. For instance, WP finally updated the core code to stop using plain mysql_* functions, so you don't have to worry about that. Admittedly, they only did that a little less than two years ago as far as I can tell, but hey, it's done.

  • Like 1
Link to comment
Share on other sites

Thank you maxxd for your input, and you Muddy_Funster, for your continued assistance...

I am very sure you both know exactly what I am trying to do. Sadly, I have no idea how to update the script in regards to defining said functions and calling / printing this data.

 

Maxxd, the site you posted is indeed a wordpress help page for understanding how to retrieve user info.

 

If someone could create this script I am looking for, I would gladly pay for the service. This is a very important part in my next development stage. Can either of you jump in for the assist?

Edited by axepex
Link to comment
Share on other sites

I don't have much time to commit outside my 8 to 6 day job I'm afraid, so while I can offer help on coding issues I can't give certainty of being able to complete a commision in a reasonable time scale.  If you're happy to pay then post your request in the freelance boards, where people go to pick up such things (or maybe PM a staff member and ask for this thread to be moved there).  

Link to comment
Share on other sites

I'm in the same boat as Muddy_Funster - my job is currently got me very, very busy. I'll be more than happy to help out when I can with advice and code snippets, but can't commit to a full commission. If you want to keep working at it, post your updates and work here. If that's not a possibility, best bet is to follow Muddy_Funster's advice and post to the job board.

Link to comment
Share on other sites

That's fair enough. I as well find time to come here for assistance in between my own 2 jobs haha, so we're all very busy... and employed! (Which is great!)

 

I put an ad out on Fiverr for a script request. Unsure of the success rate of such website, but I can't imagine this task being too complex. I appreciate your time though to chime in. I will certainly return here with my results, if founded... or If I need any more help.

Link to comment
Share on other sites

Best of luck, I hope you get the solution you're after - but please remember: just because someone on the internet is willing to charge you for something does not mean that it will be at all good.  There are a lot of blagger's out there so never pay (at least never pay in full) until you have the code.

 

I personally recommend the use of the freelance board on here because I've been a member here for years and know that the vast majority of senior posters are both honest and highly skilled programmers, so there's a good chance you will get a solid bit of code with a quick turn around (that said you are just vulnerable to scammers as anywhere else).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.