0

I am trying to get an old PHP script to work but I keep getting this notice:

Notice: Trying to get property of non-object in ...

The notice is based on the last line of this code:

$result_id_check = mysql_query("select ses_userid from fe_sessions where ses_id = '".$_COOKIE['fe_typo_user']."';");
$row_check = mysql_fetch_object($result_id_check);

if ($row_check->ses_userid) {

I also tried using mysqli_query and mysqli_fetch_object but that won't take any changes.

Any ideas how I can resolve this?

Pom Canys
  • 369
  • 2
  • 5
  • 16

2 Answers2

1

This error normally means that the query failed.

You should be checking for errors as you go like this, also the string concatenation can be made simpler if you use either the {$_COOKIE['fe_typo_user']} form of variable expansion inside a double quoted string, or alternatively this will also work $_COOKIE[fe_typo_user]

$sql = "select ses_userid 
        from fe_sessions 
        where ses_id = '{$_COOKIE['fe_typo_user']}'"

$result_id_check = mysql_query($sql);

if ( $result_id_check === false ) {
    echo mysql_error();
    echo 'Bad SQL : ' . $sql;
    exit;
}

$row_check = mysql_fetch_object($result_id_check);

This way when you make small mistakes with your SQL, you get told about them directly and you dont have to wonder what nay have gone wrong.

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the PDO database extensions. Start here

You should also be using parameterized queries ( available in the mysqli_ and PDO database extensions, but not the old deprecated mysql_ extension) to avoid SQL Injection Attack Specially if you are using data got from $_POST or $_GET or $_COOKIE

If you are considering moving to mysqli_ or PDO you should read also read this Can I mix MySQL APIs in PHP?

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Before using $row_check->ses_userid just check whether $row_check is true or false.

if ($row_check) {
    if ($row_check->ses_userid) {

    }
}
vartika
  • 494
  • 2
  • 12