3

I'm fairly new to PHP/MySQL and I seem to be having a newbie issue.

The following code keeps throwing me errors no matter what I change, and I have a feeling it's got to be somewhere in the syntax that I'm messing up with. It all worked at home 'localhost' but now that I'm trying to host it online it seems to be much more temperamental with spaces and whatnot.

It's a simple login system, problem code is as follows:

<?php
session_start();
require 'connect.php';
echo "Test";

//Hash passwords using MD5 hash (32bit string).
$username=($_POST['username']);
$password=MD5($_POST['password']);

//Get required information from admin_logins table
$sql=mysql_query("SELECT * FROM admin_logins WHERE Username='$username' ");
$row=mysql_fetch_array($sql);

//Check that entered username is valid by checking returned UserID
if($row['UserID'] === NULL){
    header("Location: ../adminlogin.php?errCode=UserFail");
}
//Where username is correct, check corresponding password
else if ($row['UserID'] != NULL && $row['Password'] != $password){
    header("Location: ../adminlogin.php?errCode=PassFail");
}
else{
    $_SESSION['isAdmin'] = true;
    header("Location: ../admincontrols.php");
}

mysql_close($con);

?>

The test is just in there, so I know why the page is throwing an error, which is:

`Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in 'THISPAGE' on line 12`

It seems to dislike my SQL query.

Any help is much appreciated.

EDIT:

connect.php page is:

<?php
$con = mysql_connect("localhost","username","password");
if(!$con) {
    die('Could not connect: ' . mysql_error());
}
    mysql_select_db("dbname", $con);
?>

and yes it is mysql_*, LOL, I'll get to fix that too.

Community
  • 1
  • 1
Sean Missingham
  • 926
  • 1
  • 7
  • 19
  • 2
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799). Instead you should learn about prepared statements and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you care to learn, [here is a quite good PDO-related tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – DCoder Sep 09 '12 at 10:05
  • 1
    You don't actually seem to *make* the connection anywhere to the database in your code. Is it somewhere else? – Fluffeh Sep 09 '12 at 10:07
  • I think it's done in `require 'connect.php';` – Ilia Ross Sep 09 '12 at 10:18
  • What is in `connect.php` particularly? (Just replace your username and password to database ;) ) – Ilia Ross Sep 09 '12 at 10:31
  • I've updated my answer, please check! – Ilia Ross Sep 09 '12 at 11:02
  • I think I have unraveled your problem. I've updated my answer, please check! – Ilia Ross Sep 09 '12 at 11:19
  • Oops. Sorry. In your case it is an array. When I tested it, I turned mine in to a single row.. – Ilia Ross Sep 09 '12 at 11:28
  • tried the " to ' thing, no dice. tried to print/echo variables: $sql and $row, neither did a thing. commenting out the mysql_fetch_array() line got rid of the error though, page just went nowhere. Ill just rewrite the whole page tomorrow with PDO when ive got a fresh brain, bit tired tonight. Thanks for trying though, hopefully i havent created one of those annoying recurring problems that gets stuck in your head! – Sean Missingham Sep 09 '12 at 11:31
  • @SeanMissingham As an off-topic comment, I strongly suggest you to NOT use md5. Read this article: http://crackstation.net/hashing-security.htm – Renato Massaro Sep 09 '12 at 11:32
  • hashing was going to be my next area of investigation, thanks for the early advice and time saving! – Sean Missingham Sep 09 '12 at 11:42

2 Answers2

3

You should escape column name username using backtick, try

SELECT * 
FROM admin_logins 
WHERE `Username` = '$username'

You're code is prone to SQL Injection. Use PDO or MYSQLI

Example of using PDO extension:

<?php
$stmt = $dbh->prepare("SELECT * FROM admin_logins  WHERE `Username` = ?");
$stmt->bindParam(1, $username);
if ($stmt->execute(array($_GET['name']))) {
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
}
?>
John Woo
  • 258,903
  • 69
  • 498
  • 492
2

Sean, you have to use dots around your variable, like this:

$sql = mysql_query("SELECT * FROM admin_logins WHERE Username = '". mysql_real_escape_string($username)."' ");

If you use your code just like this then it's vulnerable for SQL Injection. I would strongly recommend using mysql_real_escape_string as you insert data into your database to prevent SQL injections, as a quick solution or better use PDO or MySQLi.

Besides if you use mysql_* to connect to your database, then I'd recommend reading the PHP manual chapter on the mysql_* functions, where they point out, that this extension is not recommended for writing new code. Instead, they say, you should use either the MySQLi or PDO_MySQL extension.

EDITED:

I also checked your mysql_connect and found a weird regularity which is - if you use " on mysql_connect arguments, then it fails to connect and in my case, when I was testing it for you, it happened just described way, so, please try this instead:

$con = mysql_connect('localhost','username','password');

Try to replace " to ' as it's shown in the PHP Manual examples and it will work, I think!

If it still doesn't work just print $row, with print_r($row); right after $sql=mysql_query() and see what you have on $row array or variable.

Community
  • 1
  • 1
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • 1
    Variables within double quotes should work fine within a string as the OP has. I do agree with the rest of the answer though moving on to PDO or MYSQLI – Fluffeh Sep 09 '12 at 10:08
  • dots dont seem to help unfortunately, but Ill just go and read up on PDO anyway, I didnt realise there was an alternative. Thanks :) – Sean Missingham Sep 09 '12 at 10:19
  • You are very welcome! This is the right call to use PDO. Please tell me what else doesn't work? – Ilia Ross Sep 09 '12 at 10:21
  • tried concatenation, changing up all the double quotes to singles and all that fun stuff, tried removing all spaces, just no luck, ill have to rack through all the pages and see if its somewhere else :/ – Sean Missingham Sep 09 '12 at 10:27
  • are you sure you don't call `session_start()` twice? – Ilia Ross Sep 09 '12 at 10:29