yogibear Posted January 23, 2007 Share Posted January 23, 2007 Hi I have been trying to get a check on a username field so that the username is uniquethis is what i have but im getting errors[code]<?php$con = mysql_connect('localhost','web22-james','***');if (!$con)$username = $_POST['username']; $email = $_POST['email']; $pword1 = $_POST['pword1']; $pword2 = $_POST['pword2']; $query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1"; // check if username already $result = mysql_query($query); if(0 != mysql_num_rows($result)) echo "Our database shows this username has already been registered. Please choose another username."; } else { echo "WORKING!"; } mysql_close();?> [/code]any ideas what is the problemmany thanksyogi Quote Link to comment Share on other sites More sharing options...
c4onastick Posted January 23, 2007 Share Posted January 23, 2007 You need to bracket that first 'if', for starters:[code]<?php$con = mysql_connect('localhost','web22-james','stash');if (!$con) { die("Connection failed.");}else{ $username = $_POST['username']; $email = $_POST['email']; $pword1 = $_POST['pword1']; $pword2 = $_POST['pword2']; $query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1"; // check if username already $result = mysql_query($query); if(0 != mysql_num_rows($result)) echo "Our database shows this username has already been registered. Please choose another username."; } else { echo "WORKING!"; } mysql_close();}?> [/code]What kind of errors are you getting? Post them, we'll help you sort them out. Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 23, 2007 Author Share Posted January 23, 2007 Hi this is the errorParse error: syntax error, unexpected T_ELSE in /home/sites/***/public_html/registerconfirm.php on line 16many thanksyogibear Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 You're missing a { here:[code]if(0 != mysql_num_rows($result)) [b]{[/b][/code]And that's assuming you adapted c4onastick's changes also as that open 'if' statement will produce another error.If statements always have code to run and that code is between curly's:if ($this_does_not_work == true){ echo "This does not work";} Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 23, 2007 Author Share Posted January 23, 2007 hi im getting a error on line 14 now[code]<?php$con = mysql_connect('localhost','***','***');if (!$con) { die("Connection failed.");}else{ $username = $_POST['username']; $email = $_POST['email']; $pword1 = $_POST['pword1']; $pword2 = $_POST['pword2']; $query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1"; // check if username already $result = mysql_query($query); if(0 != mysql_num_rows($result)) [b]{[/b] echo "Our database shows this username has already been registered. Please choose another username."; } else { echo "WORKING!"; } mysql_close();}?> [/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 This is why you don't just copy and paste code without understanding it. [b] isn't php, that's a bold tag. Take that stuff out, and in the future POST the error, not just that you got one. Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 23, 2007 Author Share Posted January 23, 2007 [quote author=jesirose link=topic=123684.msg511547#msg511547 date=1169576103]This is why you don't just copy and paste code without understanding it. [b] isn't php, that's a bold tag. Take that stuff out, and in the future POST the error, not just that you got one.[/quote]hi i did post the error it was in my second post and i also tried it with and without the bold tag i didnt just Copy and Paste but that is why people get help because they dont understandthanks for help Quote Link to comment Share on other sites More sharing options...
marcus Posted January 23, 2007 Share Posted January 23, 2007 [quote author=yogibear link=topic=123684.msg511554#msg511554 date=1169576651][quote author=jesirose link=topic=123684.msg511547#msg511547 date=1169576103]This is why you don't just copy and paste code without understanding it. [b] isn't php, that's a bold tag. Take that stuff out, and in the future POST the error, not just that you got one.[/quote]hi i did post the error it was in my second post and i also tried it with and without the bold tag i didnt just Copy and Paste but that is why people get help because they dont understandthanks for help[/quote]Posting the error would be like the error you posted before ERRORNAME on LINE ### on FILENAME Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 23, 2007 Author Share Posted January 23, 2007 This is the code[code]<?php$con = mysql_connect('localhost','***','***');if (!$con) { die("Connection failed.");}else{ $username = $_POST['username']; $email = $_POST['email']; $pword1 = $_POST['pword1']; $pword2 = $_POST['pword2']; $query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1"; // check if username already $result = mysql_query($query); if(0 != mysql_num_rows($result)) { echo "Our database shows this username has already been registered. Please choose another username."; } else { echo "WORKING!"; } mysql_close();}?> [/code]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sites/***/public_html/registerconfirm.php on line 14Many thanksyogi Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 I was gonna say, personally i've not seen that 'if' statement written in that format. Here's what I use;$num_rows = mysql_num_rows($result);if ($num_rows => 0) { echo "Our database shows this username has already been registered. Please choose another username.";} else { echo "WORKING!";}Also, your mysql connection is missing one vital ingredient. The database name:mysql_select_db("database_name"); Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 Indeed, you need to select a db - do$result = mysql_query($query) OR die(mysql_error());This will show you when you have an SQL error. It would tell you something about no database. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 23, 2007 Share Posted January 23, 2007 The error you are receiving indicates that the previous mysql_query did not work correctly, change this line:[code]<?php$result = mysql_query($query);?>[/code]to[code]<?php$result = mysql_query($query) or die("Problem with the query:<pre>$query</pre><br>" . mysql_error());?>[/code]The output should give you a hint as to why the query didn't work correctly. If not post the output here.Ken Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 23, 2007 Author Share Posted January 23, 2007 Hi this is my code i have no idea about it i have changed removed and added bits and now im lost with it. [code]<?php$con = mysql_connect('localhost','***','***');mysql_select_db("web22-james");if (!$con) { die("Connection failed.");}else{ $username = $_POST['username']; $email = $_POST['email']; $pword1 = $_POST['pword1']; $pword2 = $_POST['pword2']; $query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1";$result = mysql_query($query) or die("Problem with the query:<pre>$query</pre><br>" . mysql_error()); $num_rows = mysql_num_rows($result);if ($num_rows => 0) { echo "Our database shows this username has already been registered. Please choose another username.";} else { echo "$results";} mysql_close();?>[/code] this is the html form[code]<form action="registerconfirm.php" method="POST">Username: <input type="text" name="username" maxlength="16" /><br>E-mail: <input type="text" name="email" /><br>Password: <input type="text" name="pword1"><br>Password (again): <input type="text" name="pword2"><br><input type="submit" value="Register"></form>[/code]when i click submit it says Parse error: syntax error, unexpected $end in /home/sites/***/public_html/registerconfirm.php on line 25 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 if ($num_rows => 0) {=> is syntax for assigning array values. You want >= Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 23, 2007 Author Share Posted January 23, 2007 [quote author=jesirose link=topic=123684.msg511609#msg511609 date=1169579762]if ($num_rows => 0) {=> is syntax for assigning array values. You want >=[/quote]I'm still getting the same errorthanksyogi Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 You're missing a closing } to your first 'else' statement. Put it just before your closing ?> php tag. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 Thanks, jesirose.. my fingers got ahead of me :) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 23, 2007 Share Posted January 23, 2007 You're missing the terminating "}" on the else statement:[code]<?phpif (!$con) { die("Connection failed.");}else{?>[/code]Put the "}" just before the "?>" at the end of your script.Ken Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 [quote author=yogibear link=topic=123684.msg511611#msg511611 date=1169579981][quote author=jesirose link=topic=123684.msg511609#msg511609 date=1169579762]if ($num_rows => 0) {=> is syntax for assigning array values. You want >=[/quote]I'm still getting the same errorthanksyogi[/quote]Actually, I think you'd just want > - you don't want it to go if it's 0, just if it's > 0 Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 he could also useif ($num_rows == 1) { Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 23, 2007 Author Share Posted January 23, 2007 [quote author=jesirose link=topic=123684.msg511617#msg511617 date=1169580331][quote author=yogibear link=topic=123684.msg511611#msg511611 date=1169579981][quote author=jesirose link=topic=123684.msg511609#msg511609 date=1169579762]if ($num_rows => 0) {=> is syntax for assigning array values. You want >=[/quote]I'm still getting the same errorthanksyogi[/quote]Actually, I think you'd just want > - you don't want it to go if it's 0, just if it's > 0[/quote]IT WORKS THANK YOU ;D simcoweb got it going but it always said it was in the database. jesirose fixed the last problem many thanks everyone Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.