0

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 !!!!

Edited by Latrell_vie

5
Contributors
5
Replies
47
Views
1 Year
Discussion Span
Last Post by _martinh
0

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;

Edited by PsychicTide

0

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

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

Edited by PsychicTide

0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.