2016-11-30 1 views
-2

mysqlが廃止されたので、mysqlをmysqliに変換するのが好きです。mysqlをPHPのmysqliに変換しますか?

ここに次のコードがあります。お手伝いできますか?私は唯一の "i" は、ここでMySQLへ挿入した

 <?php 
     error_reporting(0); 
     if ($_REQUEST['submit']) { 
      $search_file = $_POST['search_file']; 
      $sql = mysql_query("select * from information where firstname like '%$search_file%' or lastname like '%$search_file%' or ID like '%$search_file%' Order by lastname ASC") or die('Error in query : $sql. ' .mysql_error()); 

      if (empty($search_file)) { 
      echo '<script language="javascript">'; 
      echo 'alert("Text field cannot be empty. Please Try it again.")'; 
      echo '</script>'; 
      header("refresh:2; url=index.php"); 
      } 
      else if (mysql_num_rows($sql) > 0) 
      {    
      $i = 1; 
      while ($row = mysql_fetch_array($sql)) { 
       // Print out the contents of the entry 
       echo '<tr>'; 
       echo '<td class="text-center">' . $i . '</td>'; 
       echo '<td class="text-center">' . $row['firstname'] . '</td>'; 
       echo '<td class="text-center">' . $row['lastname'] . '</td>'; 
       echo '<td class="text-center">' . $row['phonenumber'] . '</td>'; 
       echo '<td class="text-center">' . $row['email'] . '</td>'; 
       $i++; 
      } 
      } 
      else 
      { 
      echo '<div class="alert alert-danger" style="width:130px; float:right; margin-top:-142px;">No Results Found!!!</div>'; 
      } 
     } 
     ?> 

は、私が作ってみたものです が、それはエラーを示しています

"Error in query : $sql."

 <?php 
     error_reporting(0); 
     if ($_REQUEST['submit']) { 
      $search_file = $_POST['search_file']; 
      $sql = mysqli_query($conn, "select * from information where firstname like '%$search_file%' or lastname like '%$search_file%' or ID like '%$search_file%' Order by lastname ASC") or die('Error in query : $sql. ' .mysqli_error()); 

      if (empty($search_file)) { 
      echo '<script language="javascript">'; 
      echo 'alert("Text field cannot be empty. Please Try it again.")'; 
      echo '</script>'; 
      header("refresh:2; url=index.php"); 
      } 
      else if (mysqli_num_rows($sql) > 0) 
      {    
      $i = 1; 
      while ($row = mysqli_fetch_array($sql)) { 
       // Print out the contents of the entry 
       echo '<tr>'; 
       echo '<td class="text-center">' . $i . '</td>'; 
       echo '<td class="text-center">' . $row['firstname'] . '</td>'; 
       echo '<td class="text-center">' . $row['lastname'] . '</td>'; 
       echo '<td class="text-center">' . $row['phonenumber'] . '</td>'; 
       echo '<td class="text-center">' . $row['email'] . '</td>'; 
       $i++; 
      } 
      } 
      else 
      { 
      echo '<div class="alert alert-danger" style="width:130px; float:right; margin-top:-142px;">No Results Found!!!</div>'; 
      } 
     } 
     ?>  

親切に助けてください!ありがとうございました!

+1

myqli_fetch_assoc()のようなmyqli関数に関するphpマニュアル(http://php.net/manual/en/mysqli-result.fetch-assoc.php) – WEBjuju

+0

を読んでいる間は、コードも同様です。 – e4c5

+0

**警告**:mysqliを使用する場合は、[パラメータ化されたクエリ](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)と['bind_param']( http://php.net/manual/en/mysqli-stmt.bind-param.php)を使用してクエリにユーザーデータを追加します。 **重大な[SQLインジェクションのバグ](http://bobby-tables.com/)を作成したため、文字列の補間または連結を使用してこれを実行しないでください。 ** '$ _POST'や' $ _GET'データを直接クエリに入れないでください。誰かがあなたのミスを悪用しようとすると、非常に危険です。 – tadman

答えて

0

これらの変更

<?php 
    error_reporting(0); 
    if ($_REQUEST['submit']) { 
    $search_file = $_POST['search_file']; 
    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 

    $query = "select * from information where firstname like '%$search_file%' or lastname like '%$search_file%' or ID like '%$search_file%' Order by lastname ASC"; 

    if ($conn->query($query) === TRUE) { 
     echo "New record created successfully"; 
    } else { 
     echo "Error: " . $sql . "<br>" . $conn->error; 
    } 

    //add this where connection ends 
    //$conn->close(); 
?> 

は、PHPサイト上の他のコードを休ま検索してください。

+0

残念ながら、このコードはほとんど意味がありません。 –

+0

私は、これが "PHPでのSQLインジェクションを防ぐにはどうすればよいですか?"と重複していると誰が言うのかわかりません。ちょうど愚か度 – lokis

+0

は真剣に好きですか?それを知っていない人を助けることは悪いですか? – Rasika

関連する問題