Jump to content

PHP Notice: Undefined variable


rbragg

Recommended Posts

Why would the following give PHP Notice: Undefined variable:

 

<?php
# see if room already exists
$queryRoom = "
SELECT NVL(COUNT(*),0) AS ROOM
FROM schema.table
WHERE room = '" . $_SESSION['room'] . "'
";
$room = ociparse($connect, $queryRoom);
ociexecute($room);
            
$roomMatch = ociresult($room, "ROOM");
            
if ( $roomMatch > 0 )  # found existing room name
{
  $roomNum = ociresult($room,"ROOM_NUM");
}

echo "<br>room_num of match: " . $roomNum; 
?>

 

I had entered data into the field objects. They SHOULD have been set. I'm trying to figure out why they are not.

Link to comment
Share on other sites

What variable is undefined? Most likely, $roomMatch is not greater than zero when you run it so $roomNum doesn't get initialized. Make sure your query is correct. I would suggest using or die(ocierror()); after all your oci stuff so that it tells you if there is an error, but I think it is just your query.

Link to comment
Share on other sites

Ginger, $connect is defined b/c I use this variable in other queries on the page like INSERTs and those values are successfully entered into the db.

 

lemmin, the error:

 

[Fri Aug 24 15:38:23 2007] [error] PHP Notice:  Undefined variable:  roomNum in (omitted directory structure)\\addcallconfirm.php

  on line 224

 

Thanks for continued help!

Link to comment
Share on other sites

What you say makes great sense. Let me explain what I'm trying to do. I'm checking the db for a match to $_SESSION['room']. If there is a match, I want to get the room_num of that room. So, I'd like to keep the > 0 operation.

 

I know how to do this in mySQL but am learning SQL and am uncertain.

Link to comment
Share on other sites

Well i would guess, the problem is that you echo roomNum regardless of wether or not there was a match. Try:

 

if ( $roomMatch > 0 )  # found existing room name
{
  $roomNum = ociresult($room,"ROOM_NUM");
echo "<br>room_num of match: " . $roomNum; 
}else{
echo "No matching room found";
}

 

 

Link to comment
Share on other sites

Thanks for the replies!

 

Ginger, there IS a match there and I'm testing it. I have an else part of the script commented out, which will be used in production.

 

lemmin, I am using this same count and ociresult() in another page when matching user/pass to the db. So, I know it works.  ???

Link to comment
Share on other sites

lemmin, I have used ocirowcount:

 

$roomMatch = ocirowcount($room);

 

When I print $roomMatch I correctly get 1. My problem is that inside the if statement, I am trying to then use values in that one record:

 

$roomNum = ociresult($room,"ROOM_NUM");

 

But, $roomNum is empty even though there is a value in the db.

Link to comment
Share on other sites

lemmin, I have used ocirowcount:

 

$roomMatch = ocirowcount($room);

 

When I print $roomMatch I correctly get 1. My problem is that inside the if statement, I am trying to then use values in that one record:

 

$roomNum = ociresult($room,"ROOM_NUM");

 

But, $roomNum is empty even though there is a value in the db.

[qoute]

$roomMatch = ociresult($room, "ROOM");

           

if ( $roomMatch > 0 )  # found existing room name

{

  $roomNum = ociresult($room,"ROOM_NUM");

}

 

Your most recent statement doesn't match what you first posted. You are not using "$roomMatch = ocirowcount($room);," or at least, you said you aren't. If you are now, it would be helpful if you would post your newest code so that people can see what needs to be changed.

 

 

Assuming you are still using that code, You can use my first post as the answer, still, because you can't use that comparison. change

$roomNum = ociresult($room,"ROOM_NUM");

 

to:

$roomMatch = ocirowcount($room);

 

You will then be making a proper comparison and the ociresult won't be in $roomMatch.

Link to comment
Share on other sites

I would just turn error reporting down a little. Set it to E_ERROR and problem solved. Lots of scripts will give a warning like this. This snippet would return an undefined variable warning.

 

<?php

if(isset($somevar))
{
    echo 'lets get busy';
}

?>

 

It is because the variable has not been explicitly defined. Is this the best solution.... probably not, but it is just a warning and should not keep the script from running.

 

Nate

Link to comment
Share on other sites

Your most recent statement doesn't match what you first posted. You are not using "$roomMatch = ocirowcount($room);," or at least, you said you aren't. If you are now, it would be helpful if you would post your newest code so that people can see what needs to be changed.

 

What I meant was that I have tried both methods and both let me count the matches. My problem is that after that, I would like to get the data contained in one of the fields (room_num) and that is not happening with either method.

Link to comment
Share on other sites

I would just turn error reporting down a little. Set it to E_ERROR and problem solved. Lots of scripts will give a warning like this. This snippet would return an undefined variable warning.

 

<?php

if(isset($somevar))
{
    echo 'lets get busy';
}

?>

 

It is because the variable has not been explicitly defined. Is this the best solution.... probably not, but it is just a warning and should not keep the script from running.

 

Nate

 

Uhm, not really, that's the part of the reason to use isset(). It helps prevent those warnings. This code would not generate any errors.

Link to comment
Share on other sites

I would just turn error reporting down a little. Set it to E_ERROR and problem solved. Lots of scripts will give a warning like this. This snippet would return an undefined variable warning.

 

<?php

if(isset($somevar))
{
    echo 'lets get busy';
}

?>

 

It is because the variable has not been explicitly defined. Is this the best solution.... probably not, but it is just a warning and should not keep the script from running.

 

Nate

 

Uhm, not really, that's the part of the reason to use isset(). It helps prevent those warnings. This code would not generate any errors.

 

Thanks Jesi,

 

Now that you mention it, thats why I got in the habit of using isset() in the first place. What I *meant* to type was

 

<?php
if($somevar)
{
    echo 'lets get busy';
}
?>

 

lol, lose the bad habits and look like a dummy when trying to help someone :)

 

Thanks,

 

Nate

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.