dbk Posted June 1, 2010 Share Posted June 1, 2010 Hi I'm trying to get all from the row 'client_id' in a table and put it in a array, exept where the 'client_id' has a specific value. After it's in a array I can search for a specific value! This is what I've been trying: $except_client = 34343434; $check = 12121212; $check_sql = "SELECT client_id FROM clients WHERE client_id !='$except_client'"; $client_result = mysql_query($check_sql); $client_row = mysql_fetch_array($client_result); $search = array_search($check, $client_row); When I write the mysql command in the CMD I get the result I want, but I can't get it to the array! Only the first row. Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/ Share on other sites More sharing options...
kenrbnsn Posted June 1, 2010 Share Posted June 1, 2010 In PHP the mysql_query() function returns a pointer to the results array. You need to put the fetch function in a loop to get all the results: <?php $except_client = 34343434; $check = 12121212; $check_sql = "SELECT client_id FROM clients WHERE client_id !='$except_client'"; $client_result = mysql_query($check_sql); $tmp = array(); while ($client_row = mysql_fetch_array($client_result)) { $tmp[] = $client_row['client_id']; } $search = array_search($check, $tmp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066239 Share on other sites More sharing options...
Psycho Posted June 1, 2010 Share Posted June 1, 2010 @dbk, That process doesn't make sense. Why would you want to get all the clinet_id's not equal to a ccertain value and then do a search for a specific client_id? Why not just do a query for the client_id you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066241 Share on other sites More sharing options...
dbk Posted June 1, 2010 Author Share Posted June 1, 2010 Hi kenrbnsn I get this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in .... Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066245 Share on other sites More sharing options...
premiso Posted June 1, 2010 Share Posted June 1, 2010 $client_result = mysql_query($check_sql) or trigger_error("Query Failed: " . mysql_error()); Use that and see what error the mysql statement is throwing. Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066247 Share on other sites More sharing options...
dbk Posted June 1, 2010 Author Share Posted June 1, 2010 Hi mjdamato I have a reason: The table contains all our clients numbers, and this function is when you want to edit the client data in a form. The value i get from $_POST[] could be the same as it already is if no changes is made. But it could also be the same as one of the other clients in our database, wich would be a mess! That's why I want to exclude the row where the existing client number is, and only search all other! Maybe there is a other way?? Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066249 Share on other sites More sharing options...
dbk Posted June 1, 2010 Author Share Posted June 1, 2010 Sorry Sorry!!! kenrbnsn it works beautiful!!! Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066253 Share on other sites More sharing options...
Psycho Posted June 1, 2010 Share Posted June 1, 2010 Hi mjdamato I have a reason: The table contains all our clients numbers, and this function is when you want to edit the client data in a form. The value i get from $_POST[] could be the same as it already is if no changes is made. But it could also be the same as one of the other clients in our database, wich would be a mess! That's why I want to exclude the row where the existing client number is, and only search all other! Maybe there is a other way?? If I am understanding you correctly the post data will have the old client ID and a (possibly) new client ID. And you are wanting to update the client ID if there was a new one submitted. You would just need a single query to the effect UPDATE table SET client_id = '$newID' WHERE client_id = '$oldID' If you need to ensure there are no duplicates, just do a query before that to see if the new ID already exists or not. Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066327 Share on other sites More sharing options...
dbk Posted June 2, 2010 Author Share Posted June 2, 2010 Thanks a lot guys! Now it's rolling With your help I got this fine working code: //fictitious values $except_client = 77131923; $check = 65771245; $check_sql = "SELECT client_id FROM client WHERE client_id !=$except_client"; $client_result = mysql_query($check_sql) or trigger_error("Query Failed: " . mysql_error()); //Place query in a array $client_temp = array(); while ($client_row = mysql_fetch_array($client_result)) { $client_temp[] = $client_row['client_id']; } //Return 1 if value exists in array $search = in_array ($check, $client_temp); //Do the desired action if.. if ($search == 1) { echo "Client no: $check is taken!"; } else { echo "no: $check is a fine client number!"; } Using the 'in_array' function it returns 1 if exists in array and nothing if not. Before I tried the 'search_array' function wich gave me problems to evaluate with 'if' because the first place in the array returns 0 if the value exists in array. Quote Link to comment https://forums.phpfreaks.com/topic/203546-help-with-passing-mysql-table-to-a-array/#findComment-1066808 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.