11 Replies - 1707 Views - Last Post: 23 December 2011 - 11:59 AM Rate Topic: -----

#1 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 10:40 AM

Greetings. My name is Beau, and I'm fairly new to these boards.
I am quite new to PHP and mySQL. I am trying to check if the session's user is an admin.
Surely there is a better way to do this.
Here is a snippet of the code that I think has incorrect logic:

			$conn = mysql_connect("$host", "$username", "$my_gateway")or die("Connection failed. If you received this error it is the webhost's fault, not yours. Please retry. <a href = 'index.php'> Home </a>"); 
			mysql_select_db("$db")or die("Connection failed. If you received this error, it is mySQL's fault, not yours. Please retry. <a href = 'index.php'> Home </a> ");
			$sql = 
			"SELECT * FROM PREREG
			WHERE admin=1 
			AND emaad='$user1'"; 
			$result = mysql_query($sql, $conn); 
			if($result) { 
				echo "You are an admin!"; 
			} 



Alright. So, there are two central properties I am attempting to verify.
1) admin. If the user is an admin, the property will be manually set by the owner of the database to 1. Otherwise it remains 0.
2) emaad. This is the email that was registered into the session.

Now I'm attempting to find the row inside the table that contains the registered email and check if admin=1. If so, echo 'You are an admin!'.

I did not receive an error. I just get the elements originally inside the page.
My registered user is however an admin, and I do not get this
echo
message.

$user1 is defined as
 $_SESSION['input_use']; 
.
Any help?
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP/mySQL Table Access. No Error, but not the correct response.

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 10:46 AM

Your $result variable doesn't hold a true/false result of the query, it holds a reference to a resource id. So whatever the query is, assuming it doesn't fail, it will return this resource id and will therefore pass your:

if($result)



Test.

A simple way to achieve what you're trying to do, is:

$result = mysql_query($sql, $conn);
$count = mysql_num_rows($result);
if($count > 0)
{
echo "You are an admin!";
}



Which returns the number of rows selected by your query. So if there is a record with that username and where admin=1, it'll return at least 1 row. If not, it'll return 0.
Was This Post Helpful? 1
  • +
  • -

#3 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 10:51 AM

Thank you. Your help was appreciated. :]
Was This Post Helpful? 0
  • +
  • -

#4 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 10:57 AM

EDIT:
Disregard that.
Now I have an error..
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a1342013/public_html/workdesk.php on line 35

Was This Post Helpful? 0
  • +
  • -

#5 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:12 AM

Probably a problem with your query then. DO:

$result = mysql_query($sql, $conn) or die('Error: ' . mysql_error());



And see what error message you get.
Was This Post Helpful? 1
  • +
  • -

#6 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:23 AM

Thanks Duckington, it gave me an error now. So from what I'm seeing, it doesn't allow email syntax (@example.com).

rror: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com' at line 3


Sorry if I seem a bit clueless ;).
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:37 AM

what does the SQL string look like?

(I know that you donít have this problem with Prepared Statements)
Was This Post Helpful? 0
  • +
  • -

#8 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:44 AM

^I have already shown this in the first post, but here it is again:

			$sql = 
			"SELECT * FROM PREREG
			WHERE admin=1 
			AND emaad='$user1'"; 

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:46 AM

I meant the string with $user1 replaced
Was This Post Helpful? 0
  • +
  • -

#10 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:54 AM

Oh haha. Excuse me.

Well, it depends on the user. Mine is: perkinsbeau@gmail.com. That is what I registered with. So as I said, I guess it doesn't support @email.com
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:55 AM

Take was Duckington gave you:

$result = mysql_query($sql, $conn) or die('Error: ' . mysql_error());



and change it to this:

$result = mysql_query($sql, $conn) or die('Query ' . $sql . ' failed; Error: ' . mysql_error());


So you can see what exactly you're passing as a query to SQL server.

PHP/MySQL Debugging 101. If a query is failing, and you're building the query yourself from variables, figure out what it is you're sending the SQL server!
Was This Post Helpful? 1
  • +
  • -

#12 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: PHP/mySQL Table Access. No Error, but not the correct response.

Posted 23 December 2011 - 11:59 AM

*Facepalm*
Thank you JackOfAllTrades!
I realized that I typed in the wrong form. For some idiotic reason I typed 'emaad' instead of 'emailadd' for the column in the mySQL base.

Also thank you to all who contributed to this thread, you guys are awesome.

[SOLVED]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1