This topic is locked
[SOLVED]

Getting User ID from Chat Username

7/18/2021 12:09:44 AM
General Tips and Tricks
D
DRCR Dev author

Hi

In the Chat template, a snippet gets the username of the current chat using echo $_SESSION["currentUserName"];

I am trying to add a button next to the username to redirect to the profile of the user which is found at https://website.com/profiles/%ID%

I've created a button, in the server code I have:

$chatuser = $_SESSION["currentUserName"];
$sql = DB::PrepareSQL("select ID from users where username = $chatuser");
DB::Exec( $sql );
$result["ID"]=$record["ID"];

And the client after:

location.href='https://website.com/profiles/'+result["ID"];

But I cant get the button to work. Any ideas on how to get user ID from the username?

-Cec

A
acpan 7/18/2021

Try to ensure your SQL returns value. A few things:

  1. You should enquote your username, it is a string.
  2. Your Prepared SQL has no effect in sanitizing your SQL without parameterized statement, you'd rather use a simple SQL Query as suggested below.
  3. I am not sure if you can get the result without a while loop (refer to PHPRunner manual)

if you want to use PreparedSQL, should be like this with parameterized placeholder:

$sql = DB::PrepareSQL("select ID from users where username = ':session.currentUserName' ");
DB::Exec( $sql );

// save and pass the DB error to client after event
if ( DB::LastError() ) {
$result["error"] = " Error: " .DB::LastError();
}
while( $record = $rs->fetchAssoc() )
{
$result["ID"] = $record["ID"];
}

OR use simple DB::Query

$chatuser = $_SESSION["currentUserName"];
$sql = "select ID from users where username = '$chatuser' " ;
$rs = DB::Query( $sql );

if ( DB::LastError() ) {
$result["error"] = " Error: ". DB::LastError();
}
while( $data = $rs->fetchAssoc() )
{
$result["ID"] =$data["ID"];
}

// pass the sql to your after client code to print it out for troubleshooting:
$result["sql"] = $sql;

Then at the Client After, console.log the 3 parameters passed.

hope it helps.

D
DRCR Dev author 7/18/2021

Thanks. This gave me the error and I was able to solve the query.

-Cec