Jump to content


Photo

How to write it ?


  • This topic is locked This topic is locked
9 replies to this topic

#1 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 20 October 2006 - 08:03 PM

Hi there!

This is a query of a PHP script I wrote which is supposed to return the number of rows for each different object present in the field nammed "list"
$query="SELECT list, count( * ) AS n FROM $table GROUP BY list HAVING ( n > 1 )";
//I don't know how to write the following
print (
// ? ? ? ? ? 
);

The result returned by this query is, FOR EXAMPLE, something like this

list    n
_____________
ai      72
bg      325
he      127
etc...

I want to send this result to my Flash file

&ai=72
&bg=325
&he=127
etc...

but I don't know to write it the right way !!!

Many thanks in advance for your help !

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 20 October 2006 - 08:52 PM

<?php
$query="SELECT list, count(*) AS n FROM $table GROUP BY list HAVING ( n > 1 )";
$res = mysql_query($query);
while (list($list, $n) = mysql_fetch_row($res)) {
    echo "$list : $n <br>";
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 20 October 2006 - 09:19 PM

Thank you very much for your reply.
I just gave it a try and... alas! It didn't work...

Is "list" a reserved keyword (as it appears in green in your code, I suppose so...)?

Anyway, thanks for your time!
Regards.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 20 October 2006 - 09:52 PM

Change the second line to

$res = mysql_query($query) or die(mysql_error());

and see if you get an error message
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 09:49 AM

Change the second line to
$res = mysql_query($query) or die(mysql_error());
and see if you get an error message


Thanks for your time and your advice but I already did it and the query is good : I got no error at all and I got the expected result as I described it in my first post.

You can yourself try it in one of your DB tables (replacing, of course the names of the fields! he he he) if you have one and using the window in the SQL Tab of phpMyAdmin...

But would you please answer my question :
Is "list" a reserved keyword in SQL Language ?

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 October 2006 - 10:18 AM

"LIST" is not in the list of MySQL reserved words.

http://dev.mysql.com...rved-words.html

It is a PHP function

http://www.php.net/list


I ran this
<?php
include 'db2.php'; // coonnection stuff

mysql_query ("CREATE TABLE test1 (
        id int not null auto_increment primary key,
        list varchar(5)
        )");

mysql_query ("INSERT INTO test1 (list)  VALUES 
        ('aaaaa'),
        ('aaaaa'),
        ('aaaaa'),
        ('bbbbb'),
        ('aaaaa'),
        ('bbbbb'),
        ('ccccc'),
        ('aaaaa')
        ");

$table = 'test1';        
$query="SELECT list, count(*) AS n FROM $table GROUP BY list HAVING ( n > 1 )";
$res = mysql_query($query) or die (mysql_error());
while (list($list, $n) = mysql_fetch_row($res)) {
    echo "$list : $n <br>";
}
?>

and got

[pre]
aaaaa : 5
bbbbb : 2
[/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 10:47 AM

The results you got do not surprise me as we are sure the query is good...

We make some progress:
I now get all the 45 fields of my Flash File populated (until now they stayed blank!).

But I don't get any number in them: they all display "undefined" !!!!!
Why the hell they do not receive the variables the query found?

Have you some knowledge about Flash Action Script?
I think it will help us to communicate...

You are a very kind person to provide me with such help.
Thank you!

PS: Thanks also for the links about "list" and the SQL Reserved Keywords !


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 October 2006 - 11:14 AM

Have you some knowledge about Flash Action Script?
I think it will help us to communicate...


Unfortunately  - none

Now you can get the variable names and values out of your database it may be better to go to a dedicated Actionscript forum to find out how to set the values inside Flash.

Or maybe open a new topic here with title "Setting actionscript variables from PHP" which should attract the attention of those with PHP/actionscript knowledge.

PS Does this help
http://www.phpfreaks....html#msg387573
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 12:16 PM

Thanks for your valuable help and for your advices!

I've posted a New Topic "Setting ActionScript variables from PHP" in this forum.
Hope some Flash geek will read it...

How old are you("senile")? I'm 65!

A+
Gerard

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 October 2006 - 02:04 PM

57
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users