I've spent ages trying to figure this out but no joy, please help...

my test code reads
$mysqli = mysqli_connect("localhost","user","password","database");

$sql = "SELECT url FROM useful_urls WHERE urlName = 'redirect'";

if($res = mysqli_query($mysqli,$sql)){

while ($query_result = mysqli_fetch_field($res)) {
printf ("URL: %s\n", $query_result->url);                     //line 21

	}
mysqli_free_result($res);
}

I get the following error message (and the url in question is not printed to the browser)

[Fri Mar 11 01:28:30 2011] [error] [client 127.0.0.1] PHP Notice: Undefined property: stdClass::$url in /var/www/delete.php on line 21, referer: http://localhost/index.php

If I change the code to use mysqli_fetch_array - it works perfectly!!

i.e. while ($query_result = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
printf ("URL: %s\n", $query_result['url']);

thanks.

    Did you read the manual page for [man]mysqli_fetch_field/man? It doesn't do what you think it does.

      thanks for reply,

      sorry, I did read it but obviously I didn't understand it! (I only started writing PHP this week)

      I've now got the code working fine using the three incarnations of mysqli_fetch_array (ASSOC,NUM,BOTH) and mysqli_fetch_row and mysqli_fetch_obj but is there a more simple why of retrieving a single field value that doesn't require the WHILE loop?

      thanks

        manual page should be [man]mysqli-result.fetch-field[/man]

        That depends on what you mean by "requires a while loop".

        $row = $result->fetch_assoc();
        
        # even though you could iterate over all columns
        foreach ($row as $field => $value)
        {
        	if ($field == 'someField')
        	{
        		echo $value
        	}
        }
        # You do not have to, since you can access each specific field directly
        echo $row['someField'];
        

        If you are aware of this and rather talk about specifying a certain row (and column), rather than just a certain column in any row, then you should probably use a sql query that retrieves one row, for example

        SELECT some, fields FROM table WHERE pirmary_key = 23
        

        If that is not an option, but you do know in which row(s) your desired data is found, you could also use either data_seek or fetch_all

        $rows = $result->fetch_all(MYSQLI_ASSOC);
        
        # Row 16 in the array built from the result set (row 1 has index 0)
        echo $rows[15]['someField'];
        
        # row 16 in the result set
        $result->data_seek(15);
        $row16 = $result->fetch_assoc();
        

          thanks johanafm, that's answered my question.

          cheers

            18 days later
            php_steve;10976154 wrote:

            I've spent ages trying to figure this out but no joy, please help...

            my test code reads
            $mysqli = mysqli_connect("localhost","user","password","database");

            $sql = "SELECT url FROM useful_urls WHERE urlName = 'redirect'";

            if($res = mysqli_query($mysqli,$sql)){
            
            while ($query_result = mysqli_fetch_field($res)) {
            printf ("URL: %s\n", $query_result->url);                     //line 21
            
            	}
            mysqli_free_result($res);
            }

            I get the following error message (and the url in question is not printed to the browser)

            [Fri Mar 11 01:28:30 2011] [error] [client 127.0.0.1] PHP Notice: Undefined property: stdClass::$url in /var/www/delete.php on line 21, referer: http://localhost/index.php

            If I change the code to use mysqli_fetch_array - it works perfectly!!

            i.e. while ($query_result = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
            printf ("URL: %s\n", $query_result['url']);

            thanks.

            HI.
            it is an syntax error in line: while ($query_result = mysqli_fetch_field/COLOR)
            the right syntax is :

            while ($query_result = mysqli_fetch_fields/B)
            field with an 's' at the end..

              Write a Reply...