dongxing7083 2018-07-22 05:33
浏览 18
已采纳

如何使用php显示一个sql字段?

lets say I have a single record with fields; id, username and password inside of a database. how do I use php to display the username from the first record field without using a while, for or if loop?

Can some write a script for me please?

<?php
session_start();

require_once 'config.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
<?php
$name = $_SESSION["name"];
$email = $_SESSION["email"];
?>
<?php

echo "your name is " . $name. "<br>";
echo "your email address is " . $email. "<br>";

$sql = "SELECT username FROM users LIMIT 1";
$result = mysqli_fetch_object($sql);
echo = $result->username;
?>


</body>
</html>

this is the config.php file

<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_NAME', 'users');

/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
  • 写回答

1条回答 默认 最新

  • douao3636 2018-07-22 05:57
    关注

    Using PDO for prepared statements:

    $servername = "localhost";
    $username = "Database Username";
    $password = "Database Password";
    $dbname = "Database Name";
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
    }catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }
    

    Then Run your SQL query:

    $stmt = $conn->prepare('SELECT username FROM yourTableName LIMIT 1');
    $stmt->execute();
    $username = $stmt->fetchAll();
    
    echo $username[0];
    //Or foreach($username as $name){echo $name;}
    

    That code should be wrapped inside php tags <?php //the code ?>

    UPDATE from comments it showed up that you are using MYSQLI, SO you should use something like:

    $mysqli = new mysqli("localhost", "username", "password", "DBname");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
    ", mysqli_connect_error());
        exit();
    }
    
    $query = "SELECT username from users ORDER by username DESC LIMIT 1";
    $result = mysqli_query($mysqli, $query);
    $result = mysqli_fetch_object($result);
    
    echo $result->username;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?