silverglade Posted July 9, 2011 Share Posted July 9, 2011 Hi I am having a problem understanding this code. I commented the question spots next to the code. Lets say the table is 'usertable', field is 'firstname', haystack is 'ID' and needle is '$userID' which == '007'. When we run the function, shouldn't the return $row[$field], be return $row[$haystack]? I don't know what ID is supposed to be. if we have the table, the field, and the userID we want to find, what is the "ID" in this code? Any help greatly appreciated. Thank you. I also don't know what LIMIT 1 is for. function simplequery($table, $field, $needle, $haystack) { $result = mysql_query("SELECT $field FROM $table WHERE $haystack = $needle LIMIT 1;");//WHAT DOES LIMIT 1 DO? if ($result){ if (mysql_num_rows($result)) {//IF RESULT HAS ROWS IN IT $row = mysql_fetch_assoc($result);// MAKE RESULT INTO AN ARRAY return $row[$field];//SHOULDN'T THIS BE $ROW[$HAYSTACK];? } } else { print "Error in query<br />"; } } Quote Link to comment Share on other sites More sharing options...
trq Posted July 9, 2011 Share Posted July 9, 2011 $needle and $hackstack are used as criteria for the query. They have nothing to do with what filed is returned, that is $field. LIMIT (as the name suggests) limits your results to a certain number of results. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 9, 2011 Share Posted July 9, 2011 <?php function simplequery($table, $field, $needle, $haystack) { $result = mysql_query("SELECT $field FROM $table WHERE $haystack = $needle LIMIT 1;");//WHAT DOES LIMIT 1 DO? //returns one result if ($result){ if (mysql_num_rows($result)) {//IF RESULT HAS ROWS IN IT //if returns an actual result set $row = mysql_fetch_assoc($result);// MAKE RESULT INTO AN ARRAY //returns an associative array of the fetched row return $row[$field];//SHOULDN'T THIS BE $ROW[$HAYSTACK];? //no... $haystack is a column name for this function } } else { print "Error in query<br />"; } } ?> Basically the function is made to select one field and is why they used $row[$field], look at what the SELECT is. SELECT $field FROM Edit: blah blah, same as Thorpe wrote. Quote Link to comment Share on other sites More sharing options...
silverglade Posted July 9, 2011 Author Share Posted July 9, 2011 Ok thank you both, I think I am getting it but I have a question again. take this $firstname = simplequery("usertable","firstname","ID", $UserID"); so $table = usertable $field = firstname $needle = ID; (I don't know what ID is here. If we have the table and the field (column), why do we need ID too, isn't ID a separate field, the first one in the table? Like we only need, Find X in this table and this field. I thought. But then we have ID. $haystack = $UserID. (the book's code never tells me what $UserID is equal to) Quote Link to comment Share on other sites More sharing options...
trq Posted July 9, 2011 Share Posted July 9, 2011 Look at the query. That would produce.... SELECT firstname FROM usertable WHERE ID = $UserID LIMIT 1; If you don't understand the code, don't use it. I wouldn't recommend using this function in the first place as it has holes that have the potential to cause security concerns. Quote Link to comment Share on other sites More sharing options...
silverglade Posted July 9, 2011 Author Share Posted July 9, 2011 Thank Thorpe, I am just going to skip this page of the book, it was a poor example anyway. thanks. Quote Link to comment 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.