Jump to content

mysql_fetch_array(): not a valid mysql result resource


scottreid1974

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.