Beginner ? why .php file does db work but recast as .htm does not?
The query is resolved in the .php form but not when a browser opens the .html
What am I missing?
mySQL is localhost:3306
wampserver is localhost:80
root is c:\inetpub\wwwroot
a.htm is
<!DOCTYPE html>
<html>
<head>
<title>Selecting Data</title>
</head>
<body>
<?php
$user = "root";
$pass = "";
$host="localhost";
$port=3306;
$db = "mysql";
$link = mysql_connect( "$host:$port", $user, $pass );
if ( ! $link ) {
die( "Couldn't connect to MySQL: ".mysql_error() );
}
mysql_select_db( $db, $link ) or die ( "Couldn't open $db: ".mysql_error() );
$result = mysql_query( "SELECT * FROM user" );
$num_rows = mysql_num_rows( $result );
print "<p>$num_rows that have been added data to the table</p>\n";
print "<table>";
while ( $a_row = mysql_fetch_row( $result ) ) {
print "<tr>";
foreach ( $a_row as $field ) {
print "<td>".stripslashes($field)."</td>";
}
print "</tr>";
}
print "</table>";
mysql_close( $link );
?>
</body>
</html>
OUTPUT: file:///C:/inetpub/wwwroot/a.html
$num_rows that have been added data to the table
\n"; print ""; while ( $a_row = mysql_fetch_row( $result ) ) { print ""; foreach ( $a_row as $field ) { print ""; } print ""; } print "
| ".stripslashes($field)." |
"; mysql_close( $link ); ?>
a.php is:
<?php
$user = "root";
$pass = "";
$host="localhost";
$port=3306;
$db = "mysql";
$link = mysql_connect( "$host:$port", $user, $pass );
if ( ! $link ) {
die( "Couldn't connect to MySQL: ".mysql_error() );
}
mysql_select_db( $db, $link ) or die ( "Couldn't open $db: ".mysql_error() );
$result = mysql_query( "SELECT * FROM user" );
$num_rows = mysql_num_rows( $result );
print "<p>$num_rows that have been added data to the table</p>\n";
print "<table>";
while ( $a_row = mysql_fetch_row( $result ) ) {
print "<tr>";
foreach ( $a_row as $field ) {
print "<td>".stripslashes($field)."</td>";
}
print "</tr>";
}
print "</table>";
mysql_close( $link );
?>
OUTPUT:
C:\wamp\bin\php\php5.4.3\php.exe C:\inetpub\wwwroot\sa.php
<p>3 have been added data to the table</p>
<table><tr><td>localhost</td><td>root</td><td></td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td></td><td></td><td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>127.0.0.1</td><td>root</td><td></td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td></td><td></td><td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>localhost</td><td></td><td></td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td></td><td></td><td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td></tr></table>
Process finished with exit code 0
请先登录再写评论。