2016-11-27 14 views
-1

我想做一個PHP代碼,如果我把我的數據庫中的URL,代碼將拿起去到URL,看看它得到了什麼HTTP代碼,但目前它所做的是給我的HTTP-代碼0而不是賴特。爲什麼我可以讓我的mysqli_fetch_assoc工作?

$username = "root"; 
$password = "luca170385"; 
$hostname = "localhost"; 

$dbhandle = mysqli_connect($hostname, $username, $password) 
or file_put_contents($filename, date("Y-m-d H:i:s")."Unable to connect to database", FILE_APPEND); 
echo "Connect to MySQL<br>"; 

$selected = mysqli_select_db($dbhandle , "accounts") 
or file_put_contents($filename, date("Y-m-d H:i:s")."Could not select database<br>", FILE_APPEND); 
echo "Selected Database<br>"; 

$sql = "SELECT `address` FROM `url` WHERE `code` IS NULL"; 

if ($result = mysqli_query($dbhandle, $sql)){ 

    while ($row = mysqli_fetch_assoc($result));{ 
    printf ($row['address']); 
    $output = "{$row['address']}"; 
    echo "{$row['address']}"; 
    $handle = curl_init($sql); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); 

    $response = curl_exec($handle); 

    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
    $update = "UPDATE url SET `code`= $httpCode WHERE `address` = $sql"; 
    echo "$update.<br>"; 
    echo "$handle.<br>"; 
    echo "$httpCode.<br>"; 

    mysqli_query($dbhandle, $update); 
    echo "{$row['address']}"; 

    curl_close($handle); 
    } 
}else { 
    echo 'Not working'; 
} 
+1

'而($行= mysqli_fetch_assoc($結果));'<<<您會看到分號?這是「聲明的結束」字符,因爲它是PHP中的「有效」語句,所以您不會因此發生錯誤。 –

+1

根據http://php.net/manual/en/language.basic-syntax.instruction-separation.php *「和C或Perl一樣,PHP要求在每條語句結尾處以分號結束指令。一段PHP代碼的結束標記自動錶示一個分號;您不需要用分號來終止PHP塊的最後一行。如果存在一個塊,則該塊的結束標記將包括緊接的尾隨換行符。 –

+0

順便說一句,你的查詢失敗你在分號頂部。迴應該查詢,你會看到它的作用。 –

回答

1

檢查意見: -

<?php 
error_reporting(E_ALL);// check all type of error 
ini_set('display_errors',1);// display those errors 
$username = "root"; 
$password = "luca170385"; 
$hostname = "localhost"; 

$dbhandle = mysqli_connect($hostname, $username, $password, "accounts"); 
/*or file_put_contents($filename, date("Y-m-d H:i:s")."Unable to connect to database", FILE_APPEND); 
echo "Connect to MySQL<br>";*/// from where this $filename come from? 

/*$selected = mysqli_select_db($dbhandle ,) 
or file_put_contents($filename, date("Y-m-d H:i:s")."Could not select database<br>", FILE_APPEND); 
echo "Selected Database<br>";*/ //not needed 

$sql = "SELECT `address` FROM `url` WHERE `code` IS NULL"; 

if ($result = mysqli_query($dbhandle, $sql)){ 

    while ($row = mysqli_fetch_assoc($result)){ // remove ; 
     printf ($row['address']); 
     $output = $row['address']; // remove {} and "" 
     echo $row['address']; // remove {} and "" 
     $handle = curl_init($sql); 
     curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); 

     $response = curl_exec($handle); 

     $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
     $update = "UPDATE url SET `code`= $httpCode WHERE `address` = $output"; // not $sql 
     echo "$update.<br>"; 
     echo "$handle.<br>"; 
     echo "$httpCode.<br>"; 
     mysqli_query($dbhandle, $update); 
     echo $row['address'];// remove {} and "" 
     curl_close($handle); 
    } 
}else { 
    echo 'Not working'; 
} 
+0

我決定不發表一個答案(只有評論),看到他們的代碼失敗了多個計數。 –

+0

'echo'Not working';'在這裏不會幫助他們,並且應該爲查詢包含適當的錯誤處理,並且對所有查詢都應該包含這個錯誤處理。 –

2

;終止while,所以它是空的。

while ($row = mysqli_fetch_assoc($result)); 

所以,你應該只需要刪除;

+1

*「我認爲」* - 如果您不確定,您應該將其刪除。我確定一個。 –

相關問題