English 中文(简体)
php 语法错误 - 起始器
原标题:php syntax error - beginner

我是一个初学者, 试图获得对 php 的处理。 我遇到了一个我似乎无法解决的语法错误。 我会给你们看看下面的代码和一些我尝试过的修补方法。 如果有人有另一个好主意的话。 谢谢 :

$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if(!$subject_set){
  die("Database query failed: " . mysql_error());
}

while($subject = mysql_fetch_array($subject_set)) {
  echo "<li> {$subject[ menu_name ]} </li>";
}

$page_set = mysql_query("SELECT * FROM pages WHERE id_subjects = {$subject["id"]}", $connection);
if(!$page_set){
  die("Database query failed: " . mysql_error());
}

echo "<ul class= pages >";
while($page = mysql_fetch_array($page_set)) {
  echo "<li> {$page[ menu_name ]} </li>";
}
echo "</ul>";

I get: 数据库查询失败 : 您的 SQL 语法有错误 : 您在 SQL 语法中有一个错误; 请检查与您的 MySQL 服务器版本相对应的手册, 以便右语法在 " 第1行 " 附近使用

我知道问题在于 {$subject["id"]} 因为我把"Where id_subjects = 1"放回了内容,没有错误。

{$subject[ id ]}
{$subject["id"]}

但也有同样的错误...

最佳回答

试试

$page_set = mysql_query("SELECT * FROM pages WHERE id_subjects =  ".$subject["id"]." ", $connection);
                if(!$page_set){
                    die("Database query failed: " . mysql_error());
                }

BTW. 您应该真正远离 Mysql 函数 。 他们正在被贬低, 转移到 PDO 或 Mysqli, 这些功能也非常安全( 您现在很容易被 sql 注入) 。

问题回答

如果你读回你的文章,你可以清楚地看到这里出了什么问题。

"SELECT * FROM pages WHERE id_subjects = {$subject["id"]}"

正如你所看到的,“id”与其余部分没有连接。这是因为“你关闭了字符串”。

简单地修正此功能

"SELECT * FROM pages WHERE id_subjects = " . $subject["id"]

或者如果您真的想要将变量放入字符串中,您可以为密钥使用一个引用的字符串:

"SELECT * FROM pages WHERE id_subjects = {$subject[ id ]}"

我个人是第一种解决办法的粉丝,但这只是我的看法。

当循环结束循环时, 它将会用尽所有结果 。 $subject[ id] 将不会仅仅因为 $subject 不再有任何条目而有任何信息 。

我猜你想先列出所有主题, 然后列出每个主题下的所有页面。

使用 MySQL 并不漂亮, 但这里是您想要做的。 (如 Bono 所说使用 PDO 或 Mysqli, 但这里使用 psuedocode 中的解决方案, 与 MySQL 有效 ) 。

loop through first query
    print subject name
    select pages using subject id
    loop through pages under that subject id
       print page names

在引用的字符串中,您不需要任何引号,只要使用

"SELECT * FROM pages WHERE id_subjects = {$subject[id]}"




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签