D
D
Dmitry2014-03-20 21:07:16
PHP
Dmitry, 2014-03-20 21:07:16

Why does the function return NULL instead of an array?

index.php :

<?php  
require 'db.php';
require 'function.php';
  #DATABASE CONNECT;

$testConnect = new dbConnect('test');
$testConnect->dbConnect();


  #TEST QUERY
//PARAMETERS:

$queryConstruct = $testConnect->dbConnect();
$query = "SELECT * FROM qwert WHERE id = 1";
$fetchType = 'fetch_row()';

//ACTION:

fetchQuery($queryConstruct, $query, $fetchType); #Должна вернуть $sql массив с выборкой из БД;

?>
<?php 
require 'index.view.php';
?>

functions.php :
<?php 

function fetchQuery($construct, $query, $fetchType) {
  $sql = $construct->query('{$query}')->$fetchType;
  return $sql;
}

?>

Fun-i returns NULL instead of an array. Tell me where is the problem. Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nazar Mokrinsky, 2014-03-20
@another_dream

$fetchType = 'fetch_row()';
Replace with
Replace with
$sql = $construct->query($query)->$fetchType();

R
Rsa97, 2014-03-20
@Rsa97

To expand a variable, quotes must be double:
In general, a simple variable is enough:
$sql = $construct->query($query)->$fetchType;

D
Dmitry, 2014-03-20
@another_dream

One oddity. If you redefine a variable in a function, it still returns NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question