My php file connnects to my sql database , following code works

<?php
    $servername = "globalxxx.com.mysql";
    $username = "globalxxx_com";
    $password = "YS4xxx";

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

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully"
    $sql = "SELECT Id, FirstName, LastName FROM mydb";
    $result = $conn->query($sql);
    ?>

after that a staff listing which is written in html shows

BUT if i use following code to get data out of my sql to display I only get a blank screen

<?php
    $servername = "globalxxx.com.mysql";
    $username = "globalxxx_com";
    $password = "YS4xxx";
    $dbname = "mydb";

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

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully"
    $sql = "SELECT Id, FirstName, LastName FROM mydb";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "Id: " . $row["Id"]. " - Name: " . $row["FirstName"]. " " . $row["LastName"]. "<br>";
        }
    } else {
        echo "0 results";
    }


    ?>

I wonder where is the problem , can sb help ? Please !!!!

Without seeing the actual data you can try to use Chrome or FireFox (firebug extension I think?) to check if there were any errors in the PHP GET/POST... pres F12 to open the inspecter console -> click on the tab named 'network' call your php function/file and look at what vars were passed to what files when they pop up.

or can try storing the count var first (or echo'ing it to check for results)...

$result->execute();
$result->store_result();

$result->bind_result($id, $FirstName, $LastName);

while ($result->fetch()) {
    # some random output here...
}

$count = $result->num_rows;

do you actually have a table 'mydb'
that looks like the name of your database

the main idea I had was storing before calling num_rows...

$result->store_result();
$result->num_rows;

You are not specifying the table to get the data from in the sql you are using. A database and a table within a database are different things.

2 things; sorry I cant offer a solution

does the query string $sql need a terminating ';'
try end echo $result->num_rows to see what you are dealing with...

just some thoughts

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.