在上一个locate命令

时间:2016-02-05 12:07:23

标签: php mysql contao isotope

所以我得到了我的"小"代码在这里:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

//Verbindung
$connection = mysqli_connect('localhost', 'user', 'pw', 'db');

// search stuff
// Treuhandbetrag case 1
$suchstringanfang_treuhand1 = 'treuhand_betrag\"';
$suchstringende_treuhand1 = '\";';

// Treuhandbetrag case 2
$suchstringanfang_treuhand2 = 'treuhand_price\"';
$suchstringende_treuhand2 = '\";';

//queries
//Treuhandabfrage case 1
$query_treuhand1 = "SELECT SUBSTRING(LEFT(configuration, LOCATE('$suchstringende_treuhand1', configuration) +0), LOCATE('$suchstringanfang_treuhand1', configuration) +0, 100)
FROM tl_iso_product_collection_item
WHERE LOCATE('$suchstringende_treuhand1', configuration) > 0
AND LOCATE('$suchstringanfang_treuhand1', configuration) > 0 ORDER BY id DESC LIMIT 1";

//Treuhandabfrage case 2
$query_treuhand2 = "SELECT SUBSTRING(LEFT(configuration, LOCATE('$suchstringende_treuhand2', configuration) +0), LOCATE('$suchstringanfang_treuhand2', configuration) +0, 100)
FROM tl_iso_product_collection_item
WHERE LOCATE('$suchstringende_treuhand2', configuration) > 0
AND LOCATE('$suchstringanfang_treuhand2', configuration) > 0 ORDER BY id DESC LIMIT 1";

//results
// Treuhandbetrag
$result_treuhand1 = mysqli_query($connection, $query_treuhand1);
$result_treuhand2 = mysqli_query($connection, $query_treuhand2);

//Display results
if($result_treuhand2 === FALSE) { 
    echo mysqli_error($connection);
} else
    while($row = mysqli_fetch_array($result_treuhand2)){
print_r($row[0]);
}

//Close connection
mysqli_close($connection); 
?>

我基本上想要做的是在

之间找到一个字符串

treuhand_betrag

和NEXT发生

&#34 ;;

这是因为商店将所有属性数据存储在数据库中的一个TEXT字段中。我知道这是一个紧急解决方案,但我迫切需要一些停顿。到目前为止我的代码还没有给我任何结果。如果我更换&#34 ;;通过一些独特的东西,例如&#39; treuhand_price&#39;,只在现场发生一次,我确实得到了理想的结果。

TEXT字段的外观如何: 如果某人提供了所有信息,您可以期望该字段包含以下内容:

a:14:{s:6:"roller";s:1:"9";s:16:"treuhand_abfrage";s:2:"37";s:15:"treuhand_betrag";s:4:"2000";s:13:"treuhand_price";s:2:"35";s:11:"abhol_firma";s:18:"NichtdieselbeFirma";s:13:"abhol_vorname";s:14:"AndererVorname";s:14:"abhol_nachname";s:15:"AndererNachname";s:12:"abhol_str_nr";s:15:"AndereStraße 7";s:9:"abhol_plz";s:6:"456465";s:9:"abhol_ort";s:12:"Dschingistan";s:10:"abhol_land";s:3:"Bla";s:13:"abhol_telefon";s:6:"564654";s:11:"abhol_email";s:10:"d.web@d.de";s:11:"bemerkungen";s:5:"aeawe";}

如果有人不在乎并且没有提供太多信息:

a:8:{s:6:"roller";s:1:"9";s:16:"treuhand_abfrage";s:2:"36";s:13:"treuhand_price";s:2:"35";s:13:"abhol_vorname";s:5:"Deniz";s:14:"abhol_nachname";s:5:"Cetin";s:12:"abhol_str_nr";s:10:"straße 38";s:9:"abhol_plz";s:5:"90461";s:9:"abhol_ort";s:9:"Nürnberg";}

我会很乐意提供一些帮助或提示!提前谢谢!

1 个答案:

答案 0 :(得分:0)

这就是你想要的。它有点棘手。所以我反转字符串(和搜索字符串)以使用SUBSTR_INDEX从中获取其余部分。我将您的字段名称更改为此示例中的STRING

SELECT
  SUBSTRING_INDEX(REVERSE(
   SUBSTRING_INDEX(SUBSTRING_INDEX(
   REVERSE('a:14:{s:6:"roller";s:1:"9";s:16:"treuhand_abfrage";s:2:"37";s:15:"treuhand_betrag";s:4:"2000";s:13:"treuhand_price";s:2:"35";s:11:"abhol_firma";s:18:"NichtdieselbeFirma";s:13:"abhol_vorname";s:14:"AndererVorname";s:14:"abhol_nachname";s:15:"AndererNachname";s:12:"abhol_str_nr";s:15:"AndereStraße 7";s:9:"abhol_plz";s:6:"456465";s:9:"abhol_ort";s:12:"Dschingistan";s:10:"abhol_land";s:3:"Bla";s:13:"abhol_telefon";s:6:"564654";s:11:"abhol_email";s:10:"d.web@d.de";s:11:"bemerkungen";s:5:"aeawe";}')
    ,'garteb_dnahuert'
    ,1),';"',-2)),'"',-1);

<强>结果

2000

如果您搜索的是

,请告诉我

我从我的答案复制到mysql客户端:

MariaDB [(none)]> SELECT
    ->   SUBSTRING_INDEX(REVERSE(
    ->    SUBSTRING_INDEX(SUBSTRING_INDEX(
    ->    REVERSE('a:14:{s:6:"roller";s:1:"9";s:16:"treuhand_abfrage";s:2:"37";s:15:"treuhand_betrag";s:4:"2000";s:13:"treuhand_price";s:2:"35";s:11:"abhol_firma";s:18:"NichtdieselbeFirma";s:13:"abhol_vorname";s:14:"AndererVorname";s:14:"abhol_nachname";s:15:"AndererNachname";s:12:"abhol_str_nr";s:15:"AndereStraße 7";s:9:"abhol_plz";s:6:"456465";s:9:"abhol_ort";s:12:"Dschingistan";s:10:"abhol_land";s:3:"Bla";s:13:"abhol_telefon";s:6:"564654";s:11:"abhol_email";s:10:"d.web@d.de";s:11:"bemerkungen";s:5:"aeawe";}')
    ->     ,'garteb_dnahuert'
    ->     ,1),';"',-2)),'"',-1);
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SUBSTRING_INDEX(REVERSE(
   SUBSTRING_INDEX(SUBSTRING_INDEX(
   REVERSE('a:14:{s:6:"roller";s:1:"9";s:16:"treuhand_abfrage";s:2:"37";s:15:"treuhand_betrag";s:4:"2000";s:13:"treuhand_price";s:2:"35";s:11:"abhol_firma";s:18:"NichtdieselbeFirma";s:13:"abhol_v |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2000                                                                                                                                                                                                                                                             |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>
相关问题