0

I am getting mental on this. I have tried everything so far, for hours. Here is the task:

In a module override, I use this code:

$db =& JFactory::getDBO();
$title = "Analysen & Auswertungen Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;

This works, but since I am getting the $title dynamically from a variable I need this to work:

$db =& JFactory::getDBO();
$title = "$linktext Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;

I have 6 variables that populate $linktext in a foreach loop, all work except the one with the string including the "&"...

I tried htmlentities and utf8_encode and different kind (actually all combinations) of " and ' in the query... nothing worked.

Whe I use the following sql query in phpmyadmin it works:

SELECT `introtext` FROM `x999x_content` WHERE `title`="Analysen & Auswertungen Infos"

I am really puzzled over this, and right now very tired and angry... Any help will be greatly appreciated!!!

4

4 回答 4

2

有时,长时间的睡眠是您能做的最好的事情!

我只是用来strlen检查$linktext(我通过 foreach 循环从 DB 获得)的长度,发现它比可见字符长。这是合乎逻辑的,因为&which 返回为&

为了$linktext在新的数据库查询中使用它,我需要做的就是解码 html 实体:

$db =& JFactory::getDBO();
// this is the correct way of doing it
$title = html_entity_decode($linktext)." Infos"; 
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query); 
$result = $db->loadRow(); 

或其他答案中指出的查询的任何其他组合 (",',`,$query->select )

于 2011-12-12T10:20:32.753 回答
0

你试过了吗:

$db =& JFactory::getDBO(); 
$title = $linktext." Infos"; 
$query = "SELECT `introtext` FROM `#__content` WHERE `title`='".$title."'"; 
$db->setQuery($query); 
$result = $db->loadRow(); 
echo $result; 

我不知道它是否会起作用,但是我从来没有遇到任何问题,这就是我编写代码的方式。

于 2011-12-11T22:26:58.843 回答
0

尝试更改此行:

$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";

为了这:

$query = $db->getQuery( tru );
$query->select( 'introtext' );
$query->from( '#__content' );
$query->where( 'title=' . $db->Quote( $title );

我希望它有帮助!

于 2011-12-12T06:10:48.490 回答
0

我得到了这个工作:

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

$keyvalue='101';

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM mytable WHERE keyfield='".$keyvalue."'";
$result = $conn->query($sql);

if($result && $row = $result->fetch_assoc()) {
  $fsql='DESCRIBE mytable';
  $fresult = $conn->query($fsql);
  if($fresult){
    while($frow = $fresult->fetch_assoc()) {
      $fieldname=$frow['Field'];
      echo($fieldname . ' = ' . $row[$fieldname] . '<br><br>');
     }
  }
}else echo("Failed: " . $sql);

?>
于 2017-10-20T06:56:02.137 回答