72946

Calling values from my sql database isn't working properly

Question:

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 just for the sql connect and the html header.

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

Answer1:

PHP's mysql_query() function does not return the "stuff" in your SELECT clause. Rather, it returns a resource that you can use to get that "stuff".

Try this:

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

Answer2:

The function <a href="http://php.net/manual/en/function.mysql-query.php" rel="nofollow">mysql_query</a> 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's first name

<a href="http://www.php.net/manual/en/function.mysql-fetch-object.php" rel="nofollow">mysql_fetch_object</a> will initialize an object with the table's columns as attributes.

Given your case, I would probably just fetch one user record and use both name attributes from within 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;

Answer3:

the function "mysql_query" doesn't actually return a result value, you'll next 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"]; }

Answer4:

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

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); }

Recommend

  • VB.NET XML Serialization Repeat third child Element
  • OdbcConnection.GetSchema(“TABLES”); not working
  • Rpmbuild copying folders specified by a mapping file
  • function inside for loop errors out in protractor
  • Rails - Passing Arrays as Params in Model Function
  • How can I get the same/to pass parameters between decorator and decorated function?
  • How to convert all *.potx files to *.pptx files with VBA?
  • Aptana 3 Github Clone Repository Fail
  • Insert an Arabic text MySQL
  • Conditional Validation in Ecto for OR - 1 of 2 fields is required
  • PHP Redirect after Mysql Insert
  • Escaping single quotes in PHP/MySQL insert not working
  • Help with payment_status PayPal Refunded Reversed Partially Refuned IPNs
  • Mysql optimization based on explain
  • how to check if a field is not unique
  • How do I exit a series of If / else conditions in a mysql trigger?
  • How to get result of Insert
  • Strong Parameters - Can't Access the Deeply-Nested Attributes
  • How to use ctype_alpha with UTF-8
  • Using a join with three tables when a field might be null
  • Authentication - JavaScript - Logout issue
  • Insert records into two table at once
  • Getting data from database using HQL
  • redirect user after update in class based view in django
  • Yii2 Login with database
  • How can I sync the html/session used between Django's 'Client' and Selenium's we
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • Login not working in Firefox in Meteor
  • Using $this when not in object context
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Cassandra Data Model
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • How to get next/previous record number?
  • AngularJs get employee from factory
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Proper way to use connect-multiparty with express.js?
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • How to get NHibernate ISession to cache entity not retrieved by primary key