2016-02-20 246 views
0

我得到一個語法錯誤,當我運行我的腳本解析錯誤:語法錯誤,意外'

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\Program Files (x86).................\get.php on line 24

我不能看到什麼是錯,誰能幫我呢?

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "database; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname) ; 
// Check connection 
if ($conn->connect_error) 
    { 
    die('Connection failed: ' . $conn->connect_error) ; 
    } 
else 
    { 
    $product1 = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) ; 

    $query = 'SELECT price FROM forms WHERE name=' . $product1 . ' ' ; 

    $res = mysql_query($query) ; 
    if (mysql_num_rows($res) > 0) 
    { 
    $result = mysql_fecth_assoc($res) ; 
     echo json_encode($result['price']); 
    } 
    else 
     { 
     echo json_encode('no results') ; 
     } 

    } 
?> 

回答

3

有在你的代碼相當多的錯誤,除了在$dbname = "database;缺少的報價。

你在這裏混的MySQL的API。 mysql_不與mysqli_ API混用。

所以,你需要改變的mysql_所有實例mysqli_並通過連接參數查詢。

然後,mysql_fecth_assoc被拼寫錯誤和糾正與添加的i

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

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname) ; 
// Check connection 
if ($conn->connect_error) 
    { 
    die('Connection failed: ' . $conn->connect_error) ; 
    } 
else 
    { 
    $product1 = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) ; 

    $query = 'SELECT price FROM forms WHERE name=' . $product1 . ' ' ; 

    $res = mysqli_query($conn, $query) ; 
    if (mysqli_num_rows($res) > 0) 
    { 
    $result = mysqli_fetch_assoc($res) ; 
     echo json_encode($result['price']); 
    } 
    else 
     { 
     echo json_encode('no results') ; 
     } 

    } 

檢查錯誤也:

1

變化

$dbname = "database; 

$dbname = "database"; 
1

你錯過了這一行業的"

$dbname = "database; 

它應該是:

$dbname = "database"; 

希望這會有所幫助,謝謝!

相關問題