Jump to content

array question


Sanjib Sinha

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.