inspireddesign Posted March 4, 2010 Share Posted March 4, 2010 Hi everyone, I think this is a simple one. In the below code there is a function called check_username(). It has an in_array that doesn't seem to be working properly when the data array comes from the database. As you can see I'm pulling the data from the database which "I thought" was an array? When the function is called it doesn't check the username that is being passed to the function with the one that is in the $taken_usernames array. If I force the array meaning if I "fake the database array" like I'm doing below it workings fine. Oh and there is data in the array from the database. Very strange. Can someone give me a hand on this one. Thanks so much. $sql = 'SELECT user_name FROM users WHERE company_id=' . $_SESSION['cid'] . ' ' ; $taken_usernames = userData::find_by_sql($sql); // we'll fake the database check, the following names won't be available. // $taken_usernames = array( // 'remy', // 'julie', // 'andrew', // 'andy', // 'simon', // 'chris', // 'nick' // ); // main submit logic if (@$_REQUEST['action'] == 'check_username' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { // means it was requested via Ajax echo json_encode(check_username($_REQUEST['username'])); exit; // only print out the json version of the response } function check_username($username) { global $taken_usernames; $resp = array(); $username = trim($username); if (!$username) { $resp = array('ok' => false, 'msg' => "Please specify a username"); } else if (!preg_match('/^[a-z0-9\.\-_]+$/', $username)) { $resp = array('ok' => false, "msg" => "Your username can only contain alphanumerics, periods, dash and underscore"); } else if (in_array($username, $taken_usernames)) { $resp = array("ok" => false, "msg" => "<img src='includes/images/x_icon.png' height='18' width='18' />"); } else { $resp = array("ok" => true, "msg" => "<img src='includes/images/act_mark.png' height='18' width='18' />"); } return $resp; } Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/ Share on other sites More sharing options...
inspireddesign Posted March 4, 2010 Author Share Posted March 4, 2010 I got it figured out. For whatever reason the mysql_fetch_assoc was getting garbled in my userData class. Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1021675 Share on other sites More sharing options...
inspireddesign Posted March 4, 2010 Author Share Posted March 4, 2010 Haha... well I thought I had it figured out. Still have the same issue as above. Any help would be great guys. Thanks! BTW I changed how I was querying the db. This way doesn't seem to be working either. $results = mysql_query( "SELECT user_name FROM users" ) or die(mysql_error()); $taken_usernames = mysql_fetch_array($results); check_username('john'); Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1021682 Share on other sites More sharing options...
Dennis1986 Posted March 4, 2010 Share Posted March 4, 2010 Could you give us the output of the following? print_r($taken_usernames); This question was asked before you made your edit to your post and query btw. Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1021687 Share on other sites More sharing options...
inspireddesign Posted March 5, 2010 Author Share Posted March 5, 2010 The output is: $sql = 'SELECT user_name FROM users WHERE company_id=' . $_SESSION['cid'] . ' ' ; $taken_usernames = userData::find_by_sql($sql); print_r($taken_usernames); // Output Array ( [0] => userData Object ( [table][tr][td] => [user_id] => [company_id] => [insured_id] => [user_type] => [user_admin] => [full_name] => [user_email] => [user_name] => mspeck [user_password] => [user_date] => [emp_signature] => ) [1] => userData Object ( [table][tr][td] => [user_id] => [company_id] => [insured_id] => [user_type] => [user_admin] => [full_name] => [user_email] => [user_name] => dclem [user_password] => [user_date] => [emp_signature] => ) [2] => userData Object ( [table][tr][td] => [user_id] => [company_id] => [insured_id] => [user_type] => [user_admin] => [full_name] => [user_email] => [user_name] => skennedy [user_password] => [user_date] => [emp_signature] => ) [3] => userData Object ( [table][tr][td] => [user_id] => [company_id] => [insured_id] => [user_type] => [user_admin] => [full_name] => [user_email] => [user_name] => mfarley [user_password] => [user_date] => [emp_signature] => )) I'm only after the username in the above array. Thanks. [/td][/tr][/table][/td][/tr][/table][/td][/tr][/table][/td][/tr][/table] Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1021734 Share on other sites More sharing options...
JasonLewis Posted March 5, 2010 Share Posted March 5, 2010 How exactly is the method find_by_sql() grabbing and returning the data. Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1021743 Share on other sites More sharing options...
inspireddesign Posted March 5, 2010 Author Share Posted March 5, 2010 Okay, maybe the question to ask is how would I put the below query in a workable array so that I can use the in_array method I have in the code above? Thanks. $results = mysql_query( "SELECT user_name FROM users" ) or die(mysql_error()); $taken_usernames = mysql_fetch_array($results); Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1021757 Share on other sites More sharing options...
Dennis1986 Posted March 5, 2010 Share Posted March 5, 2010 Okay, maybe the question to ask is how would I put the below query in a workable array so that I can use the in_array method I have in the code above? Thanks. $results = mysql_query( "SELECT user_name FROM users" ) or die(mysql_error()); $taken_usernames = mysql_fetch_array($results); if (array_search($username, $taken_usernames) !== false) { // username taken } I'm not sure though. Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1022122 Share on other sites More sharing options...
mikesta707 Posted March 5, 2010 Share Posted March 5, 2010 why dont you just do something like $lookup_name = "john"; $sql = "select * from table where username='$lookup_name'"; $query = mysql_query($sql); if (mysql_num_rows($query) > 0){ echo "username exists"; } that database class you are using seems to be complicating things Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1022125 Share on other sites More sharing options...
ialsoagree Posted March 5, 2010 Share Posted March 5, 2010 The above suggestion seems ideal, why return all the usernames when what you really want to know if 1 specific username exists. It also avoids any PHP loops. Everything is taken care of in 1 fell swoop. But in case, for some reason, that solution doesn't work for you, I think this will fix your problem: $results = mysql_query( "SELECT user_name FROM users" ) or die(mysql_error()); while ($row = mysql_fetch_array($results)) { $taken_usernames[] = $row['user_name']; } check_username('john'); This code involves 2 loops though (1 here, while it builds the array, and the 2nd when it checks if the value is in the array, although that one is a bit more straight forward). You could further expedite the process (although, once again, not to the level that is suggested above) by saving the indexes of the username as true, and then changing your check_username function to check if the index for the username is set to true. To do the first part... $results = mysql_query( "SELECT user_name FROM users" ) or die(mysql_error()); while ($row = mysql_fetch_array($results)) { $taken_usernames[$row['user_name']] = TRUE; } check_username('john'); Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1022132 Share on other sites More sharing options...
inspireddesign Posted March 7, 2010 Author Share Posted March 7, 2010 Thanks for the help on this guys. I ended up using ialsoagree first solution to build the array of users. The reason is because (which I didn't explain the this post) I'm writing a jQuery validation script that checks against the users in the array. So I'm calling the php script each time the user enters a character in the input box and checking the array of taken usernames then returns true of false. Thanks again guys. Quote Link to comment https://forums.phpfreaks.com/topic/194179-in_array-issue/#findComment-1022708 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.