469,955 Members | 2,457 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

php script can't connect to local mysql, what the heck?

35
Okay so I am at a loss here. I have a website that I've previously had no trouble connecting to the mysql DB on. I have an include to a connect file with the relevant connection info, and it was working fine until today. I am trying to implement some ajax with the javascript framework mootools (although I don't see how this is causing the problem it started happening right around this time sooo...)

I am sending info from my login form to the processLogin.php page, which in turn logs me in and everything, and then is supposed to write new info to the relevant div which sent the call.

But my processLogin page started saying it "couldn't connect to the localhost", which most definitely is not my intent nor have it set it to. So I included the connect.php page on the page itself; nothing. It still had a mistake (occurring right at my first query). So I wrote the connection info directly before the query, and no longer receive that error, but still no data. I echo'd the query and get nothing. So, here is the connect.php code I include in index.php, so it can call it each time:
[PHP]<?php
$dbServer = 'mysql***.secureserver.net';
$dbDatabase = '****;
$dbUser = '****';
$dbPass = '****';

// Connect to database
$db = mysql_pconnect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to database due to error in page: " . mysql_error());
mysql_select_db($dbDatabase, $db) or die("Couldn't select database due to error in page: " . mysql_error()); ?>[/PHP]

Neither of these error message ever appear, btw, meaning the connection is working here.

Here is the code that is throwing the error:
[php]$sql = mysql_query("SELECT * FROM users WHERE username=" . $username . " AND password=" . $password . " AND activated='1'");
echo 'sql=' . $sql;
$login_check = mysql_num_rows($sql);[/php]

Where I echo sql=$sql, I get "sql=" and nothing else, and this is the error message:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /blahblahblah on line 51.
And finally, when I include the connect info instead of writing it straight to the page (and I'm including it to the page, as opposed to index.php [where I normally include it]):

Warning: mysql_query(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /webpage on line 47
Sorry for the lengthy first post, but I wanted to get all relevant info out there.
My guess is it's because I'm calling for the page through ajax, but I can't figure out how that causes the problem or how to fix it. Any ideas or help MUCH appreciated!
Nov 19 '06 #1
4 3400
ronverdonk
4,258 Expert 4TB
Let's start with the select statement. I assume that the userid and password are CHAR fields, so you'll have to include them within quotes. The entire select string incl. the variables van be code in one double-quote enclose string. So your statement is:
[php]$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'")
or die("Invalid query: " . mysql_error());
[/php]
Ronald :cool:
Nov 19 '06 #2
d3vkit
35
Let's start with the select statement. I assume that the userid and password are CHAR fields, so you'll have to include them within quotes. The entire select string incl. the variables van be code in one double-quote enclose string. So your statement is:
[php]$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'")
or die("Invalid query: " . mysql_error());
[/php]
Ronald :cool:
Ah, you're right about that... but it still didn't quite fix it. When I write the connect script into the page, it works; when I include the script, it doesn't. Is this some sort of scope that I don't understand? Can it not connect to an included script with ajax maybe?
Well, it works, but not as well as I'd like, but I suppose it's all the same. Thank you for your help; I can't believe I didn't see that.
Nov 20 '06 #3
ronverdonk
4,258 Expert 4TB
Is the connect script, the one that you include, in a different path from the script that includes it and, if so, are you correctly backtracking or forward tracking the include. E.g. if the connect script is one dir below (child dir) the including script, you must include it as [php]include('connect/dbinclude.php"');[/php].
If it is in a dir above (parent dir) the including script you define [php]include('../dbinclude.php"');[/php].
If it is in another dir down from the one above you (sibling dir), you do [php]include('../connect/dbinclude.php"');[/php].
Is that the reason?

Ronald :cool:
Nov 20 '06 #4
d3vkit
35
Is the connect script, the one that you include, in a different path from the script that includes it and, if so, are you correctly backtracking or forward tracking the include. E.g. if the connect script is one dir below (child dir) the including script, you must include it as [php]include('connect/dbinclude.php"');[/php].
If it is in a dir above (parent dir) the including script you define [php]include('../dbinclude.php"');[/php].
If it is in another dir down from the one above you (sibling dir), you do [php]include('../connect/dbinclude.php"');[/php].
Is that the reason?

Ronald :cool:
I'm sorry I never replied to you; I was going to that night, then got busy and it never let up apparently.

Just wanted to say thanks for the suggestions. I went to the code, did the include as I normally would (backtracking to the scripts directory), and it worked no problem. No idea what I was doing wrong before, but it's all sorted out now. Seemed I had done this in the first place, but oh well. I do appreciate thehelp; I learned that I don't need to include variables in my mysql call with periods. Forgot what that's called. Dang it.
Dec 14 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Peter Nikolaidis | last post: by
reply views Thread by Bill Hernandez | last post: by
5 posts views Thread by news | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.