Conversione Codice da mysql a mysqli

antoniobarone95

Nuovo Utente
31 Ago 2018
1
0
1
Salve ragazzi devo convertire il codice da mysql a mysqli come posso procedere?

PHP:
<?php

$username = "username";
$password = "password";
$hostname = "hostname";
$database = "database";

mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
mysql_query("set names 'utf8'");

?>
PHP:
<?php
$table_name = "cf_help_center"; //nome tabella del databse
$first_field = "title"; //campo tabella titolo
$second_field = "tag_faq"; //campo tag faq
$faq_software = "faq_software"; //campo tipo software faq
$cf_typology = "cf_typology"; //campo tipo software faq
$category_faq = "category_faq"; //campo categoria faq
$link_ancor = "link_ancor"; //campo link ancora faq
$limitchar = 3; //limitatore caratteri minimi
$records_number = 10;  // numero di ricerca per pagina
$page_number = 1;// pagina iniziale predefinita
?>
PHP:
<?php

// file di connessione al database
include('inc/db.inc.php');

// file di configurazione
include('inc/config.inc.php');

if(isset($_GET['p'])) {
  $page_number = $_GET['p'];
  $arraySearch = $_GET['terms'];
  $show_count = $_GET['count'];
  settype($page_number, 'integer');
}
$nospaces = substr($_GET['terms'],0,4);
  $offset = ($page_number - 1) * $records_number;

// minim 3 characters condition
   if(strlen($_GET['terms']) < $limitchar) {
 echo '<div id="counter">'. $limitchar .' caratteri minimi</div>';
// no spaces in first 4 letters
  } else if(preg_replace('/[a-zA-Z0-9]/', ''))  {
 echo '<div id="counter">Utilizzare lettere o numeri nei primi 4 caratteri</div>';
  } else  {

// explode parole di ricerca in un array
  $arraySearch = explode(" ", $_GET['terms']);
// campi della tabella su dove cercare
  $arrayFields = array(0 => $first_field, 1 => $second_field , 1 => $faq_software);
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM $table_name WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.") LIMIT $offset, $records_number;";
  $search = mysql_query($query);


// numero risultati di ricerca
  $arrayFields = array(0 => $first_field, 1 => $second_field);
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM $table_name WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";
  $count_results = mysql_query($query);
  $numrows = mysql_num_rows($count_results);

// no results
if($numrows == 0) {
        echo '<div id="counter">Nessun Risultato di Ricerca</div>';

// show results
} else {

echo '<div id="results">
<div id="results_top"><p><b>'. $_GET['terms'] .'</b> - <span class="font-result">'. $numrows .' risultati trovati</span></p></div>
';

while($row = mysql_fetch_assoc($search)) {

//$urltitle = str_replace(" ","_", $row['title']);

echo '<div class="item">
<div class="details">
<p style="font-size:13px;">
<span style="font-weight:bold;"><i class="fa fa-comments"></i> '.$row['cf_typology'].'</span>
<span>-</span>
<span>'.$row['faq_software'].'</span>
<span>-</span>
<span>'.$row['category_faq'].'</span>
</p>
<a style="float:left;"></a><a target="_blank;" href="'.$row['link_ancor'].'" class="title">'.$row['title'].'</a>
<br />
</div>
<div style="clear:both;"></div></div><hr>';
}
// paginazione
  $maxPage = ceil($numrows/$records_number);

  $nav = '';
for($page = 1; $page <= $maxPage; $page++) {
  if ($page == $page_number)     {
    $nav .= " $page ";
  }
  else
  {
      $nav .= "<a class=\"spazio\" href=\"javascript:htmlData('search.php','terms=".$_GET['terms']."&amp;p=$page')\">$page</a>";
  }
}

if ($page_number > 1) {

  $page = $page_number - 1;
  $prev = "<a href=\"javascript:htmlData('search.php','terms=".$_GET['terms']."&amp;p=$page')\">&laquo;</a>";

  $first = "<a href=\"javascript:htmlData('search.php','terms=".$_GET['terms']."&amp;p=1')\">Prima</a>";
}
else {
  $prev = '';
  $first = '';
}

if ($page_number < $maxPage) {
  $page = $page_number + 1;
  $next = "<a href=\"javascript:htmlData('search.php','terms=".$_GET['terms']."&amp;p=$page')\">&raquo;</a>";

  $last = "<a href=\"javascript:htmlData('search.php','terms=".$_GET['terms']."&amp;p=$maxPage')\">Ultimo</a>";
}
else {
  $next = '';
  $last = '';
}
  echo $data;

  echo "<div id=\"results_bottom\"><p class=\"paginazione\"><span>$first</span> $prev $nav $next <span>$last</span></p></div>";
    }
 }
?>