使用php变量和“%”进行mysql查询

时间:2015-07-13 01:01:21

标签: php mysql

我有以下代码:

if ($mysqli->connect_errno) {
    echo "Fallo al contenctar a MySQL: ("
         . $mysqli->connect_errno
         . ") "
         . $mysqli->connect_error;
}

$resultado = $mysqli->query('select title FROM 4d_topics 
                             where title Like "B%" and forum_id=174');

$row_cnt = $resultado->num_rows;
for ($i = 0; $i <= $row_cnt; $i++) {
    $resultado->data_seek($i);
    $row = $resultado->fetch_row();
    printf ($row[0]);
    echo '<br>';
}

这样可行,但是,我如何用包含字母的php变量(如... $ letter)更改“B”?

例如......

$resultado = $mysqli->query('select title FROM 4d_topics 
                             where title Like **"$Letter%"** and forum_id=174');

2 个答案:

答案 0 :(得分:1)

您可以使用此代码(puedes usarestecódigo):

local myclasses = { 'Primary', 'Secondary', 'Heavy' }
local myprimaries = { 'Auto Rifle', 'Scout Rifle', 'Pulse Rifle', 'Sniper     Rifle', 'Hand Cannon' }
local mysecondaries = { 'Shotgun', 'Sidearm', 'SMG/SEG' }
print( myclasses[ math.random(#myclasses) ] )
if 'Primary' then
  print( myprimaries[ math.random(#myprimaries) ] )
elseif 'Secondary' then
  print( mysecondaries[ math.random(#mysecondaries) ] )
end

答案 1 :(得分:0)

试试这个:

function searching_letter($value){
    return $mysqli->query('select title FROM 4d_topics 
                         where title Like '.$value.' and forum_id=174');
}

if ($mysqli->connect_errno) {
    echo "Fallo al contenctar a MySQL: ("
         . $mysqli->connect_errno
         . ") "
         . $mysqli->connect_error;
}

$value = 'B%';
$resultado = searching_letter($value);

$row_cnt = $resultado->num_rows;
for ($i = 0; $i <= $row_cnt; $i++) {
    $resultado->data_seek($i);
    $row = $resultado->fetch_row();
    printf ($row[0]);
    echo '<br>';
}

使用此代码,您可以从开始%B%B%B%

更改内容