解析错误:语法错误,在 ....在第 17 行 [英] Parse error: syntax error, unexpected T_STRING in . . . . on line 17

查看:53
本文介绍了解析错误:语法错误,在 ....在第 17 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个将放入 foreach 语句的字符串.该字符串将决定哪些字段将从数据库中出现.由于我无法预先确定将从表单中选择哪些字段,因此我认为这将是处理未知并显示查询结果的最佳方式.

I'm trying to create a string that will be put into a foreach statement. The string will determine which fields will appear from the database. Since I cannot determine which fields will be chosen from the form in advance, I thought this would be the best way to deal with the unknown and show the results of the query.

我以相同的方式构建了查询并且它起作用了.我试过双引号和单引号,我只是不明白为什么它不想构建这个字符串.请帮忙(如果这是一个愚蠢的问题,请原谅我,因为我是 PHP 新手 - 我可以在类似的搜索中找到匹配的答案).谢谢你!:-)

I built the query the same way and it worked. I've tried double and single quotes and I'm just not understanding why it doesn't want to build this string. Please help (and excuse me if this is a stupid question as I'm new to PHP - I could find an answer that matched in the similar searches). THANK YOU! :-)

<?php
  include('database.php');


  function show_products($table, $productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb)
  {
        $list = "";
        global $db;
        $theQuery = 'select ';
        if($productIDcb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'productID, ';
                $list .=' $products['productID']';  //THIS IS LINE 17
         }
        if($categoryIDcb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'categoryID, ';
                $list .=' $products['categoryID']';

         }
        if($productCodecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'productCode, ';
                $list .=' $products['productCode']';

         }
        if(isset($_POST['productNamecb']))
         {
                $theQuery == $theQuery;
         }
        else
         {
                 $theQuery .= 'productName, ';
                 $list .=' $products['productName']';
         }
        if(isset($_POST['listPricecb']))
         {
                $theQuery .= 'listPrice, ';
         }
        else
         {
                $theQuery .= 'listPrice, ';
                $list .=' $products['listPrice']';
         }

        $theQuery .=' "" from ' .$table;
        echo($theQuery);
        $rSet = $db -> query($theQuery);
        foreach($rSet AS $products)
          {
              $list .= "<br>";
          }
        echo($list);
  }



?>

推荐答案

你使用了两次单引号

$list .=' $products['productID']';

尝试对字符串使用不同的引号:例如,

Try using different quotes for strings: for example,

$list .=" $products['productID']";//这将产生一个类似

$products['productID']

当编译器在字符串的开头遇到引号 ' 时,它期望另一个引号将其关闭.在您的原始代码中,第二个引号过早出现,使您的字符串成为

When the compiler encounters a quote at the start of the string, ', it expects another one to close it. In your original code, the second quote appears prematurely making your string become

' $products['

这使得编译器无法理解下一部分

which makes the next part become incomprehensible to the compiler

productID']'

另外,如果你想在字符串中包含一个变量,你需要双引号",而不是单引号'.

Also, if you want to include a variable within the string, you need double quotes ", not single '.

最后,你不能直接在这样的带引号的字符串中访问数组.尝试连接:

Lastly, you can't access arrays directly in a quoted string like that. Try concatenating:

$list .=' ' .$products['productID'];

*编辑

关于@marcelkorpel 建议的说明.您可以使用花括号,但注释中有一个轻微的错字.请使用

A note on @marcelkorpel 's suggestion. You can use curly braces, but there is a slight typo in the comment. Please use

"{$products['productID']}" 而不是 "${products['productID']}"

这篇关于解析错误:语法错误,在 ....在第 17 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆