2

I'm trying to build a very basic API, I've got a query that pulls data from a MySQL view, I can echo the query out as json no problem but I want to put the query in a function in order to be able to call it from the API script...I'm just having some trouble putting the code into a function.

Here's the ode that works.....

<?php
$servername = "database.com";
$username = "username";
$password = "password";
$dbname = "db1";

// Create connection
$conn = new mysqli($servername, $username, $password,          $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * from DB_Available_Dates";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo $row["year"]. " - " . $row["Month"]. " " . $row["the_days"]. "<br>";
}
} else {
echo "0 results";
}


$conn->close();
?>

...and this is my attempt to put it into a function (that doesn't work!)...

//Connection as above
function available_dates() {
$sql = "SELECT * from DB_Available_Dates";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row

$encodeArray = array();

while($row = $result->fetch_assoc()) {
    $encodeArray[] = $row;
}
} else {
echo "0 results";
}
$dates = array();
$dates = json_encode($encodeArray);
return $dates;
}

available_dates();

$conn->close();
?>

I've just started with functions so I expect my errors to be quite comical! ...do I need to use echo to call the function?

3 Answers 3

1

you are not returning anything,you should assign return value to some thing.You should call your function like this.

function available_dates() {
$sql = "SELECT * from DB_Available_Dates";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row

$encodeArray = array();

while($row = $result->fetch_assoc()) {
    $encodeArray[] = $row;
}
} else {
echo "0 results";
}
$dates = array();
$dates = json_encode($encodeArray);
return $dates;
}

$result=available_dates();
Sign up to request clarification or add additional context in comments.

Comments

1

You are doing nothing with the return value of your function.

You can echo it or put it in a variable.

echo available_dates();

or

print_r(available_dates());

or

$results = available_dates();

1 Comment

print_r(available_dates());, missed a bracket.
0

You're returning an array in your code:

$dates = array();
$dates = json_encode($encodeArray);
return $dates;

You should simply echo the json_encode like this:

echo json_encode($encodeArray);

And then take care of the formatting/displaying (alternatively, you could do it like in your first example and echo back the output ready for displaying):

while($row = $result->fetch_assoc()) {
    $encodeArray[] = $row["year"]. " - " . $row["Month"]. " " . row["the_days"].<br>";
}

and then

echo json_encode($encodeArray);

4 Comments

so, would the return value be 'return json_encode($encodeArray);' ?
If you're calling directly for the result (like in your first example that works) it is echo. But if you're calling the function (in php), then you should return $encodeArray And then when you're returning to jQuery, echo json_encode(available_dates())
I'm also getting an error that refers to the '$result = $conn' line - it says "Call to a member function query() on a non-object"
...fixed the error, it was a scope issue - so I put the connection inside the function - working properly now!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.