I'm tryin to figure this out.
Let's say that I run the following query against a db;
$email =mysql_query("select email from cust where cstid='4'") or
die(mysql_error());

What exactly does this pull?
It would seem that since only one record is being pulled "email" that the variable $email would have to whatever data was pulled from the database.

BUT
I can't seem to do anything with the data that is pulled with out running a mysql_fetch_array of the result.

$row=mysql_fetch_array($email);
$email= $row["email"];

echo $email;

would allow me to display to the screen the e-mail address that I pulled.

I've looked in the manual on the php.net, and the answer confused me slightly.
It stated that the mysql_query function will generate a positive if the query is doable and negative if it is not. It doesn't say anything about how the data is pulled, or what format the data is in as it is pulled.

Does my question even make sense?

Help

    I'm not sure I have a full grasp of it either, but my understanding is this:

    after running mysql_query a "temp table" is created with the results of the query. In order to be of use, that temp table must be transfered into an array (mysql_fetch_array). The array can then be used to get the actual values from the query.

      I like to think of it this way.

      When you do mysql_query it basicly just queries the database and returns an "identifer" so you could get all kinds of info on what happened. It would also be harder to use other mysql functions without doing it one step at a time.

      May seem kind of strange that it doesn't just return what you queried, but you have to remember that mysql_query wasn't just made for "SELECT" statements. You don't want wasted memory space used up by returned values that are never used. Plus it wouldn't know what you wanted returned.

      The good thing about php that makes this all worth while is you could make your own function. Say like mysql_retrive ($query).

      Well hope that made sense.

        Afterthought:

        function mysql_select_data ( $query ) {
        $result = mysql_query ( $query );
        if ( $result ):
        $i = 0;
        while ( $row = mysql_fetch_array ( $result ) {
        while ( list ( $line_num, $line ) = each ( $row ) ) {
        $data [$i][$line_num] = $line;
        }
        $i++;
        }
        endif;
        return $data;
        }

        That should give you an array with all the data you wanted from a SELECT statement.

        (Not tested. And it would be sloppy if it did work because you wouldn't know how many array variables there would be without even more code which would negate the purpose. I would only use it if I knew I would only get 1 or 2 rows and knew how many values were in each row.)

        Anyways its 3 am I'm probably just rambling....

          • [deleted]

          mysql_query() returns a result-set. (well, a pointer to a resultset really, but that's not important here)

          If the query failed, the resultset contains just "FALSE" and some error messages

          If the query did work, the resultset contains a the data that the query got from the database.

          You can get data out of this data-structure using the mysql_fetch_array(), mysql_num_rows() etc.

          These functions generate the real PHP-data, the arrays, numbers etc that contain the database data.

            It would seem that since only one record is being pulled "email"

            (1) Why would it "seem" that way, when clearly in the case where more than one row is being retrieved that couldn't be the case? Surely you wouldn't expect the same function to return the data directly in some cases, but a "handle" to a result set in others.

            (2) How do you know that only one row is being retrieved? There could be any number of rows that have a matching 'ctsid', unless that column happens to be defined as the primary key or used to create a unique index. But again the function mysql_query() has to work generically with all kinds of queries.

            At any rate, it's all there in the manual section on mysql, including example code...

              Write a Reply...