Jump to content

Recommended Posts

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 />";
                }
}
          

Link to comment
https://forums.phpfreaks.com/topic/241456-php-mysql-function-question/
Share on other sites

<?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.

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)

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.