0

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?

4
  • Did you fill the credentials? Commented Jul 2, 2017 at 21:00
  • @PseudoAj what credentials? How do I find those and where do I enter them? Would they not be needed during connection as well though? Commented Jul 2, 2017 at 21:03
  • The db.php contains $connectstr_dbhost = ''; there you need enter your host credentials. Commented Jul 3, 2017 at 0:21
  • @PseudoAj I thought that was the point of the foreach-statement. Because after I have echoed out $connectstr_dbhost at 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? Commented Jul 3, 2017 at 5:51

2 Answers 2

1

Judging from your codes, it looks like you are using the Azure MySQL In App. By default, Azure injects the database connection information through MYSQLCONNSTR_localdb variable at run-time environment. And the database name it injects into the variable is named: localdb.

So I am wondering under which database have you created the people table? If you have put the people table into other database, like the screenshot below:
enter image description here

Then the code in your fetch.php file will echo table not found; if you have put the table inside the localdb database, then it will echo `table found”;

You can add the phpMyAdmin extension through Azure Portal, like below screenshot:

enter image description here

Then you can view your database content through the url: https://{YOUR WEB APP’S NAME}.scm.azurewebsites.net/phpmyadmin/ to enter the workbench.

Besides, you can refer to this link for more details regarding how MySQL in-app works for Web Apps.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes the table is in localdb which I am sure the php file is connected to: image. I am not sure what adding the extension adds to the functionality as I was already able to access https://{YOUR WEB APP’S NAME}.scm.azurewebsites.net/phpmyadmin/ right after I enabled "Azure MySQL In App" and restarted the app.
0

Turns out I was mixing MySQL and MySQLi. The last code block (which converts the table into JSON) is using MySQL. I changed this and got it working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.