suttercain Posted March 18, 2009 Share Posted March 18, 2009 I know the answer is simple, but I have been under the weather and it's affecting my thinking I am doing a form check via ajax, php and mysql. I think the problem I am running into is on the database query. The code basically allows a user to input some data in a form field, the ajax checks to see if that data exists in the database or not. "That username is already taken" type of thing. Here's the PHP code: <?php require('connection.php'); $q=$_REQUEST['username']; $statement = "SELECT distinct(engine_family_name) FROM eo_engine_family WHERE engine_family_name = '".$q."'"; $results = mysql_query($statement) or die(mysql_error()); while($row=mysql_fetch_assoc($results)) { $taken_usernames=$row; } /*$taken_usernames = array( 'remy', 'julie', 'andrew', 'andy', 'simon', 'chris', 'nick' );*/ ?> Note: When I use the standard array (the commented out part), the code works. It'll say 'nick' is already taken, etc. But when I instead try and replace it with a sql query result, it doesn't work. How do I get the mysql result to act like that of the regular 'static' array? Any suggestions? Thanks. Link to comment https://forums.phpfreaks.com/topic/149983-the-quivilant-of-a-standard-array-but-coming-from-a-database/ Share on other sites More sharing options...
Kalland Posted March 18, 2009 Share Posted March 18, 2009 You don't need to fetch the query, just check if it returns one record. Try this: <?php // Chech if mysql returns one row if(mysql_num_rows($results) == 1) { // One record returned echo 'username taken'; } else { // username available echo 'username available'; } ?> Link to comment https://forums.phpfreaks.com/topic/149983-the-quivilant-of-a-standard-array-but-coming-from-a-database/#findComment-787702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.