zimmo Posted April 22, 2010 Share Posted April 22, 2010 I am learning how to use functions, and they are a great. I am unsure of how to perform an sql query in a function. It is checking to see if a username is taken. Can this be done with a function? Here is the query, but it is failing when called. <? function validUsername($username) { $sql = "SELECT * FROM tableA WHERE username = '$_POST[username]' "; $sql_result = mysql_query($sql); if (mysql_num_rows($sql_result) !=0) { $isValid = true; } else { $isValid = false; } return $isValid; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/ Share on other sites More sharing options...
grim1208 Posted April 22, 2010 Share Posted April 22, 2010 try defining username outside of the function. making code look like this <? $username = $_POST['username']; // defines username validUsername($username); // this calls the function function validUsername($username) { $sql = "SELECT * FROM tableA WHERE username = '$_POST[username]' "; $sql_result = mysql_query($sql); if (mysql_num_rows($sql_result) !=0) { $isValid = true; } else { $isValid = false; } return $isValid; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046307 Share on other sites More sharing options...
oni-kun Posted April 22, 2010 Share Posted April 22, 2010 Please do not use short tags, It is sloppy and not even a PHP delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046313 Share on other sites More sharing options...
zimmo Posted April 22, 2010 Author Share Posted April 22, 2010 Please do not use short tags, It is sloppy and not even a PHP delimiter. Hi Can you tell me where? I have not heard of this, still learning. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046317 Share on other sites More sharing options...
grim1208 Posted April 22, 2010 Share Posted April 22, 2010 <? = short tag .... <?php = correct way, even though <? is not incorrect Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046319 Share on other sites More sharing options...
Re321 Posted April 22, 2010 Share Posted April 22, 2010 Mhmm oni-kun is right. Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046323 Share on other sites More sharing options...
zimmo Posted April 22, 2010 Author Share Posted April 22, 2010 Doh... sorry, yes I just missed that. I normally do us php not just the ?. Thanks for pointing that out. Sorry I forgot to add about the function. The function I posted up, is actually held within an external file and called from the header using an include. The actual page that calls the form has the following code, but it does not work, it gives me no error, but it does not actually perform the sql query? it just tries to insert a duplicate. Here is the code calling the function: if ( empty($username) ) { $error['username_error'] = '<div class="formerror">Please enter your username</div>'; } elseif (!validUsername ($username) ) { $error['username_error'] = '<div class="formerror">Username taken</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046326 Share on other sites More sharing options...
oni-kun Posted April 22, 2010 Share Posted April 22, 2010 Try: $sql_result = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046340 Share on other sites More sharing options...
zimmo Posted April 22, 2010 Author Share Posted April 22, 2010 Oni its not giving me any errors now, but it is stating that the username is taken, even though I have emptied the tables so nothing is there now, it just wont register now? Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046353 Share on other sites More sharing options...
zimmo Posted April 22, 2010 Author Share Posted April 22, 2010 I had the isvalid the wrong way around, doh!! just realised and its working great now. Could you tell me oni why changing to that query worked? Quote Link to comment https://forums.phpfreaks.com/topic/199362-functions-with-sql-queries/#findComment-1046362 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.