How to run a Query in Azure?

1

I have a PHP WebService type SOAP in Azure and I'm using MySQL in App. The database is up and running ... but I can not execute a simple Query! Where is the error?

<?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 "<br> Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "<br> Host information: " . mysqli_get_host_info($link) . PHP_EOL;

echo "<br> host  information: " .$connectstr_dbhost;
echo "<br> dname information: " .$connectstr_dbname;
echo "<br> user  information: " .$connectstr_dbusername;
echo "<br> pass  information: " .$connectstr_dbpassword;
echo "<br>";

  //query para selecionar todos os produtos
  $sql = "select * from produtos";
  $result= mysqli_query($sql,$link);

  var_dump($result);
  $produtos = array();

  $linha = mysql_fetch_assoc($result);
  $total = mysql_num_rows($result);
  $i =0;
  if($total > 0) {
   do {
     echo "<br>".$linha['codigo']." ".$linha['nome']."<br>";
     $produtos[$i] = $linha;
     $i++;
   }while($linha = mysql_fetch_assoc($result));
  }
  return $produtos;


mysqli_close($link);


?>

BankData:

    
asked by anonymous 31.05.2017 / 18:18

1 answer

0

I was able to solve the problem ... In the search you were using mysql_query when you should use mysqli_query and other places also make the same mistake.

Here are the correct code changes:

$result = mysqli_query($link,$sql);
$linha = mysqli_fetch_assoc($result);
$total = mysqli_num_rows($result);
while($linha = mysqli_fetch_assoc($result));

At the end everything is correct:

    
31.05.2017 / 18:49