'>' 之后的 PHP 代码显示为 HTML
我疯狂地尝试使用 PHP 获取值并将其插入到 SQLite 数据库中。我什至还没有做到这一点!
看来'>'之后的所有php代码显示为 html 文本,我不明白为什么。代码发布在下面。
<html>
<head></head>
<body>
<?php
try
{
//create or open the database
$database = new SQLiteDatabase('myDatabase.sqlite', 0666, $error);
}
catch(Exception $e)
{
die($error);
}
//add Movie table to database
$query = 'CREATE TABLE Movies ' .
'(Title TEXT, Director TEXT, Year INTEGER)';
if(!$database->queryExec($query, $error))
{
die($error);
}
//insert data into database
$query =
'INSERT INTO Movies (Title, Director, Year) ' .
'VALUES ("The Dark Knight", "Christopher Nolan", 2008); ' .
'INSERT INTO Movies (Title, Director, Year) ' .
'VALUES ("Cloverfield", "Matt Reeves", 2008); ' .
'INSERT INTO Movies (Title, Director, YEAR) ' .
'VALUES ("Beverly Hills Chihuahua", "Raja Gosnell", 2008)';
if(!$database->queryExec($query, $error))
{
die($error);
}
//read data from database
$query = "SELECT * FROM Movies";
if($result = $database->query($query, SQLITE_BOTH, $error))
{
while($row = $result->fetch())
{
print("Title: {$row['Title']} <br />" .
"Director: {$row['Director']} <br />".
"Year: {$row['Year']} <br /><br />");
}
}
else
{
die($error);
}
?>
</body>
</html>
这就是 html 中显示的内容。
queryExec($query, $error)) { die($error); } //insert data into database $query = 'INSERT INTO Movies (Title, Director, Year) ' . 'VALUES ("The Dark Knight", "Christopher Nolan", 2008); ' . 'INSERT INTO Movies (Title, Director, Year) ' . 'VALUES ("Cloverfield", "Matt Reeves", 2008); ' . 'INSERT INTO Movies (Title, Director, YEAR) ' . 'VALUES ("Beverly Hills Chihuahua", "Raja Gosnell", 2008)'; if(!$database->queryExec($query, $error)) { die($error); } //read data from database $query = "SELECT * FROM Movies"; if($result = $database->query($query, SQLITE_BOTH, $error)) { while($row = $result->fetch()) { print("Title: {$row['Title']}" . "Director: {$row['Director']}". "Year: {$row['Year']}"); } } else { die($error); } ?>
我在 Mac OS X 上的本地 apache 服务器上运行此程序。
任何帮助都会很棒。
I am going nuts trying to use PHP to get and insert values into an SQLite database. I haven't even managed to get that far yet!
It seems that all the php code after '>' is displayed as html text and I cant understand why. The code is posted below.
<html>
<head></head>
<body>
<?php
try
{
//create or open the database
$database = new SQLiteDatabase('myDatabase.sqlite', 0666, $error);
}
catch(Exception $e)
{
die($error);
}
//add Movie table to database
$query = 'CREATE TABLE Movies ' .
'(Title TEXT, Director TEXT, Year INTEGER)';
if(!$database->queryExec($query, $error))
{
die($error);
}
//insert data into database
$query =
'INSERT INTO Movies (Title, Director, Year) ' .
'VALUES ("The Dark Knight", "Christopher Nolan", 2008); ' .
'INSERT INTO Movies (Title, Director, Year) ' .
'VALUES ("Cloverfield", "Matt Reeves", 2008); ' .
'INSERT INTO Movies (Title, Director, YEAR) ' .
'VALUES ("Beverly Hills Chihuahua", "Raja Gosnell", 2008)';
if(!$database->queryExec($query, $error))
{
die($error);
}
//read data from database
$query = "SELECT * FROM Movies";
if($result = $database->query($query, SQLITE_BOTH, $error))
{
while($row = $result->fetch())
{
print("Title: {$row['Title']} <br />" .
"Director: {$row['Director']} <br />".
"Year: {$row['Year']} <br /><br />");
}
}
else
{
die($error);
}
?>
</body>
</html>
And this is what is displayed in html.
queryExec($query, $error)) { die($error); } //insert data into database $query = 'INSERT INTO Movies (Title, Director, Year) ' . 'VALUES ("The Dark Knight", "Christopher Nolan", 2008); ' . 'INSERT INTO Movies (Title, Director, Year) ' . 'VALUES ("Cloverfield", "Matt Reeves", 2008); ' . 'INSERT INTO Movies (Title, Director, YEAR) ' . 'VALUES ("Beverly Hills Chihuahua", "Raja Gosnell", 2008)'; if(!$database->queryExec($query, $error)) { die($error); } //read data from database $query = "SELECT * FROM Movies"; if($result = $database->query($query, SQLITE_BOTH, $error)) { while($row = $result->fetch()) { print("Title: {$row['Title']}" . "Director: {$row['Director']}". "Year: {$row['Year']}"); } } else { die($error); } ?>
I am running this on Mac OS X on my localhost apache server.
Any help would be awesome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为
和
->
之间的代码不包含或>符号它被视为某种 html 标签。所以,如果有这个标签,你就可以看到外面的一切。这意味着 - php 未安装(正确)
尝试以下操作:检查您的 httpd.conf 中是否有这样的字符串:
AddType application/x-httpd-php .php
并尝试将文件另存为 .phpsince code between
<?php
and->
contains no < or > symbols it's treated like some kind of html tag. so, you see everything outside if this tag. this means - php isn't installed (properly)try this: check your httpd.conf for such string:
AddType application/x-httpd-php .php
and try to save your file as .php如果您使用的是 .html 文件,请尝试将其重命名为 .php。
If you're using a .html file, try renaming it to .php.
你确定你的 php 安装正常吗?尝试运行此脚本(test.php):
Are you sure your php installation is working? Try running this script (test.php):
如果你说的是 ?> 之后的代码作为 html 运行那么它完全没问题..因为?>是 php 的结束标记..
“”下的任何内容都被视为 php,其之外的内容被视为 html...
或者 php 服务器未正确安装,因此所有内容都被视为 html 代码..
if you are saying that code after ?> running as html then it is completely fine..because ?> is closing tag for php ..
anything under "" is considered as php and outside of it consider as html...
or php server is not installed properly so that everything is considered as html code..
当然你的Apache没有正确配置php,它只是发送原始页面,不仅仅是在>之后,检查浏览器中的查看源代码你可以看到你的所有代码
Surely your Apache is not configured properly with php, its just sending the raw page, not only after >, check view-source in your browser you can see all your code
您为脚本使用了什么文件名?
What filename have you used for the script?
您的代码不在服务器上执行。 Apache 提供您的 php 源代码。您的网络浏览器认为
是一个开始标记,但它无法识别它并且不显示该标记。这里你的浏览器认为我提到的标签结束:
if(!$database->queryExec($query, $error))
。看到这一行中的>
了吗?因此,您的浏览器认为从到
>
的所有内容都是未知标签,它会忽略并且不显示该标签。在浏览器中右键单击页面并选择查看源代码。然后你应该看到完整的源代码。确保您的 apache 中已正确安装 mod-php 或您的 cgi-mod 设置正确。检查您的 httpd.conf 中是否有已注册的文件扩展名(应类似于
AddType application/x-httpd-php .php .phtml .php3
)。确保您的 php 文件具有适合此配置的文件存在扩展名。Your code is not executed on the server. Apache delivers your php-Sourcecode. Your webbrowser thinks that
<?php
is an opening tag, but it does not recognize it and does not display the tag. Here your browser thinks that the tag I mentioned ends:if(!$database->queryExec($query, $error))
. See the>
in this line? So your browser believes that everything from<?php
to this>
is an unknown tag, which it ignores and does not display. In your Browser right click on the page and select view source code. Then you should see the full source.Make sure that you have mod-php correctly installed in your apache or that you have your cgi-mod setup correctly. Check your httpd.conf for the registered file extensions (should look like
AddType application/x-httpd-php .php .phtml .php3
). Make sure that your php-file has a file existensions that fits to this configuration.