【问题标题】:About sql and str_replace function关于sql和str_replace函数
【发布时间】:2016-04-02 14:53:45
【问题描述】:

首先感谢大家对我的帮助,请原谅我的英语,我正在努力做到最好:)

这就是我想做的:

我在 MySQL 数据库中有一个名为 acciones 的表,我希望在该表中注册的每个“操作”都在屏幕上打印的文本中进行替换:

<?php
    include('../procesos/abre_conexion.php'); 
    $query99 = "SELECT * from acciones";     
    // This line make the query 
    $result99 = mysql_query($query99);  
    $registro99 = mysql_fetch_array($result99)   
?> 
<?php 
    // create the function 
    function howls($valor) { 
        // set the variable // saving as array // possible texts 
        $variables = array('$registro99[original]',); 
        // $imagenes, tambien contendra un array 
        // with the replace text 
        $sus = array('$registro99[reemplazo]',); 
        // making the replace 
        return (str_replace($variables, $sus, $valor)); 
    } 
    // starting function 
    ob_start("howls"); 
?> 
#SalirA beber Cerveza Montoro en barrio santo con Andreita. 
<?php 
    // ending function 
    ob_end_flush(); 
?>
<? include('../procesos/cierra_conexion.php'); ?>

但它不起作用,有什么建议吗?

【问题讨论】:

    标签: php sql str-replace


    【解决方案1】:

    首先,you should NOT be using the mysql_* functions。它们已被弃用并从 PHP7 中完全删除。

    关于您的代码发生了什么。您不应该单引号 $registro 变量。如果您出于某种原因确实需要引用它们,您需要 double-quote 他们。

    function howls($valor) { 
        // set the variable // saving as array // possible texts 
        $variables = array("{$registro99[original]}",); 
        // $imagenes, tambien contendra un array 
        // with the replace text 
        $sus = array("{$registro99[reemplazo]}",); 
        // making the replace 
        return (str_replace($variables, $sus, $valor)); 
    } 
    

    【讨论】:

    • 嗨!感谢您的回答 GentlemanMax :) 我已经按照您所说的尝试过,但仍然无法正常工作:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多