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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.