mysql_query error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zyklon
Forum Commoner
Posts: 49
Joined: Mon Jul 31, 2006 7:14 pm
Location: MA, USA

mysql_query error

Post by zyklon »

i just revisiteed some old code i had, and apparently forgotten alot of php. anyhow, i have a warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) error and a Warning: mysql_query() [function.mysql-query]: A link to the server could not be established error. i am using xampp and include a config file,

Code: Select all

<?
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("(i took name out)") or die(mysql_error()); 
?>
the following is the error and the lines related to it;

Code: Select all

$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM user WHERE username = '$usercheck'") 
or die();
$check2 = mysql_num_rows($check);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That error message suggests you are trying to connect as a user named ODBC. Is that the case. Also, did you change the users password? What version of mysql are you running?
User avatar
zyklon
Forum Commoner
Posts: 49
Joined: Mon Jul 31, 2006 7:14 pm
Location: MA, USA

Post by zyklon »

xampp 1.6.0a is 5.0.33, or at least that what phpmyadmin says.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you see your database in phpMyAdmin?
User avatar
zyklon
Forum Commoner
Posts: 49
Joined: Mon Jul 31, 2006 7:14 pm
Location: MA, USA

Post by zyklon »

yup, i have it empty, but yes.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Then you should be able to add/edit users in phpMyAdmin. Or at the very least, use the phpMyAdmin credentials to get into the database that you are wanting to connect to.
User avatar
zyklon
Forum Commoner
Posts: 49
Joined: Mon Jul 31, 2006 7:14 pm
Location: MA, USA

Post by zyklon »

i added a user using it, nothing, should i get php4 instead, would that do anything?


note: i solved it! i swapped to php4 and mysql4!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sorry, I don't think I came across correctly. The database users, you should be able to edit those so that your script can actually connect to the app. phpMyAdmin is a PHP application that is connecting to MySQL. The same way it does it your app can do it, as long as you use the same credentials. Or you can actually go into the database users admin piece and change the password for the user you are attempting to connect with and try that.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

zyklon wrote:Access denied for user 'ODBC'@'localhost' (using password: NO)
When there is no mysql connection mysql_query tries to establish a default connection. 'ODBC'@'localhost' strongly indicates this default connection.

Replace <? by <?php
mysql_connect returns a link resource, store it and use it is subsequent calls
Have a read of http://en.wikipedia.org/wiki/SQL_injection and use mysql_real_escape_string

Code: Select all

<?php
$mysql = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("secret or stupid name", $mysql) or die(mysql_error());
?>
...
<?php
require 'config.php';

$usercheck = mysql_real_escape_string($_POST['username'], $mysql) or die(mysql_error());
$query = "SELECT username FROM user WHERE username = '$usercheck'";
$check = mysql_query($query, $mysql) or die(mysql_error().': '.$query);
$check2 = mysql_num_rows($check);
?>
Post Reply