【发布时间】:2015-07-23 15:01:37
【问题描述】:
第一个链接指向我就与使用 Heredoc 语法I am unable to fix an issue concerning an HEREDOC syntax error相关的问题提出的问题
第二个链接指向一个与上述链接非常相似的问题 Issue with heredoc and PHP
我遇到了一个关于从 php 页面中的数据库中检索数据的问题。问题与Heredoc无关,Heredoc在代码中,但问题是由于sql语法错误引起的。
<?php
// riceve l'identifictivo di un regista e ne restituisce il nome completo
function get_director($director_id) {
global $db;
$query = 'SELECT
people_fullname
FROM
people
WHERE
people_id = ' . $director_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
// riceve l'identificativo di un attore principale e ne restituisce il nome
// completo
function get_leadactor($leadactor_id) {
global $db;
$query = 'SELECT
people_fullname
FROM
people
WHERE
people_id = ' . $leadactor_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
// riceve l'identificativo di un tipo di film e ne restituisce la
//descrizione
function get_movie($type_id) {
global $db;
$query = 'SELECT
movietype_label
FROM
movietype
WHERE
movietype_id = ' . $type_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype_label;
}
// funziona per calcolare se un film ha generato un profitto, una perdita o è
// in pareggio
function calculate_differences($takings, $cost) {
$difference = $takings - $cost;
if ($difference < 0) {
$color = 'red';
$difference = '$' . abs($difference) . ' million';
} elseif ($difference > 0) {
$color ='green';
$difference = '$' . $difference . ' million';
} else {
$color = 'blue';
$difference = 'broke even';
}
return '<span style="color:' . $color . ';">' . $difference . '</span>';
}
// collegamento a MYSQL
$db = mysql_connect('localhost', 'pippo', 'pluto') or
die ('Unable to connect. Check your connection parameters. ');
mysql_select_db('moviesite', $db) or die(mysql_error($db));
// recupera le informazioni
$query = 'SELECT
movie_name, movie_year, movie_director, movie_leadactor,
movie_type, movie_running_time, movie_cost, movie_takings
FROM
movie
WHERE
movie_id = ' . $_GET['movie_id'];
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
$movie_name = $row['movie_name'];
$movie_director = get_director($row['movie_director']);
$movie_leadactor = get_leadactor($row['movie_leadactor']);
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time'] .' mins';
$movie_takings = $row['movie_takings'] . ' million';
$movie_cost = $row['movie_cost'] . ' million';
$movie_health = $calculate_differences($row['movie_takings'],
$row['movie_cost']);
// mostra le informazioni
echo <<<ENDHTML
<html>
<head>
<title>Details and Reviews for: $movie_name</title>
</head>
<body>
<div style="text-align: center;">
<h2>$movie_name</h2>
<h3><em>Details</em></h3>
<table cellpadding="2" cellspacing="2"
style="width: 70%; margin-left: auto; margin-right: auto;">
<tr>
<td><strong>Title</strong></strong></td>
<td>$movie_name</td>
<td><strong>Release Year</strong></strong></td>
<td>$movie_year</td>
</tr><tr>
<td><strong>Movie Director</strong></td>
<td>$movie_director</td>
<td><strong>Cost</strong></td>
<td>$$movie_cost<td/>
</tr><tr>
<td><strong>Lead Actor</strong></td>
<td>$movie_leadactor</td>
<td><strong>Takings</strong></td>
<td>$$movie_takings<td/>
</tr><tr>
<td><strong>Running Time</strong></td>
<td>$movie_running_time</td>
<td><strong>Health</strong></td>
<td>$movie_health<td/>
</tr>
</table></div>
</body>
</html>
ENDHTML;
?>
这是我正在处理的语法错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 7 行的 '' 附近使用
【问题讨论】:
-
请stop using
mysql_*functions。它们不再被维护并且是officially deprecated。改为了解 prepared statements,并考虑使用 PDO,it's not as hard as you think。 第 7 行是什么? -
我知道 mysql_ +functions 已被弃用,但无论如何我都在遵循书中的示例,第“7”行是代码的 sn-p 中发生错误的行
-
这与heredoc 无关。这是您从运行无效查询中得到的 mysql 查询错误。这是
or die(mysql_error($db))部分的输出。我认为这是第三个查询,因为这是唯一一个 7 行长的查询。您可能没有在 url 中传递$_GET['movie_id']。 -
没有很好的方法来说明这一点,但这段代码完全不安全,应该不惜一切代价避免。如果你是从一本书中学到的,我会烧掉这本书,因为生火比教 php 更有用。
-
那么您有什么建议来解决这个问题? @JonathanKuhn 我必须在哪里更正代码才能使整个页面正常工作?提前感谢您的帮助