Trouble with PHP MySQL code

  • ben_sigma
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 12, 2008
  • Posts: 58
  • Loc: South Australia
  • Status: Offline

Post August 3rd, 2008, 6:23 pm

I am having trouble with some code and i haven't had this problem before. i cant figure out the problem.

Code: [ Download ] [ Select ]
<?php
//check user against global database

$email=$_GET['email'];
$password=$_GET['password'];

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("users", $con);

$result = mysql_query("SELECT * FROM users WHERE email='".$email."'");

while($row = mysql_fetch_array($result))
{
    $username=$row['username'];
}
mysql_close($con);

//check user against individual database

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($username, $con);

$result = mysql_query("SELECT * FROM details WHERE password='".$password."'");

while($row = mysql_fetch_array($result))
{
echo "Logged in";
}
mysql_close($con);

?>
  1. <?php
  2. //check user against global database
  3. $email=$_GET['email'];
  4. $password=$_GET['password'];
  5. $con = mysql_connect("localhost","root","");
  6. if (!$con)
  7. {
  8. die('Could not connect: ' . mysql_error());
  9. }
  10. mysql_select_db("users", $con);
  11. $result = mysql_query("SELECT * FROM users WHERE email='".$email."'");
  12. while($row = mysql_fetch_array($result))
  13. {
  14.     $username=$row['username'];
  15. }
  16. mysql_close($con);
  17. //check user against individual database
  18. $con = mysql_connect("localhost","root","");
  19. if (!$con)
  20. {
  21. die('Could not connect: ' . mysql_error());
  22. }
  23. mysql_select_db($username, $con);
  24. $result = mysql_query("SELECT * FROM details WHERE password='".$password."'");
  25. while($row = mysql_fetch_array($result))
  26. {
  27. echo "Logged in";
  28. }
  29. mysql_close($con);
  30. ?>


Thanks in advance
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 3rd, 2008, 6:23 pm

  • ben_sigma
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 12, 2008
  • Posts: 58
  • Loc: South Australia
  • Status: Offline

Post August 3rd, 2008, 6:26 pm

Some more info, all the database, table and field names are existing and correct. all the fields contain test data as well.
  • spork
  • HB
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 5488
  • Loc: Rochester, NY
  • Status: Offline

Post August 3rd, 2008, 6:44 pm

What's the problem?
How to Maintain Simple, Static Pages in a CakePHP Application
EEEEEEEEE! It's here!!
  • ben_sigma
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 12, 2008
  • Posts: 58
  • Loc: South Australia
  • Status: Offline

Post August 3rd, 2008, 6:58 pm

Oh crud, i can be a (richard cranium)sometimes.

Code: [ Download ] [ Select ]
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\partyprowl.com\includes\login.php on line 35


Sorry
  • spork
  • HB
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 5488
  • Loc: Rochester, NY
  • Status: Offline

Post August 3rd, 2008, 7:08 pm

Richard Cranium :lol:

There's probably something wrong with your SQL query. Try replacing this
PHP Code: [ Download ] [ Select ]
$result = mysql_query("SELECT * FROM users WHERE email='".$email."'");

with this
PHP Code: [ Download ] [ Select ]
$result = mysql_query("SELECT * FROM users WHERE email='".$email."'");
 
if( $result === false ) {
   die(mysql_error());
}
  1. $result = mysql_query("SELECT * FROM users WHERE email='".$email."'");
  2.  
  3. if( $result === false ) {
  4.    die(mysql_error());
  5. }


That should spit out any MySQL errors that happen during the query and provide some insight into the problem.
How to Maintain Simple, Static Pages in a CakePHP Application
EEEEEEEEE! It's here!!
  • ben_sigma
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 12, 2008
  • Posts: 58
  • Loc: South Australia
  • Status: Offline

Post August 3rd, 2008, 7:14 pm

I just tried that

Code: [ Download ] [ Select ]
<?php
//check user against global database

$email=$_GET['email'];
$password=$_GET['password'];

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("users", $con);

$result = mysql_query("SELECT * FROM users WHERE email='".$email."'");

if( $result === false ) {
    die(mysql_error());
}

while($row = mysql_fetch_array($result))
{
    $username=$row['username'];
}
mysql_close($con);

//check user against individual database

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($username, $con);

$result = mysql_query("SELECT * FROM details WHERE password='".$password."'");

while($row = mysql_fetch_array($result))
{
echo "Logged in";
}
mysql_close($con);

?>
  1. <?php
  2. //check user against global database
  3. $email=$_GET['email'];
  4. $password=$_GET['password'];
  5. $con = mysql_connect("localhost","root","");
  6. if (!$con)
  7. {
  8. die('Could not connect: ' . mysql_error());
  9. }
  10. mysql_select_db("users", $con);
  11. $result = mysql_query("SELECT * FROM users WHERE email='".$email."'");
  12. if( $result === false ) {
  13.     die(mysql_error());
  14. }
  15. while($row = mysql_fetch_array($result))
  16. {
  17.     $username=$row['username'];
  18. }
  19. mysql_close($con);
  20. //check user against individual database
  21. $con = mysql_connect("localhost","root","");
  22. if (!$con)
  23. {
  24. die('Could not connect: ' . mysql_error());
  25. }
  26. mysql_select_db($username, $con);
  27. $result = mysql_query("SELECT * FROM details WHERE password='".$password."'");
  28. while($row = mysql_fetch_array($result))
  29. {
  30. echo "Logged in";
  31. }
  32. mysql_close($con);
  33. ?>


I got the same error.

Code: [ Download ] [ Select ]
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\partyprowl.com\includes\login.php on line 39
  • ben_sigma
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 12, 2008
  • Posts: 58
  • Loc: South Australia
  • Status: Offline

Post August 3rd, 2008, 8:40 pm

Thanks for the help i figured it out...i was using post in the form to send the data and $_GET in the php to catch it. So there was no data. I should have been using $_POST.
Thanks for you help tho. :D
  • spork
  • HB
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 5488
  • Loc: Rochester, NY
  • Status: Offline

Post August 3rd, 2008, 9:25 pm

No problem, glad you got it figured it out. :thumbsup:
How to Maintain Simple, Static Pages in a CakePHP Application
EEEEEEEEE! It's here!!

Post August 4th, 2008, 12:11 am

Just a quick thing though ... it seems like that thing might log the wrong people in ... because this part:
Code: [ Download ] [ Select ]
$result = mysql_query("SELECT * FROM details WHERE password='".$password."'");
while($row = mysql_fetch_array($result))
{
echo "Logged in";
}
mysql_close($con);
  1. $result = mysql_query("SELECT * FROM details WHERE password='".$password."'");
  2. while($row = mysql_fetch_array($result))
  3. {
  4. echo "Logged in";
  5. }
  6. mysql_close($con);

chooses where the password is equal to the one typed in, but I could just type in a wrong password, and maybe I used someone else's password ... Or it could return more than one row ... and it's just going to log that user in either way ...

I would just rather change the query to something like:
Code: [ Download ] [ Select ]
"SELECT * FROM details WHERE password='".$password."' AND username='".$username."'"

That way if it does return a row it is the correct one ...
RewriteEngine On

RewriteRule ^(awesome|excellent|extraordinary)$ RT
  • ben_sigma
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 12, 2008
  • Posts: 58
  • Loc: South Australia
  • Status: Offline

Post August 7th, 2008, 5:22 pm

Um yeah it checks the database for a user match first and if the user exists it then checks the database for a matching password. It had to be set up this way because each user has their own database. So for it to work it checks the user name on a global database and if the user exists it checks their password on their user specific database.
Thanks tho.

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 357 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.