2015-11-04 67 views
-3

我有一个MySQL数据库从哪里我旁边提取基于用户搜索的数据。

我没有问题显示后变量是这样的:

<?php 

if (isset($_POST)) { 
$name = htmlspecialchars($_POST['name']);  
echo "Results - " . $name ."\n"; 
} 

$servername = "localhost"; 
$username = "root"; 
$password = "root"; 
$dbname = "test"; 

$conn = new mysqli($servername, $username, $password, $dbname); 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 

$conn->close(); 
?> 

错误 -

Parse error: syntax error, unexpected end of file.

只要我添加了一个MySQL连接,甚至没有任何的查询工作要做,我得到一个解析错误。这是为什么 ?

我已经尝试添加特定查询,因为我需要显示结果。

我也尝试使用不同于提交,仍然没有运气,得到相同的解析错误。

+0

添加完整的代码。那么只有我们可以看到什么是错的。 –

+0

<?php if(isset($ _ POST)){ $ name = htmlspecialchars($ _ POST ['name']); 回声“结果 - ”。 $ name。“\ n”; } $ servername =“localhost”; $ username =“root”; $ password =“root”; $ dbname =“test”; $ conn = new mysqli($ servername,$ username,$ password,$ dbname); ($ conn-> connect_error){ die(“Connection failed:”。$ conn-> connect_error); $ conn-> close(); > 错误越来越:解析错误:语法错误,文件 –

+0

意外结束添加完整的代码,并添加错误以及 –

回答

0

我认为你有一个语法错误。

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 

} <--- u have missed this 

$conn->close(); 

最终代码

<?php 

if (isset($_POST)) { 
$name = htmlspecialchars($_POST['name']);  
echo "Results - " . $name ."\n"; 
} 

$servername = "localhost"; 
$username = "root"; 
$password = "root"; 
$dbname = "test"; 

$conn = new mysqli($servername, $username, $password, $dbname); 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 

} <------- this is missing in your code. 

$conn->close(); 

?> 
+0

好吧......那很尴尬,它总是很明显:)非常感谢! –

+0

很高兴帮助你:) –