Member Avatar for kirtan_thakkar

This is a MySQL PHP question...
how to count no of fields in a perticular database and in a perticular table??

I have three columns in a table :id , name , info
I have to print the unique persons.. how could I??
How to code it in php??
Can any one help..

Recommended Answers

All 15 Replies

Member Avatar for rajarajan2017
$query = "SELECT * FROM symbols";
// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
//Return number of fields based on your query and the table.
echo mysql_num_fields($result);

PHP have a function like returning rows. what do you want to know? counting no of fields or printing values whatever you have in the table. Anyway you have id as a primary key right? then you can directly print all the records.

Member Avatar for kirtan_thakkar

It is returning the columns..
But i have to count rows..

Member Avatar for rajarajan2017
$query = "SELECT * FROM symbols";
// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
//Return number of rows based on your query and the table.
echo mysql_num_rows($result);
Member Avatar for kirtan_thakkar

Thanks..
And how to print Unique names from the name field???

Member Avatar for rajarajan2017

did you have duplicate names in your column?

SELECT DISTINCT "column_name"
FROM "table_name"
SELECT DISTINCT names
FROM symbols
Member Avatar for kirtan_thakkar

Yes..

Member Avatar for rajarajan2017

try above code

Member Avatar for kirtan_thakkar

it is printing "Resource id #5"
i have used the code like this..

$q2 = "SELECT DISTINCT name FROM persons";
 $r2=mysql_query($q2) or die ("Error in query: $q2. ".mysql_error());
 echo $r2;

I have to print a name with a link on it..

Member Avatar for kirtan_thakkar

it is printing "Resource id #5"
i have used the code like this..

$q2 = "SELECT DISTINCT name FROM persons";
$r2=mysql_query($q2) or die ("Error in query: $q2. ".mysql_error());
echo $r2;

I have to print a name with a link on it..

Member Avatar for rajarajan2017
$query = "SELECT DISTINCT name FROM persons";
$result=mysql_query($query) or die ("Error in query: $q2. ".mysql_error());
if (mysql_num_rows($result) > 0) { 
 while($row = mysql_fetch_row($result)) {
    echo $row[0] + "<br/>;
 }
}

Try the code and get me back

Member Avatar for kirtan_thakkar

output is "00"...

Much easier to just change the select statement in working code (i.e. rajarajan's):

$query = "SELECT DISTINCT name FROM persons";
// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
//Return number of rows based on your query and the table.
echo mysql_num_rows($result);
Member Avatar for kirtan_thakkar

Much easier to just change the select statement in working code (i.e. rajarajan's):

$query = "SELECT DISTINCT name FROM persons";
// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
//Return number of rows based on your query and the table.
echo mysql_num_rows($result);

Thank you ..
But I want to print that distinct names....

Member Avatar for rajarajan2017
SELECT DISTINCT name FROM persons;

Run this query in php admin sql editor and tell me whether it returns the rows.

Member Avatar for kirtan_thakkar
SELECT DISTINCT name FROM persons;

Run this query in php admin sql editor and tell me whether it returns the rows.

Yes it returns rows..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.