0

I have the following code:

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>';
}

And that works, but, how can i change "B" with a php variable (like... $letter) which contains a letter?

For example...

$resultado = $mysqli->query('select title FROM 4d_topics 
                             where title Like **"$Letter%"** and forum_id=174');
1
  • Are you asking about string concatenation? Try, $resultado = $mysqli->query('select title FROM 4d_topics where title Like %' . $Letter . '% and forum_id=174'); Kinda weird how you know how to echo the error message with brackets. Here's a good read. Commented Jul 13, 2015 at 1:05

2 Answers 2

1

You can use this code (puedes usar este código):

$letter = 'B';
$resultado = $mysqli->query('select title FROM 4d_topics where title Like "'.$letter.'%" and forum_id=174');
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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>';
}

with this code you can change the like from the start %B or %B% or B%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.