PHP Login Help Needed

mysql_fetch_array()

Page 1 of 1

4 Replies - 799 Views - Last Post: 22 August 2008 - 04:00 PM Rate Topic: -----

#1 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

PHP Login Help Needed

Posted 22 August 2008 - 02:14 PM

To start, here is my code
<?php
session_start();
function CheckCredentials($username, $password){
	$query = mysql_query("SELECT * FROM blog_users WHERE rank = 'Administrator' LIMIT 1");
	while ($result = mysql_fetch_array($query)) {
		if ($username != $result['username'] || $password != $result['password']) {
			echo "NOT LOGGED IN!";
		} else {
			echo "Hai";
		}
	}
}
CheckCredentials($_SESSION['username'], $_SESSION['password']);
?>


The error I get is:

Quote

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in (PATHTOMYSCRIPT) on line 5


I really don't understand why I'm getting the error. If it helps, here is the structure of blog_users:
id 	username 	password 	rank
----------------------------------------------------------
1 	Admin 	21232f297a57a5a74389 	Administrator

I also have another table in my blog_db but that's named blog_entries and is only for entries.

I'd appreciate it if someone could help me :)

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Login Help Needed

#2 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: PHP Login Help Needed

Posted 22 August 2008 - 02:37 PM

The query looks fine, but it doesn't look like you connect to mysql and select the database before running it. mysql_query assumes you've already opened a database connection unless you specify a db handle.
Use mysql_connect(host, user,password), then mysql_select_db(database_name) before running mysql_connect.

As a rule of thumb you should always check to make sure your query went through, if it doesn't then $query will not be defined, so a simple
if(!($query)){
	   print "MySQL query failed. Reason: ".mysql_error();
}


This post has been edited by mocker: 22 August 2008 - 02:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: PHP Login Help Needed

Posted 22 August 2008 - 03:35 PM

or you could just add or die(mysql_error()) to the end....

but yes, you never connect to a database...

Also, I want to suggest that you use smallint for rank, that way you can do things like: if($rank > 2){echo "Admin(3) or Mod(2)";}
Was This Post Helpful? 0
  • +
  • -

#4 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: PHP Login Help Needed

Posted 22 August 2008 - 03:44 PM

Oh wow, I feel stupid. I do have a connection to a database, but I didn't include the file holding the connection before using the function. Thanks guys :D
Was This Post Helpful? 0
  • +
  • -

#5 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: PHP Login Help Needed

Posted 22 August 2008 - 04:00 PM

lol, don't worry... it happens to all of us sooner or later =P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1