1

I would need some help with showing data that I have on my database but I can't seen to be able to.`

    $servername = "servername";
    $username = "username";
    $password = "password";
    $dbname = "dbname";

    $connect = mysqli_connect($servername, $username, $password, $dbname) or die ("connection failed");

    //Query

    $query = "SELECT * FROM 'Students'";
    mysqli_master_query($dbname, $query) or die ("Error while Query"); 

    $result = mysqli_master_query($dbname, $query);
    $row = mysql_fetch_array($result);

    while ($row = mysql_fetch_array($result)) {
    echo "<p>".$row['Name']."</p>";
    };

    mysql_close($connect);
?>` 

I am pretty new to this so I could have missed something simple. Any help appreciated.

  • Possible duplicate of [Can I mix MySQL APIs in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Qirel Apr 21 '17 at 17:08
  • `mysql_*` doesn't work with `mysqli_*` - apples and oranges. Also, lose the first `$row = mysql_fetch_array($result);` - you're not doing anything with it, and neither the first `mysqli_master_query()` – Qirel Apr 21 '17 at 17:08

1 Answers1

0

Below is a sample code of the normal procedure to connect to a database and to select data from it. Please follow this type of coding since MySQL is now deprecated and MySQLi is used.

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

For further reference check out http://php.net/manual/en/book.mysqli.php and also https://www.w3schools.com/php/php_mysql_insert.asp

Vihanga Bandara
  • 363
  • 2
  • 14