<?php 
        $username = $_SESSION['username'];
        $result = 'sample';
        $con = mysql_connect("localhost","root","");

        if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

        $sql = "Select FirstName from Person Where LoginID = (Select LoginID from UserLogin Where Username = '$username')";
        $result = mysql_query($sql,$con);

        echo $result;
?>

I'm kinda new to this server-side scripting language (php) and I've been burning the midnight oil trying to figure out, how to get my single value result to display... perhaps any of you out there could help me solve this problem? I would appreciate all the help I can get.

Recommended Answers

All 7 Replies

mysql_query returns a resource datatype on success, which is not simply printable like you are trying to do.

You need to call either mysql_fetch_array or mysql_fetch_assoc passing the $result to that function, which will return each row as an Array structure that can be iterated and printed as any typical array.

Example:

$sql = "Select FirstName from Person Where LoginID = (Select LoginID from UserLogin Where Username = '$username')"

$result = mysql_query($sql,$con);

while ($row = mysql_fetch_assoc($result)) {
    echo $row["FirstName"];
}

$result will returns as resource type.

Better try this,

while($Row = mysql_fetch_array($result)) {
   echo $Row['FirstName'];
}

Thanks for the replies gentlemen,

okay, I've done as recommended but in return I would get the following warning message upon execution:-

"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given..."

am I on the right course, or am I getting farther away from my expected result?

Looks like an error occurred. Did you use mysql_select_db ?

commented: of course! it would seem that I have forgotten about selecting my database! +0

@pritaeas: I don't think I have used "mysql_select_db".

actually i'm not entirely sure if my connection has been properly established, because I don't really understand the steps required in making the connection to a database. I only took code snippets from here and there and assumed that it was all i need to get things to work.

@pritaeas: of course! I finally understand what I am missing out on. Thank you for mentioning it!

it would seem that I have forgotten to select my database after making the connection! it was a silly mistake on my part.

Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.