SQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
prasadharischandra
Forum Commoner
Posts: 57
Joined: Thu May 09, 2002 2:19 am
Location: sri lanka
Contact:

SQL

Post by prasadharischandra »

sql=mysql_query("select count(name)as name1 from ip where name='$ip1' orderby name")
while(row=mysql_fetch_array(sql){

name_real=row['name1'];
}
echo name_real;
this code is not working how to use count sql ???
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

I'm somewhat of a newbie, but do you need to place dollar signs ($) before the sql, row, and name_real variables, or is that not required?
prasadharischandra
Forum Commoner
Posts: 57
Joined: Thu May 09, 2002 2:19 am
Location: sri lanka
Contact:

sql

Post by prasadharischandra »

it's a not a problem i correct it i want to know how is COUNT is using php
thank u
$sql=mysql_query("select count(name)as name1 from ip where name='$ip1' orderby name")
while($row=mysql_fetch_array($sql){

$name_real=$row['name1'];
}
echo $name_real;
this code is not working how to use count sql ???
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What exactly are you trying to achieve with your code?

Edit: Oh and don't post the same question twice - it will be deleted.

Mac
prasadharischandra
Forum Commoner
Posts: 57
Joined: Thu May 09, 2002 2:19 am
Location: sri lanka
Contact:

Post by prasadharischandra »

i want to count name fileds that is how many names in name fields
not using mysql_num_rows
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

prasadharischandra
Forum Commoner
Posts: 57
Joined: Thu May 09, 2002 2:19 am
Location: sri lanka
Contact:

Post by prasadharischandra »

yes i look but i want to know how to take it using php
i.e how to take out put from php
eg:
echo $count;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Then what exactly is not working in your initial code (aside from the semi-colon missing at the end of the $sql line and the closing parenthesis for the while conditions)?

Mac
Last edited by twigletmac on Wed Jul 24, 2002 3:54 am, edited 1 time in total.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

mysql_num_rows is the only way
prasadharischandra
Forum Commoner
Posts: 57
Joined: Thu May 09, 2002 2:19 am
Location: sri lanka
Contact:

Post by prasadharischandra »

my problem is
eg:
name testno
saliya 3
prasad 4
matara 5

i want to find using SQL Count saliaya has 3,prasad has 4 ...etc
please let me know how to do that using array
eg:
if array(0) it's should be saliya
echo array(0);//saliya 3
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I think this is what you are trying to do:

Code: Select all

$sql = "SELECT name, COUNT(*) as num_rows FROM ip WHERE name='$ip1' GROUP BY name ORDER BY name";
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

echo 'Name | Num Rows';
while ($row = mysql_fetch_assoc($sql)) &#123; 
    $name = $row&#1111;'name'];
    $num_rows = $row&#1111;'num_rows'];
    echo $name.' | '.$num_rows;
&#125;
I haven't tested this but it should be something along those lines - you really should read http://www.mysql.com/doc/C/o/Counting_rows.html to see what SQL statements look like for the result you are trying to achieve.

Mac
Post Reply