0

I am trying to connect my html page with MySQL database to show data from one specific table to my page, but I always get an error actually it just goes to die part of an SQL code. I am really new to PHP programming so please can someone help me, what am I doing wrong? Here is my code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AJDE</title>
</head>
<?php
$servername = "localhost";
$username = "******";
$password = "*****";

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

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

$query = "select * from PERCENTILE";
$result = mysqli_query($conn,$query);
if(!$result) {

    die ("Umro!");

    }
/* close connection */
$mysqli->close();
?>
<body>
</body>
</html>

Thank you!

1
  • 2
    Forget to select database name!! Commented Jul 21, 2016 at 8:30

3 Answers 3

1

Do you want to show the table as a HTML table or just an array?

The following is what I did to display my table as a HTML table:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '*****';
$dbname = 'dbname';
$selectedTable = 'whateverTableYouWant';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if (!$conn){
    die('cannot connect to mysql');
}

$query = "SELECT * FROM $selectedTable";
if ($result = mysqli_query($conn , $query)) {

    echo("<div class = 'data_wrapper'>");

    // Display Header of the table

    $fieldcount=mysqli_num_fields($result); //value = number of columns

    $row = mysqli_fetch_assoc($result);     //Fetch a result row as an associative array:

        //array to string conversion

        echo("<table id='example' class='table table-striped table-bordered' cellspacing='0' width='100%'>");

        echo("<thead> <tr>");

        foreach($row as $item){

            echo "<th>" .$item. "</th>";
        }

        echo("</tr> </thead>");


        //Footer
        echo("<tfoot> <tr>");

        foreach($row as $item){

            echo "<th>" .$item. "</th>";
        }

        echo("</tr> </tfoot>");



        //Display Data within the table
        echo("<tbody>");
        while ($row = mysqli_fetch_assoc($result)){
                echo "<tr>";
                foreach ($row as $item){
                    echo "<td contenteditable = 'true'>" . $item . "</td>"; //Change contenteditable later
                    //Editable data should be constricted, int = numbers only, string = words, date = date
            }
            echo "</tr>";
        }
        echo("<tbody>");
        echo "</table>";
        echo("</div>");

    }

The following is just displaying as an array:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '*****';
$dbname = 'dbname';
$selectedTable = 'whateverTableYouWant';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

if (!$conn){
    die('cannot connect to mysql');
}
 $query = "SELECT * FROM $selectedTable";

 if ($result = mysqli_query($conn , $query)) {
    while ($row = mysqli_fetch_array($result)){
       print_r($row);
    }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank u so much! Your answer helped me a lot.
Glad I can help! Good luck on your project! :D
0

Please change the connection string like mentioned below.

<?php
$con = mysqli_connect("localhost","username","password","dbname");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

Please let me know if you've any queries.

Comments

0

I Think you need to select a database where you will run the query:

Try with this code:

<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with (so replace the database name examples)
$selected = mysql_select_db("examples",$dbhandle) 
or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM PERCENTILE");

//fetch the data from the database and display results
//replace id,name,year with the columns you have on your table PERCENTILE and you want to show
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'name'}."Year: ". $row{'year'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>

Hope it helps, Vince.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.