从我的SQL数据库调用值无法正常工作 [英] Calling values from my sql database isn't working properly

查看:80
本文介绍了从我的SQL数据库调用值无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

<?php
include("inc/incfiles/header.inc.php");
session_start();
$user_id = $_SESSION["id"];
if (!isset($_SESSION["id"])) {
    header("location: index.php");
}
else {
    echo "Welcome, user number " . $user_id . ". This is you're newsfeed!";
    echo "<br><a href=\"logout.php\">Log Off?</a>";
}

echo "<br><br>";

$userFname = mysql_query("SELECT first_name FROM users WHERE id=$user_id");
$userLname = mysql_query("SELECT last_name FROM users WHERE id=$user_id");

echo "First Name: " . $userFname . "<br>";
echo "Last Name: " . $userLname . "<br>"; ?>

header.inc.php文件仅用于sql connect和html头.

The header.inc.php file is just for the sql connect and the html header.

我现在的问题是,当我叫名字和姓氏时,它只给出输出资源ID#6"和资源ID#7".我不明白为什么它没有显示实际的名字和姓氏?

My problem right now is that when I call the first name and last name, it just gives outputs 'Resource id #6' and 'Resource id #7'. I don't understand why it's not displaying the actual first and last names??

推荐答案

mysql_query()返回一个result资源,而不是精算数据.

mysql_query() returns a result resource, not the actua data.

您需要使用fetch函数之一提取数据.您的代码应如下所示:

You need to extract the data with one of the fetch functions. Your code should look something like this:

$result = mysql_query("SELECT first_name, last_name FROM users WHERE id=$user_id");
if ($result === false) {
     echo mysql_error;

} else {
 list ($userFname, $userLname) = mysql_fetch_array($result);
}

这篇关于从我的SQL数据库调用值无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆