Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 4

whats wrong with this php code?

I am using this php code for a login script for a game i am making. it was working earlier but i am following a tutorial and since the last step this hasnt worked... stupidly i didnt save it before adding the last step....

if anyone could tell me what is wrong with this code it would be much appreciated

<?php

$username = $_POST['username'];

$password = $_POST['password'];

if ($username&&$password)

{

$connect = mysql_connect("mysql11.000webhost.com","a4264749_members","billyjoe16")

or die("could not connect to database");

mysql_select_db("a4264749_login") or die ("couldn't find database");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

$numrows = mysql_num_rows ($query);

if ($numrows!=0)

}

{

else

die ("That user does not exist");

}

{

else

die ("Please enter a username and password");

}

?>

Just so you know there is nothing wrong with the user credentials that dissapear off the corner of the page with a "......"

3 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    Try this: Make sure the mysql_connect is proper

    $connect = mysql_connect('host','username','password');

    <?php

    $username = $_POST['username'];

    $password = $_POST['password'];

    if ($username&&$password)

    {

    $connect = mysql_connect('host','username','password');

    if(!$connect)

    die("could not connect to database");

    mysql_select_db("a4264749_login") or die ("couldn't find database");

    $query = mysql_query("SELECT * FROM users WHERE username='$username'");

    $numrows = mysql_num_rows ($query);

    if ($numrows!=0){

    }

    else {

    die ("That user does not exist");

    }

    }

    else

    {

    die ("Please enter a username and password");

    }

    ?>

  • Anonymous
    1 decade ago

    It would help to know how it failed. If it was script error then give us the error message and we can really help you out. I'm guessing that your code just doesn't do what you want it to but runs?

    If this is the case try these solutions:

    1) In your SQL place ` yes ` not " or ' around your table names like this.

    $query = mysql_query("SELECT * FROM `users` WHERE `username` = '$username';");

    Note: I added a semi-colon in that code as well, that's just habit for me.

    2) This code is wrong, you started it with the brackets facing the wrong way:

    if ($numrows!=0)

    }

    {

    Should be this:

    if ($numrows!=0)

    {

    }

    Put it on one line like so to easily see this: if ($numrows!=0) { <-open then close->}

    Keep up the good work though. You might want to check out some free PHP tutorials here: http://www.phpacademy.org/

    Source(s): Experience
  • ?
    Lv 4
    1 decade ago

    it might be curly brackets problem in your code. they need to be placed correctly. On more tips, be careful with SQL injection with your code

    good luck

Still have questions? Get your answers by asking now.