Jump to content

Recommended Posts

I have doing one task and saw that I don't get all selected lines from query.

I've tried it simplified and result is same.

[code]
<?php
echo "<pre>";
function connect($query) 
{
  $db = new mysqli('localhost', 'root', 'root', 'contact_books');
  if (mysqli_connect_errno()) 
     {
         printf("Connect failed: %s\n", mysqli_connect_error());
         exit();
     }
$result = $db->query($query);
$db->close();
return $result;   
} 
      $q = connect("SELECT  * FROM `contact_details`");
      $selectResult = $q->fetch_assoc(); 
     print_r($selectResult); 
?>

[/code]

 

but ..

mysql> SELECT  * FROM contact_details;

+----+-------+---------+---------------------+---------+--------+--------+

| id | name  | surname | birthday            | status  | gender | phones |

+----+-------+---------+---------------------+---------+--------+--------+

|  1 | 1    | 1      | 0001-01-01 00:00:00 | married | male  | 1      |

|  3 | 3    | 3      | 0003-03-03 00:00:00 | single  | male  | 3      |

|  4 | 4    | 4      | 0004-04-04 00:00:00 | married | female | 4      |

|  5 | trhtr | htrhtrh | 0005-05-05 00:00:00 | single  | male  | 55555  |

+----+-------+---------+---------------------+---------+--------+--------+

 

PS. I've just work all night and closer to dinner this error have appeared.  :confused:

Link to comment
https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/
Share on other sites

 

fetch_assoc() returns an associative array, so you need to iterate through the array to get all of the values.

    while ($row = $result->fetch_assoc()) {
        printf ("\n", $row["id"], $row["name"],$row["surname"],$row["birthday"],$row["status"],$row["gender"],$row["phone"]); 
    }

it is not important because it is just one way for access data in array - $selectResult

and it don't influence on output this data:

$selectResult contain this :

Array ( [id] => 1

          [name] => 1

          [surname] => 1

          [birthday] => 0001-01-01 00:00:00

          [status] => married

          [gender] => male

        [phones] => 1

        )

 

i'm just confused where are rest rows [id] => 2 ....etc.

As you can see table has 5 rows, and I use SELECT * FROM.

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.