Page 1 of 1

SQL

Posted: Wed Jul 24, 2002 12:28 am
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 ???

Posted: Wed Jul 24, 2002 1:16 am
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?

sql

Posted: Wed Jul 24, 2002 1:28 am
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 ???

Posted: Wed Jul 24, 2002 1:58 am
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

Posted: Wed Jul 24, 2002 3:15 am
by prasadharischandra
i want to count name fileds that is how many names in name fields
not using mysql_num_rows

Posted: Wed Jul 24, 2002 3:18 am
by twigletmac

Posted: Wed Jul 24, 2002 3:47 am
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;

Posted: Wed Jul 24, 2002 3:53 am
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

Posted: Wed Jul 24, 2002 3:54 am
by hob_goblin
mysql_num_rows is the only way

Posted: Wed Jul 24, 2002 4:09 am
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

Posted: Thu Jul 25, 2002 2:52 am
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