Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

If you have two seconds... PHP question

  • 28-04-2009 01:02PM
    #1
    Closed Accounts Posts: 24


    Lads,

    would really appreciate it if one of you Jedis could take a look at this script and tell me what I've done wrong.

    I've only started to learn php recently, and this is the first basic script I've put together. It's (supposed to be) a registration page which writes user information to a database after some validation.

    I'm getting an error on Line 11 when all info is valid:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blahblahblah/blahblahblah/test/register.php on line 11

    Full script attached... It's doing my head in.


Comments

  • Closed Accounts Posts: 176 ✭✭elyod


    EDIT: See post below, but this is also useful.

    Your SQL probably failed for some reason.

    I like to do it like this
    $sql = "SELECT * FROM myTable";
    $result = mysql_query($sql) or die("<b>A MySQL error occured</b><br />Error: " . mysql_error());
    //etc
    //etc
    

    This will give you far more information if something goes wrong and makes debugging a lot easier.

    Also useful for nearly all MySQL interactions.
    mysql_connect("xxxx", "xxxx", "xxxx") or die("<b>A MySQL error occured</b><br />Error: " . mysql_error());
    mysql_select_db("users") or die("<b>A MySQL error occured</b><br />Error: " . mysql_error());
    


  • Closed Accounts Posts: 176 ✭✭elyod


    Actually the reason you're getting that error is because you are passing the STRING "$query" rather than the actual variable $query to the mysql_num_rows() function. Just remove the quotation marks in line 11.
    if(mysql_num_rows("$query")){
    
    to this
    if(mysql_num_rows($query)){
    


  • Closed Accounts Posts: 24 Johnny909


    I noticed that with the quotation marks since my initial post actually, but it's still giving the same error.

    Is there anything wrong with the SQL Query i'm using:
    $query = mysql_query("SELECT 'id' FROM 'users' WHERE 'user'='".$_POST['user']."' LIMIT 1");
    


  • Closed Accounts Posts: 176 ✭✭elyod


    Try
    $query = mysql_query("SELECT id FROM users WHERE user='$_POST[user]') or die("<b>A MySQL error occured</b><br />Error: " . mysql_error());
    


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    give this a go
    [php]
    <?php
    mysql_connect("xxxx", "xxxx", "xxxx");
    mysql_select_db("users");

    if(count($_POST)>0){

    //USERNAME VALIDATION...
    $_POST=trim($_POST);
    if($_POST && strlen($_POST)>=3){
    $query = mysql_query("SELECT id FROM users WHERE user='".addslashes($_POST)."' LIMIT 0,1");
    if(mysql_num_rows($query)){
    $error='Username already exists!';
    }
    } else{
    $error='Username must contain 3 or more characters';
    }

    //PASSWORD VALIDATION...
    if($_POST && $_POST){
    if(strlen($_POST)>=6){
    if($_POST != $_POST){
    $error='Passwords don\'t match!';
    }
    else{
    $error='Please enter password twice';
    }
    }
    else{
    $error='Password must be at least 6 characters';
    }
    }
    else{
    $error='Password must be at least 6 characters';
    }

    //EMAIL VALIDATION...
    if($_POST){
    if(!eregi("^[a-zA-Z0-9]+[a-zA-Z0-9_.-]*@[a-zA-Z0-9]+[a-zA-Z0-9_.-])*\.[a-z]{2,4}$", $_POST)){
    $error='Email is invalid';
    }
    else{
    $error='Please enter your email address';
    }
    }
    //NAME VALIDATION...
    if(!$_POST){
    $error='Please enter your first name';
    }
    if(!$_POST){
    $error='Please enter your surname';
    }
    }

    //IF the form has been submitted with no errors, insert into database. ELSE continue to display form...
    if(empty($error) && count($_POST)>0){
    $query=mysql_query("INSERT INTO users (user, pass, firstname, surname, email) VALUES ('".$_POST."','".md5($_POST)."','".$_POST."','".$_POST."','".$_POST."')");
    if($query){
    echo $_POST.' is now registered';
    }
    }
    else{
    ?>

    <form name="registration" method="POST">
    <div align="center">
    <table width="802" border="0" cellspacing="8" cellpadding="8" summary="Login Form">
    <tr>
    <td width="116"><label for="user">Username</label></td>
    <td width="194"><input type="text" name="user" id="user" /></td>
    <td width="412"><b><font color=red><?php echo $error; echo $error; ?></font></b></td>
    </tr>
    <tr>
    <td><label for="pass">Password</label></td>
    <td><input type="password" name="pass1" id="pass1" /></td>
    </tr>
    <tr>
    <td><label for="pass2">Re-Enter Password</label></td>
    <td><input type="password" name="pass2" id="pass2" /></td>
    <td width="412"><b><font color=red><?php echo $error; echo $error; ?></font></b></td>
    </tr>
    <tr>
    <td><label for="firstname">First Name</label></td>
    <td><input type="text" name="firstname" id="firstname" /></td>
    <td width="412"><b><font color=red><?php echo $error; ?></font></b></td>
    </tr>
    <tr>
    <td><label for="surname">Surname</label></td>
    <td><input type="text" name="surname" id="surname" /></td>
    <td width="412"><b><font color=red><?php echo $error; ?></font></b></td>
    </tr>
    <tr>
    <td><label for="email">Email Address</label></td>
    <td><input type="text" name="email" id="email" /></td>
    <td width="412"><b><font color=red><?php echo $error; echo $error; ?></font></b></td>
    </tr>
    <tr>
    <td colspan="2">
    <div align="center">
    <input type="Submit" name="submit" value="Register" />
    </div></td>
    </table>
    </div>
    </form>
    <?php
    }
    ?>
    [/php]


  • Advertisement
  • Closed Accounts Posts: 24 Johnny909


    elyod wrote: »
    Try
    $query = mysql_query("SELECT id FROM users WHERE user='$_POST[user]') or die("<b>A MySQL error occured</b><br />Error: " . mysql_error());
    

    Deadly - that threw up an error message that told me I hadn't selected the correct database! :rolleyes:

    Now there's another problem, but hopefully i'll be able to fix that myself.

    Thanks for your help!


  • Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭Chardee MacDennis


    i use phpMyAdmin to test my queries if i cant figure out whats going on...


Advertisement