php 2007-7-12 22:36
一个连接两个不同MYSQL数据库的PHP程序
[color=#000000][color=#0000cc]<[/color][color=#0000ff]HTML[/color][color=#0000cc]>[/color][color=#0000cc]<[/color][color=#0000ff]BODY[/color] [color=#ff0000]BGCOLOR[/color][color=#0000cc]=[/color]FFFFFF[color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000cc]?[/color][color=#0000ff]php[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Connecting as mysql<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]connection1[/color] [color=#0000cc]=[/color] [color=#ff0000]mysql_connect[/color][color=#0000cc]([/color][color=#ff00ff]'localhost'[/color][color=#0000cc],[/color] [color=#ff00ff]'mysql'[/color][color=#0000cc],[/color] [color=#ff00ff]''[/color][color=#0000cc])[/color] [color=#0000ff]or[/color] [color=#ff0000]die[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]php_errormsg[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"connection1 is $connection1<BR>\n"[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Selecting test for mysql user<BR>\n"[/color][color=#0000cc];[/color]
[color=#ff0000]mysql_select_db[/color][color=#0000cc]([/color][color=#ff00ff]'test'[/color][color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]connection1[/color][color=#0000cc])[/color] [color=#0000ff]or[/color] [color=#0000cc]@[/color][color=#ff0000]die[/color][color=#0000cc]([/color][color=#ff00ff]"Error "[/color] [color=#0000cc].[/color] [color=#0000ff]$[/color][color=#008080]php_errormsg[/color] [color=#0000cc].[/color] [color=#ff0000]mysql_error[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Connection as joyce<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]connection2[/color] [color=#0000cc]=[/color] [color=#ff0000]mysql_connect[/color][color=#0000cc]([/color][color=#ff00ff]'localhost'[/color][color=#0000cc],[/color] [color=#ff00ff]'joyce'[/color][color=#0000cc],[/color] [color=#ff00ff]''[/color][color=#0000cc])[/color] [color=#0000ff]or[/color] [color=#ff0000]die[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]php_errormsg[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"connection2 is $connection2<BR>\n"[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Selecting books for joyce user<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]db2[/color] [color=#0000cc]=[/color] [color=#ff0000]mysql_select_db[/color][color=#0000cc]([/color][color=#ff00ff]'techbizbookguide'[/color][color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]connection2[/color][color=#0000cc])[/color] [color=#0000ff]or[/color] [color=#ff0000]die[/color][color=#0000cc]([/color][color=#ff0000]mysql_error[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]query1[/color] [color=#0000cc]=[/color] [color=#ff00ff]"select foo from test"[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]query2[/color] [color=#0000cc]=[/color] [color=#ff00ff]"select title, authorFirst, authorLast from bookinfo"[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Querying test<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]users[/color] [color=#0000cc]=[/color] [color=#ff0000]mysql_query[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]query1[/color][color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]connection1[/color][color=#0000cc])[/color] [color=#0000ff]or[/color] [color=#ff0000]die[/color][color=#0000cc]([/color][color=#ff0000]mysql_error[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Querying books<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]books[/color] [color=#0000cc]=[/color] [color=#ff0000]mysql_query[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]query2[/color][color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]connection2[/color][color=#0000cc])[/color] [color=#0000ff]or[/color] [color=#ff0000]die[/color][color=#0000cc]([/color][color=#ff0000]mysql_error[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Foos from test<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000ff]while[/color] [color=#0000cc]([/color][color=#ff0000]list[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]foo[/color][color=#0000cc])[/color] [color=#0000cc]=[/color] [color=#ff0000]mysql_fetch_row[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]users[/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc]{[/color]
[color=#ff0000]echo[/color] [color=#0000ff]$[/color][color=#008080]foo[/color][color=#0000cc],[/color] [color=#ff00ff]"<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000cc]}[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Books in techbizbookguide<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000ff]while[/color] [color=#0000cc]([/color][color=#ff0000]list[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]title[/color][color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]authorFirst[/color][color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]authorLast[/color][color=#0000cc])[/color] [color=#0000cc]=[/color] [color=#ff0000]mysql_fetch_row[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]books[/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc]{[/color]
[color=#ff9900]//Use trim in case we have a book by "Madonna" or "Prince" or...
[/color]
[color=#ff0000]echo[/color] [color=#0000ff]$[/color][color=#008080]title[/color][color=#0000cc],[/color] [color=#ff00ff]' by '[/color][color=#0000cc],[/color] [color=#ff0000]trim[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]authorFirst[/color] [color=#0000cc].[/color] [color=#ff00ff]' '[/color] [color=#0000cc].[/color] [color=#0000ff]$[/color][color=#008080]authorLast[/color][color=#0000cc])[/color][color=#0000cc],[/color] [color=#ff00ff]"<BR>\n"[/color][color=#0000cc];[/color]
[color=#0000cc]}[/color]
[color=#0000cc]?[/color][color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000cc]/[/color][color=#0000ff]BODY[/color][color=#0000cc]>[/color][color=#0000cc]<[/color][color=#0000cc]/[/color][color=#0000ff]HTML[/color][color=#0000cc]>[/color]
[/color]