Page 1 of 1

query from db not working

Posted: Wed Jun 06, 2007 1:52 pm
by ddragas
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all

I'm having problem with query retrieved from database.

this is query inserted into db with addslashes

[syntax="sql"]
  SELECT 
  SUM(izostanci.neopravdano) AS neopravdano,
  ucenici.prezime,
  ucenici.ime
FROM
  ucenici
  INNER JOIN izostanci ON (ucenici.id = izostanci.ucenik)
WHERE
  (izostanci.skola = '" . $_SESSION["odabrana_skola"] . "') AND 
  (izostanci.sk_god = '" . $_SESSION["odabrana_skolska_godina"] . "') AND 
  (ucenici.aktivan = '1')
GROUP BY
  ucenici.prezime,
  ucenici.ime
ORDER BY
  neopravdano DESC  
when I want to execute it it does nothing, and if I replace values that are in sessions

Code: Select all

  SELECT 
  SUM(izostanci.neopravdano) AS neopravdano,
  ucenici.prezime,
  ucenici.ime
FROM
  ucenici
  INNER JOIN izostanci ON (ucenici.id = izostanci.ucenik)
WHERE
  (izostanci.skola = '1') AND 
  (izostanci.sk_god = '1') AND 
  (ucenici.aktivan = '1')
GROUP BY
  ucenici.prezime,
  ucenici.ime
ORDER BY
  neopravdano DESC  
it works perfectly.

How can I get it working retrieved from db with sessions inside query?

regards ddragas


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jun 06, 2007 4:09 pm
by superdezign
Have you checked the values of the session variables?

Posted: Wed Jun 06, 2007 5:25 pm
by ddragas
of course I did

I've replaced them with values that are in session, and then query works. (example 2) but query doesn't work if it is retrieved from db and executed.

result of that query is empty

Posted: Wed Jun 06, 2007 5:33 pm
by maliskoleather
well check again.

your session variables dont have the proper (or any) value for some reason.

make sure your error reporting is on and set to E_ALL.

Posted: Wed Jun 06, 2007 5:44 pm
by RobertGonzalez
Just to prove you are getting the right information from the SESSION array, do this just before the query:

Code: Select all

<?php
echo '<pre>'; var_dump($_SESSION); echo '</pre>';
?>
Post back what the dump shows.

Posted: Wed Jun 06, 2007 9:15 pm
by feyd
Make sure to look in the browser source, not the browser.

Posted: Thu Jun 07, 2007 2:19 am
by ddragas
here is "var_dump"

Code: Select all

array(4) {
  ["admin_skola"]=>
  string(1) "1"
  ["odabrana_skolska_godina"]=>
  string(1) "1"
  ["naziv_skola"]=>
  string(24) "Osnovna škola MonteZaro"
  ["odabrana_skola"]=>
  string(1) "1"
}
and here is code of taking out query from db.

Code: Select all

$sql_upit = mysql_query("select * from statistike where id='$vrijednost'") or die (mysql_error());
			$sql_rez = mysql_fetch_array($sql_upit);
			
			$query = stripslashes($sql_rez['upit']); // here comes retrieved from db query shown in  example 1
			
			$result = mysql_query($query) or die( mysql_error() );
			$number_cols = mysql_num_fields($result);

browser source of table where result should be shown is

Code: Select all

<table width="100%" border="0" cellspacing="1" cellpadding="0" class=text >
<tr><td align="left">Pregled ukupnih izostanaka po razredima  </td><td align="right"><a href="../tcpdf/statistika.php?id=4" target="blank" title="Ispis"><img src="../slike/pdf.jpg" border="0"/></a></td></tr></table><br><br><table width="100%" border="0" cellspacing="1" cellpadding="0" class=text >
<tr align=left>
<td>Izostanci</td>
<td>Razred</td>
</tr>
</table>

don't get it

why is it not working?

regards
ddragas

Posted: Thu Jun 07, 2007 7:12 am
by feyd
How, and more importantly, when are those session variables set in relation to their use in your originally posted query?

Posted: Thu Jun 07, 2007 4:24 pm
by ddragas
Query is inserted into database before session variables are set (if this is what you're asking).

Posted: Thu Jun 07, 2007 4:47 pm
by RobertGonzalez
So if the session variables are set after the query, how can you expect the query to have any data in the variables when it inserts?

Posted: Thu Jun 07, 2007 5:17 pm
by ddragas
li tought it will "pick up" values from variables

than you all for reply

regards ddragas

Posted: Thu Jun 07, 2007 5:42 pm
by RobertGonzalez
Functions can be placed anywhere and still call effectively. Variable must be set before they are used.