Page 1 of 1

sum in slq statment, why don't work

Posted: Sun Feb 07, 2010 10:47 am
by mariolopes
Guys
This code works in localhost but not in my website. Any ideas?

Code: Select all

 
 <?php
session_start();
/* conexão à base de dados vendas*/
$mysql_id = mysql_connect('localhost', 'mariolopes', 'freixo');
mysql_select_db('mariolopes',$mysql_id);
/* verifica se quem acede é um aluno*/
$query="Select * from alunos where username='$_POST[username]' and password='$_POST[password]'" ;
$result=mysql_query($query);
$num_rows = mysql_num_rows($result);
echo $num_rows;
 
if ($num_rows>0){
    //apanhar o nome do aluno
    while($row=mysql_fetch_array($result)){
        
        $_SESSION['nome']=$row['Nome'];
        $_SESSION['tipo']="aluno";
        $_SESSION['username']=$row['Username'];
        $aluno=$_SESSION['nome'];
    }
    
    //faz o balanço das horas até ao momento
        $query="select Aluno, sum (N_horas) as total from registo_horas where Aluno='$aluno'";
        $result=mysql_query($query);
            while($row=mysql_fetch_array($result)){
                $_SESSION['horas']=$row['total'];
            }
            
}       
?>
 
Here is the error:

Code: Select all

 
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\inetpub\vhosts\mariolopes.com\httpdocs\estagios\lixo.php on line 25
 

Re: sum in slq statment, why don't work

Posted: Sun Feb 07, 2010 11:18 am
by mariolopes
The code works like this

Code: Select all

 
<?php
3.session_start();
4./* conexão à base de dados vendas*/
5.$mysql_id = mysql_connect('localhost', xxx, xxx);
6.mysql_select_db('mariolopes',$mysql_id);
7./* verifica se quem acede é um aluno*/
8.$query="Select * from alunos where username='$_POST[username]' and password='$_POST[password]'" ;
9.$result=mysql_query($query);
10.$num_rows = mysql_num_rows($result);
11.echo $num_rows;
12. 
13.if ($num_rows>0){
14.    //apanhar o nome do aluno
15.    while($row=mysql_fetch_array($result)){
16.        
17.        $_SESSION['nome']=$row['Nome'];
18.        $_SESSION['tipo']="aluno";
19.        $_SESSION['username']=$row['Username'];
20.        $aluno=$_SESSION['nome'];
21.    }
22.    
23.    //faz o balanço das horas até ao momento
24.        $query="select  sum (N_horas) as total from registo_horas where Aluno='$aluno'";
25.        $result=mysql_query($query);
26.            while($row=mysql_fetch_array($result)){
27.                $_SESSION['horas']=$row['total'];
28.            }
29.            
30.}       
31.?>
32. 
 
 
But i don't understand why.