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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器