Luke Fuda Luke Fuda - 11 days ago 6
PHP Question

Why can i get my mysqli_fetch_assoc working?

I am trying to make a php code where if I put in my database a URL, the code will pick up on that go to the URL and see what HTTP-code it gets but currently all it does is give me the HTTP-code 0 instead of the wright.

$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';
}

Answer

check the comments :-

<?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';
}