Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Target specific session array and value in WHERE clause

Status
Not open for further replies.

lynque

IS-IT--Management
Sep 30, 2004
124
0
0
CA
I'm trying this but I think the syntax is wrong...

WHERE coCity = '".mysql_real_escape_string($_SESSION['userinfo->city'])."' AND coStProv = '".mysql_real_escape_string($_SESSION['userinfo->stProv'])."'";

Any help is appreciated
 
If the userinfo key in $_SESSION is also an array, then treat it as such.

$_SESSION['userinfo']['city']


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
$_SESSION['userinfo->stProv'] is one element from the session array, with key 'userinfo->stProv' (which is one string).

You might mean:
$_SESSION[userinfo->stProv] or
$_SESSION['userinfo']->stProv ?


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Thanks for both suggestions but neither work

Vucinita:
Notice: Undefined index: userinfo

Don:
Notice: Undefined index: userinfo
Notice: Trying to get property of non-object in

Any further help with this would be great.
 
run this script on your page and paste the results into a reply.

Code:
echo "<pre>" . print_r($_SESSION, true) . "</pre>";
 
jpadie

Array
(
[username] => Lynque
=> /login/promotions.php [user...']."<br>"; which are both populated as well
 
right. then $session is independent of php sessions. this is bad naming convention.

$session is an object. completely different to the superglobal $_SESSION. you can see the properties of the $session object by this code
Code:
echo "<pre>".print_r($session, true) . "</pre>";

most likely the variables you are looking for are
$session->userinfo['city']
and
$session->userinfo['stProv']
 
Thanks jpadie,

I used the code you provided and it printed all of the $session properties :), I tried using this in my query but it still fails:
WHERE coCity = '".mysql_real_escape_string($session->userinfo['city'])."' AND coStProv = '".mysql_real_escape_string($session->userinfo['stProv'])."'";

 
then there is a different issue in your query.

a suggestion

construct your query into a $query variable ; then
run your query like so

Code:
$result= mysql_query($query);
if ($result === false){
  echo "error with query.  <br/>error was " . mysql_error() . " <br/>Query was: $query<br/>"
 exit;
}
 
Thanks jpadie,

here is the result of the query output:

Query was:SELECT coName,coStreet,coUnit,coCity,coStProv,coType,pack FROM vendors WHERE coCity = '' AND coStProv = ''
 
and what was the error that was printed before that?
 
Notice: Undefined variable: session in

Notice: Trying to get property of non-object in

Notice: Undefined variable: session in on line 55

Notice: Trying to get property of non-object in
error with query.
error was
Query was:SELECT coName,coStreet,coUnit,coCity,coStProv,coType,pack FROM vendors WHERE coCity = '' AND coStProv = ''
 
ok. that means that at the time you were running the query, the $session variable did not exist. you need to check your code to make sure that the variables are populated before you use them.

however the result that you have posted for the query is quite impossible to obtain.

A select query returns a recordset or FALSE. it will only ever return false if there has been an error in the query (i.e. bad permissions or bad query structure). in each of these cases, mysql_error() will ALWAYS return a value.

thus either you have a completely fubar'd installation of php (very very unlikely) or you have doctored the results before posting here. If it is the latter you will appreciate that we cannot help you if you do not give us the information needed to assist.

on this note: i also see that each of the other php errors have been altered before you post them here. that is less than helpful.
 
my apologies jpadie, I certainly don't mean to waste anyone's time and am very appreciative of all of your help thus far with regard to this problem I'm having.

I'm starting to track all of the $session data from point of login to see if I can find where the problem is.

I will post back my findings.
 
you're not wasting my time. i'm simply point out that i cannot help without the information.
 
Here is my entire query:

Code:
function displayVendorInfo(){
   global $database;
   $q = "SELECT coName,coStreet,coUnit,coCity,coStProv,coType,pack FROM vendors WHERE coCity = '".mysql_real_escape_string($session->userinfo['city'])."' AND coStProv = '".mysql_real_escape_string($session->userinfo['stProv'])."'";
   $result = $database->query($q);
   /* Error occurred, return given name by default */
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
	  echo "Error displaying info";
	  return;
   }
   if($num_rows == 0){
	  echo "error with query.  <br/>error was ".mysql_error()." <br/>Query was:$q<br/>";
	  return;
   }
   /* Display table contents */
   echo "<table align=\"left\" cellspacing=\"0\" cellpadding=\"3\" width=\"750\">\n";
   echo "<tr><td><b>Company Name</b></td><td><b>Company Address</b></td><td><b>Unit Number</b></td><td><b>City</b></td><td><b>State / Province</b></td><td><b>Company Type</b></td><td><b>Package Available</b></td></tr>\n";
   for($i=0; $i<$num_rows; $i++){
	  $coname  = mysql_result($result,$i,"coName");
	  $coaddress  = mysql_result($result,$i,"coStreet");
	  $counit  = mysql_result($result,$i,"coUnit");
	  $cocity  = mysql_result($result,$i,"coCity");
	  $coStProv  = mysql_result($result,$i,"coStProv");
	  $coType  = mysql_result($result,$i,"coType");
	  $coPack  = mysql_result($result,$i,"pack");

	  echo "<tr><td>$coname</td><td>$coaddress</td><td>$counit</td><td>$cocity</td><td>$coStProv</td><td>$coType</td><td>$coPack</td></tr>\n";
   }
   echo "</table><br>\n";
}	
?>
 
And the output of the $session object

Code:
Session Object
(
    [fName] => 
    [lName] => 
    [street] => 
    [apt] => 
    [city] => 
    [stProv] => 
    [country] => 
    [pc] => 
    [phone] => 
    [username] => me
    [userid] => 993cbe7b590e2b60950c3c48ce389502
    [userlevel] => 9
    [time] => 1248455295
    [logged_in] => 1
    [userinfo] => Array
        (
            [0] => me
            [fName] => me
            [1] => me
            [lName] => me
            [2] => 215 my st
            [street] => 215 my st
            [3] => 
            [apt] => 
            [4] => Waterloo
            [city] => Waterloo
            [5] => ON
            [stProv] => ON
            [6] => Can
            [country] => Can
            [7] => N2L 1t3
            [pc] => N2L 1t3
            [8] => 123 456 7890
            [phone] => 123 456 7890
            [9] => me
            [username] => me
            [10] => 5447c93bcfce3951f4b065c5fd833551
            [password] => 5447c93bcfce3951f4b065c5fd833551
            [11] => 993cbe7b590e2b60950c3c48ce389502
            [userid] => 993cbe7b590e2b60950c3c48ce389502
            [12] => 9
            [userlevel] => 9
            [13] => me@gmail.com
            [email] => me@gmail.com
            [14] => 1248454396
            [timestamp] => 1248454396
        )

    [url] => /login/promotions.php
    [referrer] => /login/main.php
)
 
So where is $session coming from in your function?

You don't pass it to the function, and its not set inside the function? how is it supposed to be available to it then?

Try either making it Global like your $database variable, or explicitly passing it to your function.
Code:
function myfunction ($myvar){
...
}

...

myfunction($session);


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
replace this
Code:
$result = $database->query($q);
   /* Error occurred, return given name by default */
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "error with query.  <br/>error was ".mysql_error()." <br/>Query was:$q<br/>";
      return;
   }
with the following
Code:
$result = $database->query($q);
if($result === false ) {
	echo "error with query.  <br/>error was ".mysql_error()." <br/>Query was: $q<br/>";
	return;
}

and as vacunita says, since you are firing the query inside a function, $session will not be in scope and needs to be made global

Code:
global $database, $session;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top