在php中通过许多变量回显文本

时间:2011-07-22 13:25:23

标签: php mysql

我有3个vars,由php转发(index.php?a = v1&& b = v6&& c = v10)

现在我想通过这些特定的变量来回显一个文本(来自mysql)。

MySQL的表:

的index.php A = V1&安培;和b = V6&安培;和C = V10

    id | 1
    variable1 | "v1"
    variable2 | "v6"
    variable3 | "v10"
    text | "this is the text for v1&v6&v10"

的index.php A = V3&安培;和b = V8&安培;和C = V12

    id | 2
    variable1 | "v3"
    variable2 | "v8"
    variable3 | "v12"
    text | "this is the text for v3&v8&v12"

什么是mysql-query?

$query = "SELECT * FROM text_table WHERE ???;";

3 个答案:

答案 0 :(得分:0)

SELECT
     a,
     b,
     c,
     'This is the text for ' + a + '&' + b + '&' + c
FROM mysql-table

这是你想要的吗?

答案 1 :(得分:0)

$query = mysql_query('SELECT * FROM my_table WHERE a = '.$_GET['a'].' AND b = '.$_GET['b'].' AND c = '.$_GET['c']);
$affectedRows = mysql_affected_rows();
for($i = 0; $i < $affectedRows; $i++){
    $currentResult = mysql_fetch_assoc($query);
    $a = $currentResult['a'];
    $b = $currentResult['b'];
    $c = $currentResult['c'];
    echo "this is the text for {$a}&{$b}&{$c}";
}

如果这就是你要求的。如果需要插入这些值,请使用以下查询:

mysql_query('INSERT INTO my_table SET a = "'.$_GET['a'].'", b = "'.$_GET['b'].'", c = "'.$_GET['c'].'" ');
然而,这是非常不安全的。我建议,在插入这些值之前,请执行以下操作:

$sqlA = mysql_real_escape_string($_GET['a']);
$sqlB = mysql_real_escape_string($_GET['b']);
$sqlC = mysql_real_escape_string($_GET['c']);
mysql_query('INSERT INTO my_table SET a = "'.$sqlA.'", b = "'.$sqlB.'", c = "'.$sqlC.'" ');

答案 2 :(得分:0)

<a href="index.php?a=v1&b=v6&c=v10">Link 1</a>

<?php 

if(isset($_GET)){
    foreach($_GET as $key=>$value){
        if(get_magic_quotes_gpc()) {
            $value=stripslashes($value);
        }
        $_GET[$key]=mysql_real_escape_string($value);
    }
}

$query = mysql_query('SELECT *
                        FROM my_table 
                        WHERE a LIKE '.$_GET['a'].' 
                        AND b LIKE '.$_GET['b'].' 
                        AND c LIKE '.$_GET['c']);

if(mysql_num_rows($query)>=1){
    while($row=mysql_fetch_assoc($query)){
        $a = $row['a'];
        $b = $row['b'];
        $c = $row['c'];
        echo "This is the text for {$a}&{$b}&{$c}";
    }
}else{
    echo 'No results';
}

?>