2015-09-06 83 views
-1

我想重寫我的代碼從SQL到SQli,但我似乎無法弄清楚如何重寫我使用的下面一段代碼。用戶點擊一個更新鏈接,將它們帶到一個表單中,然後使用下面的php獲取要更新的字段的id,連接到數據庫並找到id,然後將該id傳遞給php提交代碼。如何從sql重寫代碼到mysqli?

<?php 
    // check if the 'id' variable is set in URL, and check that it is valid 
    if (isset($_GET['cd']) && is_numeric($_GET['cd'])) 

    // get id value 
    $id = $_GET['cd']; 
$results = $id; 


    $username = "some username"; 
    $password = "some password"; 
    $hostname = "localhost"; 
    $databasename = "some database"; 

    //connection to the database 
     $dbhandle = mysql_connect($hostname, $username, $password) 
     or die("Unable to connect to MySQL"); 
     @mysqli_select_db($databasename,$dbhandle); 

    //select a database to work with 
     $selected = mysql_select_db("mydeos_lep",$dbhandle) 
     or die("Could not select mydeos_lep"); 

    //execute the SQL query and return records 
     $result = mysql_query("SELECT * FROM `lep` WHERE id='$id'"); 
     $num=mysql_numrows($result); 

    mysql_close(); 
    // see if any rows were returned 
     if (mysql_num_rows($result) > 0) { 
    // print them one after another 
     echo "<table cellpadding=10 cellspacing=3>"; 


      while($row = mysql_fetch_row($result)) { 
echo '<td><a><input type="submit" formaction="update.php?id=' . $row['0'] . '"></span></a></td>'; 
      echo "</tr>"; 
      } 
      echo "</table>"; 
     } 
?> 

奏效最終代碼

<?php 
    // check if the 'id' variable is set in URL, and check that it is valid 
    if (isset($_GET['cd']) && is_numeric($_GET['cd'])) 

    // get id value 
    $id = $_GET['cd']; 
$results = $id; 

//database connection and selecting database 
$con = new MySQLi('localhost', 'some username', 'some password', 'some database'); 

//running query 
$result = $con->query("SELECT * FROM `lep` WHERE id='$id'"); 

// getting number of rows 
$num = $result->num_rows; 

if($num > 0){ 
    // print them one after another 
     echo "<table cellpadding=10 cellspacing=3>"; 
    while($row = $result->fetch_array(MYSQLI_NUM)){ 
echo '<td><a><input type="submit" formaction="update.php?id=' . $row['0'] . '"></span></a></td>'; 
      echo "</tr>"; 
      } 
      echo "</table>"; 
     } 
?> 
+0

您是否嘗試過只是做一個查找和'mysql'替換 - > 'mysqli'?我認爲所有這些功能都可以在'mysqli' – HPierce

+0

@YourCommonSense這篇文章無關重複,你的重複標記是一個錯誤。 – peterh

回答

-1

這是你如何能達到的結果你想:

//database connection and selecting database 
$con = new MySQLi($hostname, $username, $password, $databasename); 

//running query 
$result = $con->query("SELECT * FROM `lep` WHERE id='$id'"); 

// getting number of rows 
$num = $result->num_rows; 

if($num > 0){ 
    //your code as usual 
    while($row = $result->fetch_array(MYSQLI_NUM)){ 
     //your regular code here 
    } 
} 
+0

你的Anser給我衣櫃裏有一些不正確的東西,但我調整了它以使其正確。謝謝你的幫助。正確的代碼如下。 –