Jump to content

Trying to access array of offset with if statement


PNewCode
Go to solution Solved by Barand,

Recommended Posts

Okay I'm nearly done with my auction project but I figured this was a different questions, so new topic.
This is what baffles me...
The part in the script that says echo $_SESSION['id']; will show the id correctly, same as if I changed it to echo $'myid';

So I'm getting the error Warning: Trying to access array offset on value of type null
on the line if($row['myid'] == $_SESSION['id']) {

I've used this method several times and I can't find what is causing this error. Any thoughts? And sorry if this should be obvious, I think I'm getting a bit fatigued on this one.

<?php


error_reporting(E_ALL);
ini_set('display_errors', '1');


  $hostname_l = "deleted for posting";
  $username_l = "deleted for posting";
  $password_l = "deleted for posting";
  $dbname_l = "deleted for posting";



  
   $conn_l = mysqli_connect($hostname_l, $username_l, $password_l, $dbname_l);
  if(!$conn_l){
    echo "Database connection error".mysqli_connect_error();
  }
  $user_id = $_SESSION['id']; 
  $myid = $_SESSION['id']; 
  $sql_l = "SELECT * FROM auction WHERE myid= 'myid' ORDER BY 'id' DESC LIMIT 1";
  
  



$result = $conn_l->query($sql_l);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
 

 $myid =  $row["myid"];
  
  }
} else {
  echo " ";
}


echo $_SESSION['id'];

if($row['myid'] == $_SESSION['id']) {

echo '

              <table width="40%" border="0" cellspacing="5" cellpadding="1" class="table-round-side2">
                <tr class="table-roundmenue44 box10"> 
                  <td class="table-roundmenue44"> 
                    YOU ARE THE WINNER!!!
                  </td>
                </tr>
              </table>

';

} else {
}
?> 

 

Link to comment
Share on other sites

  • Solution

Plus, if you are accessing $_SESSION, then you need session_start() at the top of the script.

Also errors in the query

  • You are trying to match the value in column myid with the string value 'myid'
  • You are trying to order the results by the string value 'id'
  • Like 1
Link to comment
Share on other sites

@requinix Holy Moly Batman! hahahaha. I forgot to include this if (isset($_SESSION['myid'])) { and that got rid of the error and is showing the message. But argh It's not matching the winning id and showing it to anyone logged in. Well... the error is solved anyways. Goodness I can't believe I forgot that. So it was not finding the session id to put with the column myid.

Link to comment
Share on other sites

Those are 2 different projects but yes this one is myid that is passed from a previous page that has the session_id sent to this page and then translated to myid

I know.... I make things too complicated. I'm getting the hang of it though (slowly) haha

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.