How to run a Query in Azure?

Asked

Viewed 56 times

1

I have a PHP SOAP type Webservice on Azure and I am using Mysql in App. The bank is logging in and running...but I can’t run 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);


?>

inserir a descrição da imagem aqui

Data of the Bank: inserir a descrição da imagem aqui

  • Have any dice in the bank?

  • Have...I will post the photo with the contents of the bank

  • 1

    In the browser print it says that you have connected in the my_db database, and in the phpmyadmin print you are in the localdb database. Check this out.

1 answer

0


I managed to solve the problem... In the search I was using mysql_query when you should use the mysqli_query and other places also makes 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));

In the end everything is right:inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.