scottreid1974 Posted March 4, 2007 Share Posted March 4, 2007 Hi, all, I have aproblem with a login script. it returns the message Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/goytvall/public_html/pages/eccles.php on line 9 and then Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/goytvall/public_html/pages/eccles.php on line 10 this is my login script <?php require_once("../connect.php"); if($_POST['username']) { $loginUsername = $_POST['username']; $password = $_POST['password']; $sql="SELECT username, password, url, access FROM users WHERE username='$loginUsername' AND password='$password' LIMIT 1"; $db = mysql_query ($sql); $user = mysql_fetch_array ($db); if (mysql_num_rows ($db)) { session_start(); $_SESSION['username'] = $loginUsername; session_write_close(); header('Location: '.$user['url']); } else { $warning = "Error logging in. Please check USERNAME and PASSWORD!"; } } ?> tghis code works when i test it with apache on my local test server Anyone know what might the problem be? ta scott Quote Link to comment Share on other sites More sharing options...
fert Posted March 4, 2007 Share Posted March 4, 2007 change $db = mysql_query ($sql); to $db=mysql_query ($sql) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 hi ta, it is now saying no database selected? ta Quote Link to comment Share on other sites More sharing options...
coldkill Posted March 5, 2007 Share Posted March 5, 2007 Right you need to use the mysql_select_db(); function. Please post your db_connection script. Cold Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 Here it is, thanks. <?php $host = "localhost"; $user = "goytvall_gvs"; $pass = "2007"; $db = "goytvall_gvs"; $dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); $dbase = mysql_select_db ($db); ?> Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 What i dont understand, is that the script worked on my test server on my pc, yet does not work on my website hosting comapny server? Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted March 5, 2007 Share Posted March 5, 2007 Try this and tell me what happens... <?php $host = "localhost"; $user = "goytvall_gvs"; $pass = "2007"; $db = "goytvall_gvs"; mysql_connect($host,$user,$pass); @mysql_select_db($db) or die( "Unable to select database"); require_once("../connect.php"); if($_POST['username']) { $loginUsername = $_POST['username']; $password = $_POST['password']; $sql= "SELECT username, password, url, access FROM users WHERE username='$loginUsername' AND password='$password' LIMIT 1"; $db = mysql_query ($sql); $user = mysql_fetch_array ($db); if (mysql_num_rows ($user)) { session_start(); $_SESSION['username'] = $loginUsername; session_write_close(); header('Location: '.$user['url']); } else { $warning = "Error logging in. Please check USERNAME and PASSWORD!"; } } ?> I would also not post your actual username and password on a public forum. Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 it says no database selected when i open the page in the browser. Before it opened, it just did not let me log into the user pages. Ta Scott Quote Link to comment Share on other sites More sharing options...
btherl Posted March 5, 2007 Share Posted March 5, 2007 It opened before because you were ignoring errors. That meant that it wasn't working, but you couldn't see that it wasn't working. It's much better to see the errors so you can fix them. And the error is that no database was selected. Have you implemented ballhogjoni's suggestion of adding "or die("Unable to select database");" to the mysql_select_db() call? Also, can you post the exact error message if possible? Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 Hi added die(mysql_error()) to the line $db=mysql_query($sql) Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 hi i added or die(mysql_error()) to the line $db=mysql_query($sql) so it now looks like this $db=mysql_query($sql) or die(mysql_error()); Now when i try an log in the via the login form on my home page it says 'no database selected' I cant see where i am going wrong? thanks Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 5, 2007 Share Posted March 5, 2007 R u sure ur database name is correct. $db="database"; Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 5, 2007 Share Posted March 5, 2007 if the db name is correct then try this. <?php $host = "localhost"; $user = "goytvall_gvs"; $pass = "2007"; mysql_connect($host,$user,$pass); @mysql_select_db("goytvall_gvs") or die( "Unable to select database"); Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 it still says no databse selected. It is no something like i have not called it its proper name? my webserver mysql db says it is called goytvall_gvs. do i have to put anything else in the name as it is on webserver ? i am new to this! scott Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 This is my connect folder: do you think it look right? <?php $host = "localhost"; $user = "goytvall_gvs"; $pass = "2007"; $db = "goytvall_gvs"; $dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); $dbase = mysql_select_db ($db); ?> scott Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 5, 2007 Share Posted March 5, 2007 Yes its look right, Ok try this may it works for u. <?php $host = "localhost"; $user = "goytvall_gvs"; $pass = "2007"; $db = "goytvall_gvs"; $dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); $dbase = mysql_select_db ($db,$dbh); ?> Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 Still the same message no database selected. I am baffled! Quote Link to comment Share on other sites More sharing options...
simcoweb Posted March 5, 2007 Share Posted March 5, 2007 Just for the heck of it, try this for the connection part: mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ($db); Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 5, 2007 Share Posted March 5, 2007 Still the same message no database selected. I am baffled! Try this: <?php $host = "localhost"; $user = "goytvall_gvs"; $pass = "2007"; $dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); $dbase = mysql_select_db ("goytvall_gvs",$dbh); ?> or then try this simple one: <?php $con = mysql_connect("localhost","goytvall_gvs","2007"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("goytvall_gvs", $con); ?> Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 I still get a no database found message when i try and log into the user section of my website. www.goytvalleystriders.org.uk this is my connect.php file <?php $host = "localhost"; $user = "gvs_abc"; $pass = "password"; $db = "gvs_abc"; $dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); $dbase = mysql_select_db ($db); ?> and this is ny code on the page index page ?php require_once("connect.php"); if($_POST['username']) { $loginUsername = $_POST['username']; $password = $_POST['password']; $sql="SELECT username, password, url, access FROM users WHERE username='$loginUsername' AND password='$password' LIMIT 1"; $db=mysql_query($sql) or die(mysql_error()); $user=mysql_fetch_array($db); if(mysql_num_rows($db)) { session_start(); $_SESSION['username'] = $loginUsername; session_write_close(); header('Location: '.$user['url']); } else { $warning = "Error logging in. Please check USERNAME and PASSWORD!"; } } ?> can any one help? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted March 5, 2007 Share Posted March 5, 2007 Question. Do you have access to this database in any other way? Like through a control panel or PHP MyAdmin? I'm beginning to wonder if it even exists. Quote Link to comment Share on other sites More sharing options...
scottreid1974 Posted March 5, 2007 Author Share Posted March 5, 2007 Yes i can access my database through my webserver control panel which uses php myadmin. I have created a table and put a user in there with a url for the login to retrieve. It all appeared to work well on my desktop local apache installatron. Scott Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted March 9, 2007 Share Posted March 9, 2007 sorry i've been away. Try to forget about the "connect.php" page and just put the <?php $host = "localhost"; $user = "gvs_abc"; $pass = "password"; $db = "gvs_abc"; $dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); $dbase = mysql_select_db ($db); ?> in the index page and see what happens. why is your username and database name the same, any particular reason? Quote Link to comment Share on other sites More sharing options...
btherl Posted March 9, 2007 Share Posted March 9, 2007 Scott, look at this carefully: $dbase = mysql_select_db($db) or die( "Unable to select database"); That is the call to mysql_select_db(), not mysql_query(). Modify your script with this. In your latest post your script still did not have this modification. You MUST add this. Even if it doesn't change the error message you get. All of mysql_connect(), mysql_select_db() AND mysql_query() MUST have an "or die()" after them. mysql_fetch_array() and mysql_num_rows() MUST NOT have an "or die()". Don't add it to those. Also, please post your EXACT error message each time. "No database found" is totally different from "No database selected". They are not even remotely the same. We need the exact words. Please use copy and paste to show us the error message. As for the details of which host, user, pass and db to use, you should ask your hosting company about that. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted March 9, 2007 Share Posted March 9, 2007 btherl is onto something here. First 'no database selected' is not a standard mysql_error() message. Therefore that message has to be coming from some echo statement in your script you're using in your login system which is basically stating you don't have your database parameters inputted correctly where they are supposed to be for the script to read them when trying to connect. Copy and paste this into a text file, call it db_test.php, upload it to your public_html folder and then summon it in your browser. Let us know what it says. <?php $host = "localhost"; $user = "gvs_abc"; $pass = "password"; // make sure this is correct! $db = "gvs_abc"; $dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error()); $dbase = mysql_select_db ($db); $sql = "SHOW TABLES"; $results = mysql_query($sql) or die(mysql_error()); echo $results; ?> If your database is truly there and truly has tables in it then this will display that and since it's not reliant upon any other scripts then it should either display a mysql_error OR the results. BE SURE that you edit the $pass variable! Wasn't sure of your password Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.