我收到解析错误任何人都可以帮助我[关闭]

Posted

技术标签:

【中文标题】我收到解析错误任何人都可以帮助我[关闭]【英文标题】:I am getting the parse error Can anybody Please Help me [closed] 【发布时间】:2015-03-15 13:59:13 【问题描述】:

解析错误:语法错误,意外';'在 C:\wamp\www\12.01.2015 Class 01\Coffee Website\Model\CoffeeModel.php 第 106 行

<?php

require ("Entities/CoffeeEntity.php");

// contains database related code for the coffee type

class CoffeeModel



//  Get all coffee types from the database and return them in an array

  function GetCoffeeTypes()



 require 'Credentials.php';


 //Open connection and Select database

mysql_connect($host, $user, $password) or die (mysql_error());

mysql_select_db($database);

$result = mysql_query("SELECT DISTINCT type FROM coffee") or 

die(mysql_error());

$types = array();


// Get data from databse

while($row = mysql_fetch_array($result))


        array_push($types, $row[0]);

     

/ Close connection and return

 mysql_close();

 return $types;  



// GET coffeeEntity objects from the database and return them in an array.

function GetCoffeeByType($type)


    require 'Credentials.php';


// Open connection and select database

mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);


$query = "SELECT * FROM coffee WHERE type LIKE '$type'";

 $result = mysql_query($query) or die (mysql_error());

$coffeeArray = array();


//GET Data from Database

while ($row = mysql_fetch_array($result))
     

 $name = $row[1];

 $type = $row[2];

 $price = $row[3];

 $roast = $row[4];

 $country = $row[5];

 $image = $row[6];

 $review = $row[7];


// Create Coffee objects and store them in an array

$coffee = new CoffeeEntity (-1, $name, $type, $price, $roast, $country, $image, $review);

array_push($coffeeArray, $coffee);



// CLose connection and return result

mysql_close();

return $coffeeArray;

     


function GetCoffeeById($id)

    require 'Credentials.php';


// Open connection and select database

mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);


$query = "SELECT * FROM coffee WHERE id = $id";

 $result = mysql_query($query) or die (mysql_error());



//GET Data from Database

 while ($row = mysql_fetch_array($result))



   $name = $row[1];

 $type = $row[2];

 $price = $row[3];

 $roast = $row[4];

 $country = $row[5];

  $image = $row[6];

 $review = $row[7];


        // Create Coffee 


 $coffee = new CoffeeEntity ($id, $name, $type, $price, $roast, $country, 

$image, $review);

 

 // CLose connection and return result

  mysql_close();

return $coffee;

 


 function InsertCoffee (CoffeeEntity $coffee)

 

   $query = sprintf ("INSERT INTO coffee

   (name, type, price, roast, country, image, review)

 VALUES 

 ('%s','%s','%s','%s','%s','%s','%s')",

 mysql_real_escape_string($coffee->name),

  mysql_real_escape_string($coffee->type),

 mysql_real_escape_string($coffee->price),

 mysql_real_escape_string($coffee->roast),

 mysql_real_escape_string($coffee->country),

 mysql_real_escape_string("Images/Coffee/". $coffee->image),

 $this->PerformQuery($query);

 



 function UpdateCoffee($id, CoffeeEntity $coffee)

 

  $query =("UPDATE coffee

       SET name = '%s', type = '%s', price = '%s', roast = '%s',


       country = '%s', image = '%s', review = '%s'

       WHERE id = $id"


  mysql_real_escape_string($coffee->name),

  mysql_real_escape_string($coffee->type),

 mysql_real_escape_string($coffee->price),

 mysql_real_escape_string($coffee->roast),

 mysql_real_escape_string($coffee->country),

 mysql_real_escape_string("Images/Coffee/". $coffee->image),

 $this->PerformQuery($query);


                        );

 $this-> PerformQuery($query);

 

        function DeleteCoffee($id)

 

   $query = "DELETE FROM coffee WHERE id = $id";

 $this->PerformQuery($query);

 


function PerformQuery ($query)



require ('Credentials.php');

 mysql_connect($host, $user, $password) or die(mysql_error());

 mysql_select_db($database);


//Execute query and close connection

 mysql_query($query) or die(mysql_error());

 mysql_close();





?>

【问题讨论】:

我们需要查看文件,我不知道上面的所有内容该怎么做...编辑:看到更新,现在检查 第 106 行是什么? $this->PerformQuery($query); 这是问题中贴出的o6行文件完整代码 拜托,stop using mysql_* functions。 它们不再维护,而是 officially deprecated。 了解prepared statements,然后使用PDO。 【参考方案1】:

第一个错误在第 42 行(缺少第二个斜杠以使其成为注释):

/ Close connection and return

改成:

// Close connection and return

下一个错误在第 188 行(缺少审查的值),为避免语法错误并插入一个空字符串进行审查,请将其更改为:

 mysql_real_escape_string("Images/Coffee/". $coffee->image),

到:

 mysql_real_escape_string("Images/Coffee/". $coffee->image),  "");

下一个错误在第 210 行(缺少 sprintf),所以更改

  $query =("UPDATE coffee

到:

  $query = sprintf("UPDATE coffee

然后第 207 行更改

   WHERE id = $id"

   WHERE id = $id",

然后第222行改变

 $this->PerformQuery($query);

到:

""

【讨论】:

@Sheikh Noman Mehmood:因为您犯了几个语法错误,请参阅编辑。 @Sheikh:回复帮助时,请避免简单地添加“它没有解决”或“它不起作用”。您没有为海报提供任何额外信息来帮助您。尝试进行一些调试,或准确解释如何它不起作用。更多细节 = 更好的答案!【参考方案2】:
 $this->PerformQuery($query);

正在抛出错误。不确定您要做什么,但语法不正确。

【讨论】:

抱歉,下次先简单调试一下,再浪费大家的时间。我们不是来调试您的语法错误的。【参考方案3】:

就在函数InsertCoffee中:

$query = sprintf ("INSERT INTO coffee etc etc" 
// Snip    
mysql_real_escape_string("Images/Coffee/". $coffee->image),

// You never had the closing paren ) to sprintf()

$this->PerformQuery($query);

您需要去掉最后的mysql_real_escape_string() 后面的逗号,然后用); 关闭对sprintf() 的调用。

【讨论】:

而且每个 mysql_real_escape_string 行都以逗号而不是分号结尾,我不知道为什么...... @CharlotteDunois 因为它们都是sprintf() 的参数。应该没问题。 @CharlotteDunois 没有解决我的问题

以上是关于我收到解析错误任何人都可以帮助我[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

一次又一次地解析错误[关闭]

解析错误:语法错误,意外'' [关闭]

如何修复解析器错误消息:无法加载类型?

在 Swift 中解析 JSON 24 小时的问题 [关闭]

任何人都可以帮助我有一个错误 NameError: name 'bgr_image' is not defined [关闭]

解析错误:语法错误,意外的“公共”(T_PUBLIC)[关闭]