-1

I am able to connect my mysql database but it wont echo any data. It outputs connection successful but still echos no data. Ive triple checked for misspellings I cant figure this out.

Ive looked at privileges for users, i just dont understand how it connects but wont echo the data.

index.php output

code

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbName = "printshop";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbName, $dbport);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
/* Left Nav
-----------------------------------------------------------------------------*/
body {
 margin: 0px;
 padding: 0px;
 margin-left: 0px;
 height: 100vh;
}

.maincontent {
 margin-left: 200px;
 padding: 0px 0px;
 height: 1000px;
 background-color: rgb(238, 238, 238);
}

.sidebar {
 margin: 0;
 padding: 0;
 width: 200px;
 background-color: #28343a;
 position: fixed;
 height: 100%;
 overflow: auto;
  }
  
  .sidebar a {
 display: block;
 color: rgb(255, 255, 255);
 padding: 16px;
 text-decoration: none;
  }
   
  .sidebar a.active {
 background-color: #327491;
 color: white;
  }
  
  .sidebar a:hover:not(.active) {
 background-color: #1f282c;
 color: white;
  }
  
  div.content {
 margin-left: 200px;
 padding: 1px 16px;
 height: 1000px;
  }
  
  @media screen and (max-width: 700px) {
 .sidebar {
   width: 100%;
   height: auto;
   position: relative;
 }
 .sidebar a {float: left;}
 div.content {margin-left: 0;}
  }
  
  @media screen and (max-width: 400px) {
 .sidebar a {
   text-align: center;
   float: none;
 }
  }

  /*Cards*/
  .cardcontainer {
   padding-top: 20px;
   margin-left: 20px;
   margin-right: 20px;
   display: flex;
  }

  div.card {
 width: 250px;
 box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
 text-align: center;
 margin: 20px;
  }
  
  div.header {
 background-image: linear-gradient(to bottom right, rgb(51, 96, 126), rgb(56, 104, 150));
 color: white;
 padding: 10px;
 font-size: 40px;
  }

  div.headergreen {
 background-image: linear-gradient(to bottom right, rgb(78, 168, 75), rgb(55, 124, 37));
 color: white;
 padding: 10px;
 font-size: 40px;
  }
  
  div.container {
 padding: 10px;
 background-color: white;
 font-weight: bold;
  }
<?php
 include_once 'includes/dbh.inc.php'
?>

<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src='https://kit.fontawesome.com/a076d05399.js'></script>
</head>

<body>
    <div class="sidebar">
        <a class="active" href="#home">Home</a>
        <a href="#news">Jobs</a>
        <a href="#contact">Contacts</a>
        <a href="#about">Invoices</a>
    </div>

    <div class="maincontent">
        <div class="cardcontainer">
            <div class="card">
                <div class="header">
                    <h1>8</h1>
                </div>
                <div class="container">
                    <p>Jobs In Production</p>
                </div>
            </div>

            <div class="card">
                <div class="headergreen">
                    <h1>3</h1>
                </div>
                <div class="container">
                    <p>Jobs Completed</p>
                </div>
            </div>
        </div>
    </div>

    <?php
   $sql = "SELECT firstname FROM customers;";
   $results = mysqli_query($conn, $sql);
   $resultCheck = mysqli_num_rows($result);

   if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
     echo $row['firstname'];
    }
   }
  ?>

</body>

</html>

2 Answers2

-1

There shouldn't be a ';' in this line of your script

$sql = "SELECT firstname FROM customers;";

Should be:

$sql = "SELECT firstname FROM customers";
Jaocampooo
  • 509
  • 2
  • 10
-1

First Try to remove Extra Comma
From this $sql = "SELECT firstname FROM customers;";
To this $sql = "SELECT firstname FROM customers";

If still above not worked try something like this

include('yourConfig.php'); // Have you included this connection file??
$conn->set_charset('utf8');
$sql = "SELECT firstname FROM customers";
$result = $conn->query($sql);
$resultCheck = mysqli_num_rows($result);

// output data of each row
if ($resultCheck > 0) {
    while($row = $result->fetch_assoc()) 
    {
        echo $row["commonname"];
    } else {
        echo "please try again";
    }
}

mysqli_close($conn);