query from db not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

query from db not working

Post 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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Have you checked the values of the session variables?
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post 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
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Make sure to look in the browser source, not the browser.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

How, and more importantly, when are those session variables set in relation to their use in your originally posted query?
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Query is inserted into database before session variables are set (if this is what you're asking).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

li tought it will "pick up" values from variables

than you all for reply

regards ddragas
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Functions can be placed anywhere and still call effectively. Variable must be set before they are used.
Post Reply