fluidsharp Posted May 9, 2010 Share Posted May 9, 2010 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. Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/ Share on other sites More sharing options...
hansford Posted May 9, 2010 Share Posted May 9, 2010 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"]); } Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/#findComment-1055358 Share on other sites More sharing options...
fluidsharp Posted May 9, 2010 Author Share Posted May 9, 2010 fetch_assoc() returns an associative array yes it is but it has just only one record from DB(need 5 records) and it is problem. I don't know why Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/#findComment-1055375 Share on other sites More sharing options...
niranjan81 Posted May 9, 2010 Share Posted May 9, 2010 which loop structure are you using ? Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/#findComment-1055397 Share on other sites More sharing options...
fluidsharp Posted May 9, 2010 Author Share Posted May 9, 2010 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. Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/#findComment-1055435 Share on other sites More sharing options...
kenrbnsn Posted May 9, 2010 Share Posted May 9, 2010 Each fetch get one row. To get them all, you need to put the fetch in a loop. Ken Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/#findComment-1055437 Share on other sites More sharing options...
mattal999 Posted May 9, 2010 Share Posted May 9, 2010 Change: $selectResult = $q->fetch_assoc(); To: $selectResult = array(); while($rowResult = $q->fetch_assoc()) { $selectResult[] = $rowResult; } Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/#findComment-1055444 Share on other sites More sharing options...
fluidsharp Posted May 9, 2010 Author Share Posted May 9, 2010 Thanks a lot! finally I got it. Huh Link to comment https://forums.phpfreaks.com/topic/201154-difficulty-with-mysql/#findComment-1055452 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.