MikeBroski MikeBroski - 6 months ago 52
SQL Question

Azure web app with mysqli_connect instead of PDO

I have recently been successful with utilize the PDO connection instead of mysql_query, however I find PDO to be far complex and confusing, so I wish use the mysqli/mysqlconnet instead.

I noticed that uppgrading the php version on the azure app decerese the potenial error you can get, however I was wondering how can you make your app as close to as the localhost variation, with error been extremtly precise and simple to understand?

Here is the code mysql_querry con

<?php
$servername = "localhost";
$username = "username";
$password = "password";


$conn = mysqli_connect($servername, $username, $password);

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {

if (!mysqli_query($link, "SET @a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($link));
}
mysqli_free_result($result);
}

mysqli_close($link);
?>


PDO variation

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

<?php
function getFruit($conn) {
$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
foreach ($conn->query($sql) as $row) {
print $row['name'] . "\t";
print $row['color'] . "\t";
print $row['calories'] . "\n";
}
}
?>


Here is the app settings
web app settings

Also what is the diffrent between utlizeing Connection String here

mysql connection
compare to utilize the php implmetion within in webmatrix

UPDATE here what I am basically trying to get to work. But I dunno why azure doesnt give me errors or anything. I have finally figured out that you can go into the console/kudu and change the php ini file which I have never done in my life...

$query = "SELECT * FROM randomTable WHERE innhold LIKE '%$searchFunction%';";

$result = mysqli_query($conn, $query) or die("Ikke mulig og s√łke i db");
$antallrader = mysqli_num_rows($sqlResultat);

Answer

As a production environment, Azure Web Apps Service do not enable the display errors in PHP runtime. For dev, you can config the setting display_errors in PHP ini config file. You can refer to How to: Change the built-in PHP configurations for details.

Meanwhile, you can simply add the following code in your PHP script to enable to show the errors:

error_reporting(E_ALL);
ini_set('display_errors', 'On');

And BTW, the PHP runtime on Azure Web Apps has supported the mysqli extension even in PHP 7.0. You can use phpinfo() to check all PHP runtime. enter image description here

Any further concern, please feel free to let me know.