2011-10-18 53 views
2

我需要幫助下面的Visual Basic語句轉換爲PHP相當於:VB if語句在PHP

If Not IsNumeric(siteid) Then 

    dr = GetDataReader("SELECT siteid FROM nwsite WITH (NOLOCK) WHERE mac_address = '" & siteid & "'") 

    If Not dr.HasRows Then 

     Response.Write(sep & siteid & "=" & siteid) 

     sep = "," 

    End If 

    If Not dr Is Nothing Then 

     dr.Close() 

    End If 

    End If 

我需要用if語句比什麼幫助。

感謝

+0

這就是數據庫處理一個相當基本的位碼;如果你爲此苦苦掙扎,你最好找一些教授基本數據庫處理的PHP教程。不要試圖逐行轉換;您將瀏覽PHP和VB數據庫庫工作方式的差異。相反,要弄清楚這個代碼塊作爲一個整體,並嘗試從頭開始重新在PHP中實現它。 – Spudley

+0

VB代碼很容易受到SQL注入 –

回答

2

here for the docs you need to check,也here

好像你also need to learn how to use mySQL in PHP

代碼是比較容易的,從你有什麼線路最簡單的步驟 - 幾乎線,以幫助您看轉換到PHP(有更好的實現):

$siteid = 1; // where 1 is the value of siteid, PHP vars are prefixed with '$' 


// you also could do 
// if(!isset($siteid){ 
// if(!$siteid){ 

if(!is_numeric($siteid){ 
    // there is no site ID, so get it from the DB 

    // Make a MySQL Connection 
    mysql_connect("localhost", "user", "password") or die(mysql_error()); 
    mysql_select_db("dbname") or die(mysql_error()); 

    // Run your query 
    $result = mysql_query("SELECT siteid FROM nwsite WITH (NOLOCK) WHERE mac_address = ".$siteid) 
    or die(mysql_error()); 

    if(mysql_num_rows($result)>0){ 
    // rows returned 
    //assuming only one row is returned, with your siteid value 
    $row = mysql_fetch_array($result); 
    $siteid=$row['siteid'];  
    }else{ 
    // no rows returned 
    // do something 
    } 

}else{ 
// $siteid is already a valid value 
} 
+1

固定的'拼寫is_numeric()來'你:) – Spudley

+0

+1感謝,看到太晚了! – SW4

+0

感謝:D – lmpearce1

0

AN if語句是這樣的:

if(someexpression){ 
    $yourcode = "your things"; 
} 

代碼的快速樣機:

if(!is_numeric($siteid){ 
    //sql call 
    $result = yourdb->query('SELECT'); 
    if(!$result){ 
     echo "error"; 
    } 
} 
0
if(!$this->IsNumeric($siteid)) 
{ 
    $dr = $this->GetDataReader("SELECT siteid FROM nwsite WITH (NOLOCK) WHERE mac_address = $siteid"); 
    if(!$dr->hasRows()) 
    { 
     echo "$sep$siteid = $siteid"; 
     $sep = " , "; 
    } 
    if(!is_null($dr)) 
    { 
     $dr->close(); 
    } 
} 
+0

hasRows()不是PHP函數 – lmpearce1

+0

我從來沒有說過它。我正在嘗試GetDataReader方法返回一個名爲hasRows的方法的對象。 – rjbell00

0

下面的代碼可以幫助你:

if (!is_numeric($siteid)) { 
    $res = mysql_query("SELECT siteid FROM nwsite WHERE mac_address = " . mysql_real_escape_string($siteid)); 
    $rows = mysql_fetch_assoc($res); 
    if (count($rows) == 0) { 
    print($sep . $siteid . "=" . $siteid); 
    $sep = ','; 
    } 
}