I'm trying to use variables from a function which is in an external file. To achieve this, I'm using the include function to include that file (functions.php) into my primary file (insert.php) and trying to echo variable from functions.php.
My primary file is insert.php which includes the following:
<html>
<head>
</head>
<body>
<?php
include 'functions.php';
external_functin();
echo $primary_var0."<br />";
echo $secondary_var0;
?>
</body>
</html>
functions.php file:
<?php
function external_function(){
include 'mysql_connection.php';
$query="SELECT * FROM db_table";
$exec=mysqli_query($conn, $query);
$i=0;
if(mysqli_num_rows($exec)>0){
while($result=mysqli_fetch_assoc($exec)){#This while script stores the query result values to variables.
${"primary_var".$i} = $result['field_name'];
${"secondary_var".$i} = $result['id'];
$i++;
}
}
}
?>
The issue is that everytime I load / run insert.php, since it has syntax to echo $primary_var0 and $secondary_var0, I get an undifined index error.
Help is appreciated.
Aucun commentaire:
Enregistrer un commentaire