I have created a table in PHPmyAdmin called "people" with 4 columns and filled it with 2 rows of data. I used this piece of code from Azure's documentation in a file called "db.php" to connect to the database.
db.php:
$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';
foreach ($_SERVER as $key => $value) {
if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
continue;
}
$connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
$connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
$connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
$connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}
$link = mysqli_connect($connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword,$connectstr_dbname);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
It successfully echoes Success: A proper connection to MySQL was made!...
Now, following a youtube tutorial, I'm trying to pass the data of my table and convert it to json format using this piece of code:
fetch.php:
include_once('db.php');
$query = "SELECT * FROM `people`";
$res = mysqli_query($link,$query);
if($res !== FALSE)
{
echo "table found";
}
else
{
echo "table not found";
}
$result = array();
while( $row = mysql_fetch_array($res) )
array_push($result, array( 'id' => $row[0],
'firstname' => $row[1],
'lastname' => $row[2],
'address' => $row[3]));
echo json_encode(array("result" => $result));
But this just echoes table not found
I'm very new to both SQL and PHP. Any idea what is wrong?


db.phpcontains$connectstr_dbhost = '';there you need enter your host credentials.foreach-statement. Because after I have echoed out$connectstr_dbhostat the end of the code it outputs "localdb" which is exactly the database I'm trying to access. Do you mean that this variable should hold more than one string?