From d90b20fcb3bd9af90a0ffc91c4de7a64a409acae Mon Sep 17 00:00:00 2001 From: Sudipto Bhattacharjee Date: Thu, 14 Jul 2022 17:57:05 +0530 Subject: [PATCH] Make code compatible with PHP-v5 --- advance_search_result.php | 25 +++++++++++++++++++--- bioproject_id.php | 21 +++++++++++-------- db.php | 40 ++++++++++++++++++++++++++++++++++++ disease_wise_bioprojects.php | 7 ++++--- get_choropleth_data.php | 7 ++++--- run_id.php | 11 +++++----- 6 files changed, 88 insertions(+), 23 deletions(-) diff --git a/advance_search_result.php b/advance_search_result.php index df76df3..c384993 100644 --- a/advance_search_result.php +++ b/advance_search_result.php @@ -133,6 +133,13 @@ function createParamString($keys) { } return $typeString; } + + function refValues($arr){ + $refs = array(); + for($i=0; $iprepare($query); - $stmt->bind_param($paramString, ...$values); + + array_unshift($values, $paramString); + call_user_func_array( + array($stmt, "bind_param"), + refValues($values) + ); $stmt->execute(); - $result = $stmt->get_result(); - $rows = $result->fetch_all(MYSQLI_ASSOC); + +// $stmt->bind_param($paramString, ...$values); +// $stmt->execute(); +// $result = $stmt->get_result(); +// $rows = $result->fetch_all(MYSQLI_ASSOC); + $rows = execute_and_fetch_assoc($stmt); +// foreach($rows as $row) { +// echo implode(",", $row)."
"; +// } $rowsJSON = json_encode($rows); // echo $result->num_rows." ".$result->field_count."
"; diff --git a/bioproject_id.php b/bioproject_id.php index 35766a7..bae38d4 100644 --- a/bioproject_id.php +++ b/bioproject_id.php @@ -14,17 +14,19 @@ $bioprojectStmt = $conn->prepare($bioprojectQuery); $bioprojectStmt->bind_param("s", $bioprojectID); - $bioprojectStmt->execute(); - $bioprojectResult = $bioprojectStmt->get_result(); +// $bioprojectStmt->execute(); +// $bioprojectResult = $bioprojectStmt->get_result(); // echo $bioprojectResult->num_rows." ".$bioprojectResult->field_count."

"; + $bioprojectRows = execute_and_fetch_assoc($bioprojectStmt); $bioprojectStmt->close(); $runStmt = $conn->prepare($runQuery); $runStmt->bind_param("s", $bioprojectID); - $runStmt->execute(); - $runResult = $runStmt->get_result(); +// $runStmt->execute(); +// $runResult = $runStmt->get_result(); // echo $runResult->num_rows." ".$runResult->field_count."

"; - $runRows = $runResult->fetch_all(MYSQLI_ASSOC); +// $runRows = $runResult->fetch_all(MYSQLI_ASSOC); + $runRows = execute_and_fetch_assoc($runStmt); $runRowsJSON = json_encode($runRows); $runStmt->close(); @@ -65,13 +67,13 @@
num_rows < 1) { + if (count($bioprojectRows) < 1) { echo "

Error !!! BioProject ID: ".$bioprojectID." does not exist in the database.

"; } else { echo "

BioProject ID: ".$bioprojectID."

"; echo ""; echo ""; - while ($row = $bioprojectResult->fetch_assoc()){ + foreach($bioprojectRows as $row){ foreach ($allBioProjectAttributes as $name=>$fname) { if ($name !== "BioProject") { echo ""; @@ -86,8 +88,9 @@ echo ""; diff --git a/db.php b/db.php index 37659a0..edd0e77 100644 --- a/db.php +++ b/db.php @@ -79,4 +79,44 @@ function connect() { function closeConnection($conn) { $conn->close(); } + + function execute_and_fetch_assoc($stmt, $types = false, $params = false) { + if (!$stmt->execute()) { + return false; + } + $stmt->store_result(); + + // get column names + $metadata = $stmt->result_metadata(); + $fields = $metadata->fetch_fields(); + + $results = []; + $ref_results = []; + foreach($fields as $field){ + $results[$field->name]=null; + $ref_results[]=&$results[$field->name]; + } + + call_user_func_array(array($stmt, 'bind_result'), $ref_results); + + $data = array(); + $i=0; + while ($stmt->fetch()) { + $c = 0; + $new_res = array(); + foreach($results as $f => $v) { + $new_res[$f] = $v; + $c++; + } +// echo implode(",", $new_res); + array_push($data, $new_res); + } + +// echo count($data); +// foreach($data as $d) +// echo implode(",", $d)."
"; + + $stmt->free_result(); + return $data; + } ?> diff --git a/disease_wise_bioprojects.php b/disease_wise_bioprojects.php index 11bb1a6..3954dd0 100644 --- a/disease_wise_bioprojects.php +++ b/disease_wise_bioprojects.php @@ -11,10 +11,11 @@ $bioprojectStmt = $conn->prepare($bioprojectQuery); $disease = "%".$disease."%"; $bioprojectStmt->bind_param("s", $disease); - $bioprojectStmt->execute(); - $bioprojectResult = $bioprojectStmt->get_result(); +// $bioprojectStmt->execute(); +// $bioprojectResult = $bioprojectStmt->get_result(); // echo $bioprojectResult->num_rows." ".$bioprojectResult->field_count."

"; - $rows = $bioprojectResult->fetch_all(MYSQLI_ASSOC); +// $rows = $bioprojectResult->fetch_all(MYSQLI_ASSOC); + $rows = execute_and_fetch_assoc($bioprojectStmt); $rowsJSON = json_encode($rows); $bioprojectStmt->close(); closeConnection($conn); diff --git a/get_choropleth_data.php b/get_choropleth_data.php index 926459f..6978c2e 100644 --- a/get_choropleth_data.php +++ b/get_choropleth_data.php @@ -7,10 +7,11 @@ $conn = connect(); $choroplethStmt = $conn->prepare($choroplethQuery); - $choroplethStmt->execute(); - $choroplethResult = $choroplethStmt->get_result(); +// $choroplethStmt->execute(); +// $choroplethResult = $choroplethStmt->get_result(); // echo $bioprojectResult->num_rows." ".$bioprojectResult->field_count."

"; - $rows = $choroplethResult->fetch_all(MYSQLI_ASSOC); +// $rows = $choroplethResult->fetch_all(MYSQLI_ASSOC); + $rows = execute_and_fetch_assoc($choroplethStmt); $rowsJSON = json_encode($rows); $choroplethStmt->close(); closeConnection($conn); diff --git a/run_id.php b/run_id.php index f39ae7f..087fbc2 100644 --- a/run_id.php +++ b/run_id.php @@ -10,10 +10,11 @@ $conn = connect(); $stmt = $conn->prepare($query); $stmt->bind_param("s", $runID); - $stmt->execute(); - $result = $stmt->get_result(); +// $stmt->execute(); +// $result = $stmt->get_result(); // echo $result->num_rows." ".$result->field_count."
"; // $rows = $result->fetch_all(MYSQLI_ASSOC); + $rows = execute_and_fetch_assoc($stmt); $stmt->close(); closeConnection($conn); ?> @@ -47,13 +48,13 @@
num_rows < 1) { + if (count($rows) < 1) { echo "

Error !!! Run ID: ".$runID." does not exist in the database.

"; } else { echo "

Run ID: ".$runID."



"; echo "
AttributeValue
"; foreach($subgroups as $sg) { $diseaseStmt->bind_param("s", $sg); - $diseaseStmt->execute(); - $disease = $diseaseStmt->get_result()->fetch_all(MYSQLI_ASSOC); +// $diseaseStmt->execute(); +// $disease = $diseaseStmt->get_result()->fetch_all(MYSQLI_ASSOC); + $disease = execute_and_fetch_assoc($diseaseStmt); echo $disease[0]["Grp"]." → ".$sg."
"; } echo "
"; echo ""; - while ($row = $result->fetch_assoc()){ + foreach($rows as $row){ foreach ($allRunAttributes as $name=>$fname) { if ($name !== "Run") { echo ""; @@ -70,6 +71,6 @@ } ?> - +
AttributeValue