Skip to content
Advertisement

HTML php localhost database retrieval

I was trying to get data from the database but it won’t display correctly.

Note: I used old project file I did back in college prev semester which is working. But implementation on the new project won’t work. How can I fix the issue? The table is shown but the data are not fetched correctly

What I am using:

  • Notepad+
  • wamp

things i tried:

  • used different methods (from google, stackoverflow, w3school) non of them work
  • changed server name to localhost:8082 (since this is how my link look like http://localhost:8082/phpmyadmin)

the results of the code

the code

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    
}
$result = $conn->query("select weapon_icon, weapon_name,weapon_type,weapon_rarity from weapons");

?>
<table align="center">
        <tr>
        <th>Image</th>
        <th>Name</th>
        <th>type</th>
        <th>rarity</th>

        </tr>
        <?php
        
        while($row = mysqli_fetch_array($result)) { 
            echo "<tr>";
            echo "<td><img src='".$row['weapon_icon']."' width=199 height=188 />";
            echo "<td>".$row["weapon_name"]."</td>";
            echo "<td>".$row["weapon_type"]."</td>";
                echo "<td>".$row["weapon_rarity"]."</td>";

            
        
            echo "</table>";
            }
?>

Advertisement

Answer

I’m not sure of your database makeup; your server must run PHP and the user must have access to read the database; your environment must be setup properly in order to execute. I quickly dumped some data on my end by modifying your code a little on a dummy DB:

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

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

}

$q = "select * from weapons";
$result = $conn->query($q);

?>
<table align="center">
        <tr>
        <th>Image</th>
        <th>Name</th>
        <th>type</th>
        <th>rarity</th>

        </tr>
        <?php

        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td><img src='".$row['weapon_icon']."' width=199 height=188 /></td>";
            echo "<td>".$row["weapon_name"]."</td>";
            echo "<td>".$row["weapon_type"]."</td>";
            echo "<td>".$row["weapon_rarity"]."</td>";
            echo "</tr>";
            echo "</table>";
        }
?>

Keep in mind you were using mysqli_fetch_array($result); I swapped it to $result->fetch_assoc() as I want to loop through the associated records in my select statement. I also modified select to select all (*) in my environment; doesn’t make a difference much from what you had if you elect specific columns you can select just those;

You had an unclosed Table Row tag also; I’ve modified for you by ending the properly; a row per result. 🙂

The image you posted as a result looked as if PHP wasn’t even functioning and everything came through as text; just verify your PHP version and of course the credetials of the user and database.

I ran this localhost on a linux based machine with PHP5; if you can run PHP on your server; and your username is indeed privilaged to read the data and connect; you should be able to get this code working for you.

Hope it helps!

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement