I have a code in two files and I want to convert it from mysql to mysqli.

func.php:

<?php
$con = mysql_connect('*','*','*');
mysql_select_db("*",$con);

function search_results($keywords){
    $returned_results = array(); 
    $where = "";

    $keywords = preg_split('/[\s]+/',$keywords);
    $total_keywords = count($keywords);

    foreach ($keywords as $key => $keyword) {
            $keyword = strtolower($keyword);
            $where .= "LOWER(`keywords`) LIKE '%$keyword%'";
            if ($key != ($total_keywords -1)) {
            $where .= " AND ";
    }
}
    $results = "SELECT name, image_url, game_url, alt FROM search_games WHERE $where";
    $results_num = ($results = mysql_query($results))? mysql_num_rows($results):0;
    if($results_num === 0){
        return false;
    }
    else{
        while ($results_row = mysql_fetch_assoc($results)) {
    $returned_results[] = array(
        'name' => $results_row['name'],
        'image_url' => $results_row['image_url'],
        'game_url' => $results_row['game_url'],
        'alt' => $results_row['alt']
    );
}
        return $returned_results;
    }
}
?>

search_site.php:

<?php
include('func.php');

echo '<div class="number-of-items">';

if(isset($_POST['keywords'])){
    $suffix = "";
    $keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
    $errors = array();
        $results = search_results($keywords);
        if ($results === false) {
        echo '<h1>We didn\'t find anything for &quot;'.$keywords.'&quot;</h1>';
    }
    else {
        $results_num = count($results);
        $suffix = ($results_num!=1)?'s':'';
        echo '<h1 class="number_of_results">'.$results_num.' item'.$suffix.' For &quot;'.$keywords.'&quot;</h1>';

echo '</div>';

echo '<div class="results">';

        foreach($results as $result){
            echo '

<span class="overimage">

<a href="',$result['game_url'],'" target="_blank">

<span class="hoverimage">
<span class="hovertext1line-home">'.$result['name'].'</span><img class="onlinegameimage-home" src="'.$result['image_url'].'" alt='.$result['alt'].'>
</span>

</a>

</span>

';

}
echo '</div>';

}}

?>

Recommended Answers

All 2 Replies

RTFM? The differences between them are minor, but significant. There is no "magic" way to convert mysql to mysqli. It has to be done manually, and painstakenly. Not hard, but patience and attention to detail is important.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.