TheSky Posted May 25, 2011 Share Posted May 25, 2011 is this part of code correct ? $query = mysql_query("select * from username WHERE username='$username'"); if(mysql_query($query) > 0) { die("Username already in use."); } else { Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/ Share on other sites More sharing options...
Pikachu2000 Posted May 25, 2011 Share Posted May 25, 2011 Probably not. What is it supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220020 Share on other sites More sharing options...
TheSky Posted May 25, 2011 Author Share Posted May 25, 2011 if username exist continue else exist die Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220021 Share on other sites More sharing options...
TheSky Posted May 25, 2011 Author Share Posted May 25, 2011 this is mysql part of my php code <? $query=("select * from username WHERE username='$username'"); if(@mysql_query($query) > 0) { die("Username already in use."); } else { //Add data //NOW() $sql=( "INSERT INTO u_data (id,username,status) VALUES ('NULL','$username','$as')"); if (@mysql_query($sql)) { echo('<p>Your information has been Succesfuly added</p>'); } else { echo('<p>There was error adding information</p>'); } mysql_close(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220025 Share on other sites More sharing options...
TOA Posted May 25, 2011 Share Posted May 25, 2011 What happens when you run it? My guess is nothing because you have errors turned off In your first if statement, your checking if a resource_ID is greater than 0 and you need to be checking the num_rows returned from the query. replace this: if(@mysql_query($query) > 0) { With this: $res=@mysql_query($query); if (mysql_num_rows($res) > 0 ) { I think that's what you're looking for, if I read your post right. Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220032 Share on other sites More sharing options...
Maq Posted May 25, 2011 Share Posted May 25, 2011 if username exist continue else exist die So what happens? Do you see any errors? Provide more information. - You shouldn't be using the mysql_query return value (resource or FALSE) to check how many rows are returned. Use mysql_num_rows. - You shouldn't be using the '@' suppressor symbol. Fix your errors & warnings. - If the username is already in use, don't kill the script by using die. Handle it properly, maybe give them another chance to pick a name. Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220034 Share on other sites More sharing options...
TheSky Posted May 25, 2011 Author Share Posted May 25, 2011 What happens when you run it? My guess is nothing because you have errors turned off In your first if statement, your checking if a resource_ID is greater than 0 and you need to be checking the num_rows returned from the query. replace this: if(@mysql_query($query) > 0) { With this: $res=@mysql_query($query); if (mysql_num_rows($res) > 0 ) { I think that's what you're looking for, if I read your post right. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a9953296/public_html/add_user.php on line 38 no errors, it add's data (in database user exists) so it adds new id agen but it should show error and kill the code Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220041 Share on other sites More sharing options...
Maq Posted May 25, 2011 Share Posted May 25, 2011 Your query is probably failing. Remove the '@' & change this line to: $res=mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220043 Share on other sites More sharing options...
TheSky Posted May 25, 2011 Author Share Posted May 25, 2011 hmm ok thanks i got my error mistake $query=("select * from u_data WHERE username='$username'"); //$res=mysql_query($query) or die(mysql_error()); if(mysql_query($query) > 0) { die("Username already in use."); } else { but now it shows only "Username already in use." hmm my brain is totaly crached :-\ Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220052 Share on other sites More sharing options...
Maq Posted May 25, 2011 Share Posted May 25, 2011 Where is $username defined? Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220056 Share on other sites More sharing options...
TheSky Posted May 25, 2011 Author Share Posted May 25, 2011 from cookie $username=$_COOKIE["username"]; username is correct becouse it adds in database (there is username value) Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220057 Share on other sites More sharing options...
Psycho Posted May 25, 2011 Share Posted May 25, 2011 hmm ok thanks i got my error mistake $query=("select * from u_data WHERE username='$username'"); //$res=mysql_query($query) or die(mysql_error()); if(mysql_query($query) > 0) { die("Username already in use."); } else { but now it shows only "Username already in use." hmm my brain is totaly crached :-\ I really try to refrain from disrespecting people (well, usually) but you really need to pull your head out. That is the same invalid code you posted originally. You have been told several times in this post what the problem is and how to fix it, but you go back to what you originally had. //Create a query string $query = "SELECT * FROM u_data WHERE username='$username'"; //Run the query and put RESULT REFERENCE ID into variable $result = mysql_query($query) or die(mysql_error()); //Check if there are any rows in the RESULT REFERENCE ID if(mysql_num_rows($result)>0) { //User exists echo "Username already in use."; //Add proper error handling } else { //User name does not exist - continue } Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220059 Share on other sites More sharing options...
Maq Posted May 25, 2011 Share Posted May 25, 2011 Comments in the code: $query="select * from u_data WHERE username='$username'"; echo "QUERY-> " . $query; //Echo out the string query value $res=mysql_query($query) or die(mysql_error()); //die and show error, if query is invalid if(mysql_num_rows($res) > 0) //retrieve number of results returned from query { die("Username already in use."); //you need to handle this properly & not use die() } EDIT: mjd beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220061 Share on other sites More sharing options...
TheSky Posted May 25, 2011 Author Share Posted May 25, 2011 thanks for all who was helping me im realy sorry for any problems i have made //Create a query string $query = "SELECT * FROM u_data WHERE username='$username'"; //Run the query and put RESULT REFERENCE ID into variable $result = mysql_query($query) or die(mysql_error()); //Check if there are any rows in the RESULT REFERENCE ID if(mysql_num_rows($result)>0) { //User exists echo "Username already in use."; //Add proper error handling } else{ //Add data it's working now Quote Link to comment https://forums.phpfreaks.com/topic/237431-is-this-part-of-code/#findComment-1220066 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.