tags:

views:

43

answers:

3

Hello All,

First, thanks for all the help I've received so far from StackOverflow. I've learned much.

Once again, I'm posing a rudimentary question that I've searched on, but cannot find the exact answer to. Here or on PHP.net.

It's sort of like what this guy asked, but not exactly: http://stackoverflow.com/questions/288603/mysql-throwing-query-error-yet-finishing-query-just-fine-why

So, I saw my errorlog ballooning up when I checked my site directory and opened to notice that a bunch of errors have been recorded since I wrote this new Admin area. I know something is obviously awry with my scripting for the error to be thrown, but the weird thing is, the script actually runs through and pulls all the data I need without breaking.

The log contains:

PHP Warning: mysql_query() [function.mysql-query]: Access denied for user 'someuser'@'localhost' (using password: NO) in /home/mysite/adminconsole.php on line 15

I don't get that because that very line is where I setup my connection... the exact same way I do it everywhere else on the site with no problem.

After that error, I have these thrown at the same time

[09-Apr-2010 08:44:18] PHP Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/mysite/adminconsole.php on line 15

[09-Apr-2010 08:44:18] PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mysite/adminconsole.php on line 16

From what I read in the other guys thread, the problem is the contents of the query maybe? Maybe my query is malformed?

Thanks so much for any guidance you can provide.

-Rob

ADD: To Dominic

Dominic

session_start();
if (isset($adminusr))
{
mysql_connect("localhost","user","pw") or die(mysql_error());
mysql_select_db("my_db") or die(mysql_error());
}

else    {
header("location:admin.php");
}

LINE 14 #SETUP THE QUERIES HERE FROM SESSION INFO

LINE 15 $sql1=mysql_query("SELECT * FROM admins WHERE username='$adminusr'"); ///connecting to the admin DB

LINE 16 $result1=mysql_fetch_array($sql1);

### OLD BAR INFO QUERY
$sql2=mysql_query("SELECT * FROM bars WHERE bar_id='$result1[bar_id]'"); ///connecting to the old bars DB
$result2=mysql_fetch_array($sql2);

###LIQUOR QUERIES
$sql3=mysql_query("SELECT * FROM inv_beer WHERE bar_id='$result1[bar_id]'"); ///connecting to the BEER INVENTORY DB
$beer_results=mysql_fetch_array($sql3);
+1  A: 

What might be the issue is MySQL allows only one connection per user and you are trying to connect more than once which causes Access denied. Then your queries run fine because you already have one connection established. Without code though it is just a guess.

Ivo Sabev
Thanks Ivo... should I be doing mysql_close after I run each script then? A friend told me it was a waste
rob - not a robber
No you shouldn't do close, just don't try to connect if you already have an open connection.
Ivo Sabev
Hmmmm... I am trying to deduce something from your comment and Dominic's. Maybe I am trying to make a redundant connection to that DB? I do connect to "admins" on the initial login on another page, but I guess the connection is still open?I tried connecting again to establish new variables.
rob - not a robber
Have something like this:if (!$db) { $db = mysql_connect(...);}But still I am not sure if that is the cause. Do you have more than one mysql_connect?
Ivo Sabev
No.. only one connection. Please see edited bit up top including all PHP and DB stuff for the entire problem page THANKS
rob - not a robber
How often this error occurs was it just once?
Ivo Sabev
+1  A: 

If these queries MUST run, then add an or die(); to the code like so:

$sql1=mysql_query("SELECT * FROM admins WHERE username='$adminusr'") or die('Critical error  on line #'. __LINE__ .'while trying to SELECT from admins table:<br>'. mysql_error());

That way you know exactly where the error is, what you were trying to do and the MySQL error description.

PS: If you are using string concatenation like above, I sure hope you are SQL escaping your data to prevent injection.

PPS: I recommend you use mysql_fetch_assoc() instead of _array() as it uses less memory because it doesn't waste time or memory populating keys 0,1,2,etc

TravisO
Thank you Travis. Yeah I stripslashed and escaped.. just didn't want to provide a bulky example. Thanks for the reco on _fetch_assoc. Will look into it and implement
rob - not a robber
+2  A: 

If $adminusr isn't set the first time, no connection is made to the database. Put an exit() call after the header call.

After header is called, the script continues to run. That is why you get errors in the logs but don't notice them when using the site.

webbiedave
webbiedave... well, it's obviously set because all the queries rely on it to pull the appropriate record(s) and as I mentioned, the data pulls fine... was just logging this error which now, it isn't logging anymore!?!?! Weird! Thanks man!
rob - not a robber
I added to my answer after you posted more code.
webbiedave
Gotcha... yeah.. that was it. If a login failed, obviously no $adminusr was established and like you said, the script ran with no definition of $adminusr.THANK YOU
rob - not a robber
You're welcome. Glad I could help.
webbiedave