Mysql Query in PHP only works locally

Asked

Viewed 633 times

5

The system I’m maintaining was using two types of query (don’t ask me why) and locally (localhost) works perfectly. When I uploaded the project to the server, the querys declared as such:

<?php $sql = mysql_query("INSERT INTO entrada_produto (id_produto, descricao, qtde, valor_unitario, unidade, cd, data_entrada) VALUES ('{$id_produto}', '{$descricao}', '{$qtde}', '{$valor_unitario}', '{$unidade}', '{$cd}', now())") or die(); ?>

Do not work. Show the following error:

Warning: mysql_query() [function.mysql-query]: Access denied for user.........
  • You have imported the database table into the server?

  • Hello, I did the local export and imported to the database on the server.

2 answers

9


For security, many standard installations and web hosts set the default Mysql user to be usuario@localhost, that is, only local access.

If you have administrative access to the server, you need to add access to usuario@% to access from anywhere (the % is the "joker", allowing access from any host); or even usuario@PREENCHA_SEU_HOST_ESPECIFICO for example, if you need external access by one machine only.

An example command to authorize a user on any host and in all tables:

CREATE USER 'meuusuario'@'%' IDENTIFIED BY 'senhadousuario';
GRANT ALL PRIVILEGES ON *.* TO 'meuusuario'@'%';

Note that you do not always want all the privileges for the user, so it is best to granulate the access, as intended use. See more details on Grant documentation.

If you don’t have administrative access, try the change with your hosting support.

  • Thanks for the tip, buddy.

1

Access denied for user...

Are you saying that the user you are using has no access.
Check the access data (host, user and password), the server configuration should be different from your local configuration.

  • That’s the problem. The connection is made with a connection class: <? php Connection::getInstance()->connect($username, $password, $database); ?>

  • @piters Have you ever tried to access the database remotely from a terminal using mysql even? It may be that the user/password are different

  • Still, somewhere you need to put the access data... The error you have experienced is saying that your credentials are wrong, you have access to the configuration file?

  • Yes I have access, before this Connection has this data: $username = 'user'; $password = 'password'; $database = 'banco_de_data';

  • Beauty, and can access the server database by some interface (web or remote)?

  • Yes with you, via the web (phpMyAdmin).

  • And the user, password and database are the same as the one you checked?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.