0

I am trying to send a query to my server from my website. I have allowed access to all IP's using root@'%', and I have set the bind port to 0.0.0.0 in my.ini

It appears it is connecting to the server now, the query just won't work.

$link=mysql_connect('server_ip', 'root', 'abcdefg') or die('Sorry, please try again another time.');
mysql_select_db('chars');

mysql_query("INSERT INTO `accounts` (`uuid`, `name`) VALUES ('hi', 'hi')", $link);
echo "Done";

When I run this, I see the "Done" on my webpage, however the query does not actually put the account "hi" into the database, I have been doing a little experimenting with the code trying to get it to work, which is why it may look strange.

Any help is appreciated, thank you.

  • You need to check the return value from `mysql_query()`. It returns false on failure, then `echo mysql_error();` – Michael Berkowski Aug 09 '14 at 21:47
  • An important note though - new code should not be written with the now-deprecated `mysql_*()` functions. You ought to begin using MySQLi or PDO instead, as `mysql_*()` will be removed in a future PHP version. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Michael Berkowski Aug 09 '14 at 21:48
  • [This is an excellent tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) which introduces PDO in context of the mysql_*() API.... – Michael Berkowski Aug 09 '14 at 21:49
  • It says "No database selected". I have a database selected though. – helpmeplease Aug 09 '14 at 21:50
  • Ah, I did not know that. Thanks for making me aware I will definitely begin using it. – helpmeplease Aug 09 '14 at 21:52
  • Move the `echo mysql_error();` to occur right after the `mysql_select_db()` call, as that must have failed (such as if `root@%` doesn't actually have rights on that database - which you can determine with `SHOW GRANTS FOR root@'%'` – Michael Berkowski Aug 09 '14 at 21:52
  • mysql_select_db('chars',$link) maybe? – pregmatch Aug 09 '14 at 21:56
  • @pregmatch Only if the code we see here is not the actual code (and another connection was subsequently opened after `$link`) – Michael Berkowski Aug 09 '14 at 21:58
  • Thanks so much Michael you have saved me a lot of time. Apparently when I added the user to connect with any ip '%' it had no privileges because even though the privileges were checked, I had to go back and check them again. It's working now. – helpmeplease Aug 09 '14 at 21:58

1 Answers1

1

Try:

mysql_select_db('chars', $link);

Also, for what it's worth:

1) As someone else already mentioned, you should avoid using mysql_. Use mysqli_ or PDO.

2) Maybe your code is just for testing but you should avoid using root for queries like this. Create another user with just the privileges needed for your application and use that. You've essentially opened up MySQL to the world, especially since you're using the % wildcard.

vch
  • 211
  • 2
  • 5