Sanjib Sinha Posted January 2, 2009 Share Posted January 2, 2009 I have a database called 'people'. It has a table 'record'. It contains 4 fields : Name, Address, Phone-Number, Pan-Number. The page exp1.php has the following code <html> <head> <meta http-equiv="Expires" content="0"> <title>experiment</title> </head> <body bgcolor="#ffffff" leftmargin="100" topmargin="100" marginwidth="100" marginheight="100"> <?php $my_connection = mysql_connect('localhost', 'root', ''); if (!$my_connection) { die('Could not conncet: ' . mysql_error()); } echo 'Connected Successfully' . '<br><br>'; $my_database = mysql_select_db('people'); if (!$my_database) { die('Could not find database: ' . mysql_error()); } $query = "SELECT * FROM record"; $result = mysql_query($query); $my_rows = " "; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $my_rows = $my_rows . "Name : {$row['Name']} <br>" . "Address : {$row['Address']} <br>" . "Phone_Number : {$row['Phone-Number']} <br>" . "Pan_Number : {$row['Pan-Number']} <br><br>"; } echo ($my_rows); mysql_close($my_connection); ?> </body> </html> It has an output as follows: Connected Successfully Name : Sanjib Sinha Address : 55 Phone_Number : 332225648 Pan_Number : 123456 Name : Kaberi sinha Address : 55 Phone_Number : 123456 Pan_Number : 123456 Name : Hagudu Mutudu Address : 55 Phone_Number : 123456 Pan_Number : 123456 Name : D N chowdhury Address : 55, Regent Park, Calcutta-711109 Phone_Number : 123456 Pan_Number : 123456 So it works fine. Now I want to retrieve only the 'Name' fields. What should be the code? Link to comment https://forums.phpfreaks.com/topic/139165-array-question/ Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 SELECT Name FROM record; That should be your query. Link to comment https://forums.phpfreaks.com/topic/139165-array-question/#findComment-727889 Share on other sites More sharing options...
Sanjib Sinha Posted January 2, 2009 Author Share Posted January 2, 2009 SELECT Name FROM record; That should be your query. Yes that has solved the problem. I also tried in a different way, I just put $query = "SELECT * FROM record"; $result = mysql_query($query); $my_rows = " "; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $my_rows = $my_rows . "Name : {$row['Name']} <br>"; } echo ($my_rows); It also reads only 'Name' from record. Thank you for answering. Link to comment https://forums.phpfreaks.com/topic/139165-array-question/#findComment-728205 Share on other sites More sharing options...
GingerRobot Posted January 2, 2009 Share Posted January 2, 2009 That will indeed work but if you're only wanting the name field, that is all you should select (rather than selecting all fields, which * does). Selecting more fields than you need is inefficient. Link to comment https://forums.phpfreaks.com/topic/139165-array-question/#findComment-728224 Share on other sites More sharing options...
Sanjib Sinha Posted January 2, 2009 Author Share Posted January 2, 2009 That will indeed work but if you're only wanting the name field, that is all you should select (rather than selecting all fields, which * does). Selecting more fields than you need is inefficient. Sorry, I should not have thought that way. I just tried to manage as my knowledge is incomplete. Thanks. I'm learning. Link to comment https://forums.phpfreaks.com/topic/139165-array-question/#findComment-728251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.