This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Data is not displaying in DATABASE......error is not showing in IDE

// Hello, // I am facing problem in displaying data which is uploaded in web server. I am struggling since long but couldn't find a way out. // Can i get some help, please?

// I am attaching a part of the code i used for reference. Please let me know if there is any problem in the code or i need to check elsewhere?

//Thanks, //Love, Souvik

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "Combination";

    if ($conn->connect_error){
        die("Connection failed: ". $conn->connect_error);
    }
function func_mol($conn, $result){
                    $keyword_query = $conn->real_escape_string($_GET['query']);
                    $keyword_query = explode(',', $keyword_query);
                    switch ($_GET['molecules']) {
                        case "Byprotein":
                            while ($row = $result->fetch_assoc()) {
                                echo '<tr>
                                        <td class="text-center" width="20%">' . $row['Crop'] . '</td>
                                        <td class="text-center" width="20%">' . $row['stress'] . '</td>
                                        <td class="text-center" width="20%">' . $row['Gene_Id'] . '</td>
                                        <td class="text-center" width="20%">' . $row['Gene_symbol'] . '</td>
                                        <td class="text-center" width="20%">' . $row['Description'] . '</td>
                                        <td class="text-center" width="20%">' . $row['proteins'] . '</td>
                                        <td class="text-center" width="20%">' . $row['Source'] . '</td>
                                        <td class="text-center" width="20%">' . $row['LinkToSource'] . '</td>
                                    </tr>';
                            }
                     }
}
function sql_line($conn){
                    $keyword_query = $conn->real_escape_string($_GET['query']);
                    $keyword_query = explode(',', $keyword_query);
                    switch ($_GET['molecules']) {
                        case "Byprotein":
                            // $sql = "SELECT Crop,stress,Gene_Id,Gene_symbol,Description,proteins,Source,LinkToSource FROM Cropcs WHERE Crop='" . $keyword . "' AND stress='" . $keyword . "'";
                            $sql = "SELECT Crop,stress,Gene_Id,Gene_symbol,Description,proteins,Source,LinkToSource FROM Cropcs WHERE Crop = '" . $_GET['crop'] . "' AND stress = '" . $_GET['stress'] ."'";
                            @$i = 0;
                            foreach ($keyword_query as $queryKey) {
                                ++$i;
                                $sql .= "Gene_Id = '" . $queryKey . "'";
                            }
                   }
}
switch ($keyword_crop) {
                    case "Bymalus":
                        switch ($keyword_stress) {
                            case "$sp1[0]":
                                sql_line($conn);
                        }
}
$result = $conn->query($sql);
                if ($result->num_rows > 0) {
                    switch ($_GET['searchBytype']) {
                        case "Bymalus":
                            switch ($_GET['stresss']) {
                                case "$sp1[0]":
                                    $queryKey->func_mol;
                            }
                     }
                }


?>
server database php

1 answer

It seems error might be due to a problem with the way u construct SQL query. the $sql variable isn't being assigned a value in some scenarios, and therefore when you run $conn->query($sql), it might not be able to execute anything. Look into the function sql_line(), you are constructing the SQL query based on $_GET['molecules'] but not returning the $sql variable from the function or assigning it to a global variable that can be accessed outside the function.

try the code including this revision

<?php 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "Combination";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error){
    die("Connection failed: ". $conn->connect_error);
}

function func_mol($conn, $result){ 
    $keyword_query = $conn->real_escape_string($_GET['query']); 
    $keyword_query = explode(',', $keyword_query); 
    switch ($_GET['molecules']) { 
        case "Byprotein": 
            while ($row = $result->fetch_assoc()) { 
                echo ' ' . $row['Crop'] . ' ' . $row['stress'] . ' ' . $row['Gene_Id'] . ' ' . $row['Gene_symbol'] . ' ' . $row['Description'] . ' ' . $row['proteins'] . ' ' . $row['Source'] . ' ' . $row['LinkToSource'] . ' '; 
            } 
    } 
} 

function sql_line($conn){ 
    $keyword_query = $conn->real_escape_string($_GET['query']); 
    $keyword_query = explode(',', $keyword_query); 
    $sql = "";
    switch ($_GET['molecules']) { 
        case "Byprotein": 
            $sql = "SELECT Crop,stress,Gene_Id,Gene_symbol,Description,proteins,Source,LinkToSource FROM Cropcs WHERE Crop = '" . $_GET['crop'] . "' AND stress = '" . $_GET['stress'] ."'";
            @$i = 0; 
            foreach ($keyword_query as $queryKey) { 
                ++$i; 
                $sql .= " AND Gene_Id = '" . $queryKey . "'"; 
            } 
    } 
    return $sql;
} 

$sql = sql_line($conn);
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    switch ($_GET['searchBytype']) { 
        case "Bymalus": 
            switch ($_GET['stresss']) { 
                case "$sp1[0]": 
                    func_mol($conn, $result);
            } 
    } 
} 
?>

I added the line

$conn = new mysqli($servername, $username, $password, $dbname);

because the $conn object wasn't being initialized anywhere in the provided code. Also, added a return $sql; in sql_line() function, and before executing the query, I assigned $sql = sql_line($conn);.

I would also enable error reporting as its very helpful

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Thank you. But the command already added before. Can you give other way to write the two functions and show the code?

Log in to answer this question.