将数据从SQL获取到PHP

时间:2016-09-08 04:43:28

标签: php html mysql sql

我在mySQL中有一个数据表,我需要帮助访问要在HTML页面上显示的信息。

以下是一些细节。

Host: 127.0.0.1:8889
username: root
password: root
database-name: gibsonek
table-name: events

这是我的代码:

<html>
  <head>
    <meta charset="UTF-8">
    <title>Gibson Ek Schedule</title>

<!--JQuery Add-->    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<!--My JS Add-->
<script src="script.js"></script>

<!--Normalize CSS Add-->
<link rel="stylesheet" href="css/normalize.css">


<!--Google Font - Open Sans - Add -->
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600'>

<!--Bootstrap Add-->
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>

<!--My CSS Add-->
<link rel="stylesheet" type="text/css" href="style.css">




  </head>




  <body>
    <div class="container">
  <div class="navbar">
    <span>Gibson Ek Schedule</span>
  </div>

  <div class="header">
    <div class="color-overlay">
      <div class="day-number"></div>
      <div class="date-right">
        <div class="day-name"></div>
        <div class="month"></div>
      </div>
    </div>

  </div>

  <div class="timeline">
    <ul id = "l">

<?php

$connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
mysql_select_db('gibsonek');

$query = "SELECT * FROM `events` WHERE 1"; 
$result = mysql_query($query);



while($row = mysql_fetch_array($result)){  


echo "<p>SQL DATA WILL GO IN HERE</p>";  
}





mysql_close(); //Make sure to close out the database connection



?>





    </ul>
  </div>  
</div>




  </body>


</html>

3 个答案:

答案 0 :(得分:0)

请使用 PDO (它更灵活,更安全)

$servername = "127.0.0.1:8889";
$username = "root";
$password = "root";
$dbname = "gibsonek";

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `events` WHERE id='1'"); 
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();

var_dump($result); // or as you like

There是一篇很好的文章。
There是最佳指南。

答案 1 :(得分:0)

<div class="timeline">
    <ul id = "l">
    <?php
        $host = "127.0.0.1:8889";
        $username = "root";
        $password = "root";
        $db_name = "gibsonek"; 

        $mysqli = new mysqli($host, $username, $password, $db_name);

        if ($mysqli->connect_error) {
               die('Error : ('. $mysqli->connect_errno .') '.$mysqli->connect_error);
        }

        $query = "SELECT * FROM `events` WHERE id='1'"; // Here if 1 means id 
        $result = $mysqli->query($query);
        while($row = $result->fetch_assoc()) {
        ?>
        <!-- Here You can add any HTML or css -->
        <li><?php echo $row["column"]; ?> </li> // write your column name what you want to show 

    <?php
      }
    ?>
    </ul>
</div>

我认为这对您来说很清楚,如果有任何疑问则留下评论。 感谢

答案 2 :(得分:-1)

我觉得你错过了什么 这是正确的代码:

<?php

    $connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
    mysql_select_db('gibsonek');

    $query = "SELECT * FROM `events` WHERE id(or any column as u want)='1'"; 
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)){  

                   print_r($row);  
    }

    mysql_close(); //Make sure to close out the database connection
    ?>