Calling values ​​from my sql database is not working properly

This is my code:

<?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>"; ?> 

The header.inc.php file is for sql and html header only.

My problem right now is that when I call the name and surname, it simply displays “Resource Identifier No. 6” and “Resource Identifier No. 7”. I do not understand why it does not display the actual names and surnames?

0
source share
4 answers

The mysql_query() PHP function does not return "stuff" in your SELECT . Rather, it returns a resource that you can use to obtain this "material."

Try the following:

 // Obtain the resource: $resource = mysql_query("SELECT first_name FROM users WHERE id=$user_id"); // From that resource, get a row of data as an associative array // The keys in that array will match the names in your SELECT clause $row = mysql_fetch_assoc($resource); echo "First Name: " . $row['first_name'] . "<br/>"; 
0
source

The mysql_query function returns a result set that you still need to parse. Add this step:

 $result = mysql_query("SELECT first_name FROM users WHERE id=$user_id"); $userFname = mysql_fetch_object($result); echo $userFname->first_name; //will print the user first name 

mysql_fetch_object initializes the object using table columns as attributes.

Given your case, I will probably just extract one user record and use both name attributes from the same object:

 $result = mysql_query("SELECT * FROM users WHERE id=$user_id"); $user = mysql_fetch_object($result); echo $user->first_name; echo $user->last_name; 
0
source

the mysql_query function does not actually return the result value, you will need to call something like this:

 $result = mysql_query("SELECT first_name FROM users WHERE id=$user_id"); $row = mysql_fetch_assoc($result); $userFname = $row["first_name"]; $result = mysql_query("SELECT last_name FROM users WHERE id=$user_id"); $row = mysql_fetch_assoc($result); $userLname = $row["last_name"]; 

Or even better:

 $result = mysql_query("SELECT first_name, last_name FROM users WHERE id=$user_id"); $row = mysql_fetch_assoc($result); if($result) { $userFname = $row["first_name"]; $userLname = $row["last_name"]; } 
0
source

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

You need to extract data using 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); } 
0
source

Source: https://habr.com/ru/post/1489200/


All Articles