0

here I am once again! I've come accross a rather annoying issue, and i'm pretty sure I'm not doing it wrong.

When I try to check if an username already exists in the MYSQL Database via PHP, it nags about the string being a boolean and not a variable.

This is the PHP code:

if (isset($_POST['Submit1'])) 
{
    $username = $_POST['username'];
    if(IsNameInUse($username)) header("Location: cselect.php?exist");
}
else if (isset($_POST['Submit2'])) 
{
    $username = $_POST['username'];
    if(IsNameInUse($username)) header("Location: cselect.php?exist");
}
else if (isset($_POST['Submit3'])) 
{
    $username = $_POST['username'];
    if(IsNameInUse($username)) header("Location: cselect.php?exist");
}

Here is the "IsNameInUse" from my functions file:

function IsNameInUse($username)
{
    $username = mysql_real_escape_string($username);
    $query = mysql_query("SELECT COUNT(ID) FROM Accounts WHERE Username = '$username'");
    return (mysql_result($query, 0) == 1) ? true : false;
}

I've already performed an: "echo $username;" and this clearly shows that a string is set as $username. It clearly gave me the string I entered. Now, I honestly have no idea why its giving the following error:

[error] PHP Warning:  mysql_result() expects parameter 1 to be resource, boolean given in /var/zpanel/hostdata/paradise/public_html/paradiseroleplay_com/includes/functions.php on line 7

So here I am again, asking the never ending knowledge of the internet. Regards, Heartfire.

EDIT Got it fixed. "Sorry for this. The die on mysql error, clearly showed how stupid I can be. I forgot that for Accounts, I made it "AccID" and not "ID"... Thanks a bunch, got it fixed now." And about the DTO, good point -- Will do.

Heartfire
  • 23
  • 1
  • 7
  • put an `or die(mysql_error())` on the `mysql_query` to see whats up, anyway, don't use mysql anyway – Kevin Sep 23 '14 at 09:02
  • What @Ghost means is that you should be using [`PDO`](http://php.net/manual/en/book.pdo.php) or [`mysqli`](http://php.net/manual/en/mysqli.quickstart.php). The original mysql functions are old an in some cases insecure. – max Sep 23 '14 at 09:07
  • Sorry for this. The die on mysql error, clearly showed how stupid I can be. I forgot that for Accounts, I made it "AccID" and not "ID"... Thanks a bunch, got it fixed now. – Heartfire Sep 23 '14 at 09:08

1 Answers1

0

The problem is that in functions.php on line 7 (could you post this line also) php want's the resource to the database.

e.g. $link would be the resource is this case:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

Also you are running mysql functions inside a function, please read this post for more info: How to put mysql inside a php function?

Hope this will help. If not please provide the lines of functions.php

Community
  • 1
  • 1
Erwin van Hoof
  • 192
  • 1
  • 9