rbragg Posted August 24, 2007 Share Posted August 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
rbragg Posted August 24, 2007 Author Share Posted August 24, 2007 $roomMatch > 0 is true ... there is a match. I have errors turned off on the server but am checking the logs. The above error is what is in the log. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 24, 2007 Share Posted August 24, 2007 Well, if thats all the code in the script, $connect in undefined. If its not, then the error could be generated elsewhere in the script. Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 The error doesn't specify what variable is undefined? Quote Link to comment Share on other sites More sharing options...
rbragg Posted August 24, 2007 Author Share Posted August 24, 2007 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! Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 $roomMatch is not greater than zero. Since ociresult() returns a string, you are comparing a string to an integer. Change the if statement to: if ($roomMatch) Quote Link to comment Share on other sites More sharing options...
rbragg Posted August 24, 2007 Author Share Posted August 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 24, 2007 Share Posted August 24, 2007 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"; } Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 You can't keep that operation with the ociresult() function because it just doesn't work. Try using ocirowcount(). $roomMatch = ocirowcount($room); That should return the number of rows that the query returned. Quote Link to comment Share on other sites More sharing options...
rbragg Posted August 25, 2007 Author Share Posted August 25, 2007 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. ??? Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 The only way it would work in another part of the script is if "$roomMatch" was an number or a boolean. If you compare a string to an integer, it is always going to be false (0). Quote Link to comment Share on other sites More sharing options...
rbragg Posted August 27, 2007 Author Share Posted August 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
chronister Posted August 28, 2007 Share Posted August 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
rbragg Posted August 30, 2007 Author Share Posted August 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
rbragg Posted August 30, 2007 Author Share Posted August 30, 2007 Thanks for all of you replies. I figured it out. Of course, I needed ocifetch($room); Quote Link to comment Share on other sites More sharing options...
chronister Posted August 31, 2007 Share Posted August 31, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.