golds golds -3 years ago 76
SQL Question

How to assign a DB value to a PHP variable?

I am trying to assign a DB value to a PHP variable. The database is SQL. I want the database value to be $HaveOfficialPage. How can I do this?

$connection = mysqli_connect('localhost', '', '', '');


echo "Connected";


else {

die("Database connection failed");


$query = "SELECT HaveOfficialPage FROM vf_Category";

$result = mysqli_query($connection, $query);


die("Query failed" . mysqli_error());

Answer Source

If you’re trying to iterate through the results:

while ($row = mysqli_fetch_row($result)) {
      $haveOfficialPage = $row[0];
    // do something with the variable

If you’re getting only one record:

$row = mysqli_fetch_assoc($result);
$haveOfficialPage = $row["HaveOfficialPage"];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download