Newbie MySQL/PHP question re output formatting
I can get this to work just fine: ?php $number = 23999.39; print $; print number_format($number, 2); ? Comes out $23,999.39 I'd like use the number_format() thingie on an array returned from a mysql_query. My current program snippet looks like: $res = mysql_query(SELECT org, COUNT(*), SUM(annual_cost) AS cost FROM a05 GROUP BY org ORDER BY cost DESC,$dbh); if (!$res) { echo mysql_errno().: . mysql_error ().; return 0; } print table border=1; while ($thearray = mysql_fetch_array($res)) { printf(trtd {$thearray[org]} /td td align=right {$thearray[COUNT(*)]} /td td align=right $ {$thearray[cost]} /td/tr); } print /table; and works fine -- see http://ibbmonitor.com/sked_1.php, 3rd block of stuff down. If I replace {$thearray[cost]} with number_format({$thearray[cost]}, 2) I get $ number_format(7842554.24, 2) in the cell where I would expect to get $ 7,842,554.24 Any idea what I'm doing wrong? Clearly, I don't understand arrays very well. Thanks VERY much for any help on this. bw --- Bill Whitacre [EMAIL PROTECTED] -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Newbie MySQL/PHP question re output formatting
Bill Whitacre wrote: printf(trtd {$thearray[org]} /td td align=right {$thearray[COUNT(*)]} /td td align=right $ {$thearray[cost]} /td/tr); If I replace {$thearray[cost]} with number_format({$thearray[cost]}, 2) Although this is a MySQL mailing list and your problem is not MySQL related, but a PHP question I'll give you a brief answer. PHP does not evaluate functions inside a double quoted string, so you should use: trtd .number_format($thearray['cost'],2). /tdtd align... Furthermore, using $thearray[cost] is not advisable; PHP will try to find a constant named 'cost' and if it can't find one it will use the string itself as the value. Use a real string (quoted) instead of relying on this feature: $thearray['cost'] or $thearray[cost] Also you can use aliases in your query to avoid things like $thearray[COUNT(*)]; Use something like this in your query: SELECT COUNT(*) AS `count` FROM and you can use $thearray['count'] instead As a last pointer, printf() is pretty much useless here since you don't use any variable formatting features of printf() here. print() or echo() are more suitable in this case. Regards, Jigal -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Newbie MySQL/PHP question re output formatting
Bill Whitacre wrote: I can get this to work just fine: ?php $number = 23999.39; print $; print number_format($number, 2); ? Comes out $23,999.39 I'd like use the number_format() thingie on an array returned from a mysql_query. My current program snippet looks like: $res = mysql_query(SELECT org, COUNT(*), SUM(annual_cost) AS cost FROM a05 GROUP BY org ORDER BY cost DESC,$dbh); if (!$res) { echo mysql_errno().: . mysql_error ().; return 0; } print table border=1; while ($thearray = mysql_fetch_array($res)) { printf(trtd {$thearray[org]} /td td align=right {$thearray[COUNT(*)]} /td td align=right $ {$thearray[cost]} /td/tr); } print /table; and works fine -- see http://ibbmonitor.com/sked_1.php, 3rd block of stuff down. If I replace {$thearray[cost]} with number_format({$thearray[cost]}, 2) I get $ number_format(7842554.24, 2) The issue is that PHP replaces $thearray[cost], with the contents of that variable (that is an array, it doesn't matter). But in the second case it replaces the same thing ($thearray[cost]), with the contents of the variable, but you want to place there the result of the function. To do date change the first line from printf(trtd number_format({$thearray[cost]}, 2) /td to printf(trtd .number_format({$thearray[cost]}, 2). /td in the cell where I would expect to get $ 7,842,554.24 Any idea what I'm doing wrong? Clearly, I don't understand arrays very well. Thanks VERY much for any help on this. bw --- Bill Whitacre [EMAIL PROTECTED] -- Nuno Pereira -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Newbie MySQL/PHP question re output formatting
Nuno Pereira wrote: To do date change the first line from printf(trtd number_format({$thearray[cost]}, 2) /td to printf(trtd .number_format({$thearray[cost]}, 2). /td I think you mean: print( trtd . number_format( $thearray['cost'], 2 ) . /td ); As stated by a previous poster to this thread, you shouldn't rely on PHP's automagical conversion of nonexistent constants to strings. The extra curly braces { } would also likely cause an error in the function params, and are extraneous even if they don't. -- Jasper Bryant-Greene Freelance web developer http://jasper.bryant-greene.name/ -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
A mysql php question
Hello everyone, I'm not sure if this is the proper place to be asking this question as I believe it is more of a question on how to traverse through php arrays and such but it is using a mysql_query function. So if this not the place pleace don't read any further and ignore and/or delete this message. So I am new to mysql AND php, with a background in Oracle SQL. Anyway I am just starting out writing a web application for a DB of the collection of videos, games, and cds of me and my wife's. So I want to start out with just making a table with the field names as the header rows and then the data underneath the corresponding field name/column. The current code (last part of this message) only prints out the right column headers/field names and then puts the right information under it but it only gives me the first record, I want all records. *example of current code output* IdNum FName LName 1 Ryan McDougall This is my code so far, be prepared to laugh as this is pretty bad code I'm sure (I just don't want to be held liable for any injuries from falling out of your chairs :-P ): html headtitleMy PHP test file/title/head body ?php $link = mysql_connect (localhost, username, password) or die (Could not connect br\n); print (Connected successfullybr\n); mysql_select_db(DBname) or die(Could not select database); $query = SELECT * FROM Who; $result = mysql_query ($query) or die (Query failed); $line = mysql_fetch_array($result, MYSQL_ASSOC); print table\n; print \ttr\n; foreach ($line as $key = $value) { print \t\ttd$key/td\n; } print \t/tr\n; print \ttr\n; foreach ($line as $col_value) { print \t\ttd$col_value/td\n; } print \t/tr\n; print /table\n; mysql_free_result ($result); mysql_close ($link); ? /body /html __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
Re: A mysql php question
Ryan, this line is not correct: $line = mysql_fetch_array($result, MYSQL_ASSOC); Instead, you will need to loop through the result set, like: while($row = mysql_fetch_array($result)) { for($i=0; $i mysql_num_fields($result); $i++) { echo $row[$i]; } } You can find more info here: http://www.php.net/manual/en/ref.mysql.php I leave formatting the output up to you ;-) Regards, -- Stefan Hinz [EMAIL PROTECTED] Geschäftsführer / CEO iConnect GmbH http://iConnect.de Heesestr. 6, 12169 Berlin (Germany) Tel: +49 30 7970948-0 Fax: +49 30 7970948-3 - Original Message - From: Ryan McDougall [EMAIL PROTECTED] To: mysql [EMAIL PROTECTED] Sent: Sunday, January 19, 2003 6:47 PM Subject: A mysql php question Hello everyone, I'm not sure if this is the proper place to be asking this question as I believe it is more of a question on how to traverse through php arrays and such but it is using a mysql_query function. So if this not the place pleace don't read any further and ignore and/or delete this message. So I am new to mysql AND php, with a background in Oracle SQL. Anyway I am just starting out writing a web application for a DB of the collection of videos, games, and cds of me and my wife's. So I want to start out with just making a table with the field names as the header rows and then the data underneath the corresponding field name/column. The current code (last part of this message) only prints out the right column headers/field names and then puts the right information under it but it only gives me the first record, I want all records. *example of current code output* IdNum FName LName 1 Ryan McDougall This is my code so far, be prepared to laugh as this is pretty bad code I'm sure (I just don't want to be held liable for any injuries from falling out of your chairs :-P ): html headtitleMy PHP test file/title/head body ?php $link = mysql_connect (localhost, username, password) or die (Could not connect br\n); print (Connected successfullybr\n); mysql_select_db(DBname) or die(Could not select database); $query = SELECT * FROM Who; $result = mysql_query ($query) or die (Query failed); $line = mysql_fetch_array($result, MYSQL_ASSOC); print table\n; print \ttr\n; foreach ($line as $key = $value) { print \t\ttd$key/td\n; } print \t/tr\n; print \ttr\n; foreach ($line as $col_value) { print \t\ttd$col_value/td\n; } print \t/tr\n; print /table\n; mysql_free_result ($result); mysql_close ($link); ? /body /html __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
mysql php question
Does anyone have some simple sample code using php to access a mySQL database? Could you post it or send someit to me please? Taylor Lewick Unix System Administrator Fortis Benefits 816 881 6073 Help Wanted. Seeking Telepath... You Know where to apply. Please Note The information in this E-mail message is legally privileged and confidential information intended only for the use of the individual(s) named above. If you, the reader of this message, are not the intended recipient, you are hereby notified that you should not further disseminate, distribute, or forward this E-mail message. If you have received this E-mail in error, please notify the sender. Thank you * - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
Re: mysql php question
Not really a simplistic one, but simple to set-up and use: http://phpnuke.org Very nice dynamic php/mysql based web site you can download and install. Larry Irwin CCA Medical - Original Message - From: Taylor Lewick [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, June 24, 2002 2:16 PM Subject: mysql php question Does anyone have some simple sample code using php to access a mySQL database? Could you post it or send someit to me please? Taylor Lewick Unix System Administrator Fortis Benefits 816 881 6073 Help Wanted. Seeking Telepath... You Know where to apply. Please Note The information in this E-mail message is legally privileged and confidential information intended only for the use of the individual(s) named above. If you, the reader of this message, are not the intended recipient, you are hereby notified that you should not further disseminate, distribute, or forward this E-mail message. If you have received this E-mail in error, please notify the sender. Thank you * - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
Re: mysql php question
gotcha, thanks, which manual is that? Taylor Lewick Unix System Administrator Fortis Benefits 816 881 6073 Help Wanted. Seeking Telepath... You Know where to apply. [EMAIL PROTECTED] 06/24/02 01:39PM in the php manual in the section of functions about mysql, it has a few examples. SNIP ?php /* Connecting, selecting database */ $link = mysql_connect(mysql_host, mysql_user, mysql_password) or die(Could not connect); print Connected successfully; mysql_select_db(my_database) or die(Could not select database); /* Performing SQL query */ $query = SELECT * FROM my_table; $result = mysql_query($query) or die(Query failed); /* Printing results in HTML */ print table\n; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print \ttr\n; foreach ($line as $col_value) { print \t\ttd$col_value/td\n; } print \t/tr\n; } print /table\n; /* Free resultset */ mysql_free_result($result); /* Closing connection */ mysql_close($link); ? /SNIP On Monday 24 June 2002 1:16, you wrote: Does anyone have some simple sample code using php to access a mySQL database? Could you post it or send someit to me please? Taylor Lewick Unix System Administrator Fortis Benefits 816 881 6073 Help Wanted. Seeking Telepath... You Know where to apply. Please Note The information in this E-mail message is legally privileged and confidential information intended only for the use of the individual(s) named above. If you, the reader of this message, are not the intended recipient, you are hereby notified that you should not further disseminate, distribute, or forward this E-mail message. If you have received this E-mail in error, please notify the sender. Thank you * - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php -- mysql, sql, query Please Note The information in this E-mail message is legally privileged and confidential information intended only for the use of the individual(s) named above. If you, the reader of this message, are not the intended recipient, you are hereby notified that you should not further disseminate, distribute, or forward this E-mail message. If you have received this E-mail in error, please notify the sender. Thank you * - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
RE: mysql php question
The one at www.php.net -Original Message- From: Taylor Lewick [mailto:[EMAIL PROTECTED]] Sent: Monday, June 24, 2002 1:50 PM To: [EMAIL PROTECTED] Subject: Re: mysql php question gotcha, thanks, which manual is that? Taylor Lewick Unix System Administrator Fortis Benefits 816 881 6073 Help Wanted. Seeking Telepath... You Know where to apply. [EMAIL PROTECTED] 06/24/02 01:39PM in the php manual in the section of functions about mysql, it has a few examples. SNIP ?php /* Connecting, selecting database */ $link = mysql_connect(mysql_host, mysql_user, mysql_password) or die(Could not connect); print Connected successfully; mysql_select_db(my_database) or die(Could not select database); /* Performing SQL query */ $query = SELECT * FROM my_table; $result = mysql_query($query) or die(Query failed); /* Printing results in HTML */ print table\n; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print \ttr\n; foreach ($line as $col_value) { print \t\ttd$col_value/td\n; } print \t/tr\n; } print /table\n; /* Free resultset */ mysql_free_result($result); /* Closing connection */ mysql_close($link); ? /SNIP On Monday 24 June 2002 1:16, you wrote: Does anyone have some simple sample code using php to access a mySQL database? Could you post it or send someit to me please? Taylor Lewick Unix System Administrator Fortis Benefits 816 881 6073 Help Wanted. Seeking Telepath... You Know where to apply. Please Note The information in this E-mail message is legally privileged and confidential information intended only for the use of the individual(s) named above. If you, the reader of this message, are not the intended recipient, you are hereby notified that you should not further disseminate, distribute, or forward this E-mail message. If you have received this E-mail in error, please notify the sender. Thank you * - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php -- mysql, sql, query Please Note The information in this E-mail message is legally privileged and confidential information intended only for the use of the individual(s) named above. If you, the reader of this message, are not the intended recipient, you are hereby notified that you should not further disseminate, distribute, or forward this E-mail message. If you have received this E-mail in error, please notify the sender. Thank you * - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
Re: mysql php question
Taylor ... Check out ... http://hotwired.lycos.com/webmonkey/databases/tutorials/tutorial4.html ... or ... http://vtwebwizard.com/tutorials/mysql/ - Original Message - From: Taylor Lewick [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, June 24, 2002 1:16 PM Subject: mysql php question Does anyone have some simple sample code using php to access a mySQL database? Could you post it or send someit to me please? Taylor Lewick Unix System Administrator Fortis Benefits 816 881 6073 Help Wanted. Seeking Telepath... You Know where to apply. Please Note The information in this E-mail message is legally privileged and confidential information intended only for the use of the individual(s) named above. If you, the reader of this message, are not the intended recipient, you are hereby notified that you should not further disseminate, distribute, or forward this E-mail message. If you have received this E-mail in error, please notify the sender. Thank you * - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php