dhope Posted June 7, 2011 Share Posted June 7, 2011 Hi, Is it possible to use an array to store a while loop? I am trying to retrieve some information from my database to store in an array. I am trying to store about five of my customer id's, I thought this way was possible and that I had done it before. unfortunately I am getting the following error "Fatal error: [] operator not supported for strings" if possible please can someone point me in the right direction. Is there an easy for me to store all of the ids that come from the database? My code so far... $result = mysql_query("SELECT * FROM customers WHERE $search_field = \"$search_term\"") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $customer_id[] = $row['id']; } Thank you Quote Link to comment https://forums.phpfreaks.com/topic/238723-using-array-to-store-a-while-loop/ Share on other sites More sharing options...
btherl Posted June 7, 2011 Share Posted June 7, 2011 PHP is telling you that $customer_id was used already in the same script, and was set to a string. You should be fine if you use another name for the array. I like names such as $customer_id_arr, which tells me it's an array. Quote Link to comment https://forums.phpfreaks.com/topic/238723-using-array-to-store-a-while-loop/#findComment-1226697 Share on other sites More sharing options...
teynon Posted June 7, 2011 Share Posted June 7, 2011 My guess is $customer_id is already set to a type of string, not an array. This code is probably going to get your site hacked, by the way. Quote Link to comment https://forums.phpfreaks.com/topic/238723-using-array-to-store-a-while-loop/#findComment-1226702 Share on other sites More sharing options...
dhope Posted June 8, 2011 Author Share Posted June 8, 2011 Hi, thank you for the help What would be the best way of doing this that wouldn't lead to being hacked? Quote Link to comment https://forums.phpfreaks.com/topic/238723-using-array-to-store-a-while-loop/#findComment-1226856 Share on other sites More sharing options...
cyberRobot Posted June 8, 2011 Share Posted June 8, 2011 Hi, thank you for the help What would be the best way of doing this that wouldn't lead to being hacked? If you're not doing so already, you should use the mysql_real_escape_string() on $search_term before querying the database. For more information, visit http://php.net/manual/en/function.mysql-real-escape-string.php Quote Link to comment https://forums.phpfreaks.com/topic/238723-using-array-to-store-a-while-loop/#findComment-1226916 Share on other sites More sharing options...
teynon Posted June 8, 2011 Share Posted June 8, 2011 It also appears that you are letting the user determine the outcome of $searchfield Quote Link to comment https://forums.phpfreaks.com/topic/238723-using-array-to-store-a-while-loop/#findComment-1227086 Share on other sites More sharing options...
cyberRobot Posted June 8, 2011 Share Posted June 8, 2011 It also appears that you are letting the user determine the outcome of $searchfield Good point, I was assuming that variable was populated by the program. If $searchfield contains data entered/selected by the user, make sure it's a valid choice. Quote Link to comment https://forums.phpfreaks.com/topic/238723-using-array-to-store-a-while-loop/#findComment-1227091 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.