3 Replies - 989 Views - Last Post: 28 October 2012 - 12:16 PM Rate Topic: -----

#1 maram_al  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-August 12

error in my sql query

Posted 28 October 2012 - 04:41 AM

0 down vote favorite


i hava this table with this attributes http://im20.gulfup.com/kOss1.png i will send section number so i want i sql query that i can get each studentid with all his dates that he absent the table continue only the absent dates for example if you look at the record studentId 1 have 3 date that he absent on it

her is my code in php hope you can help me

if(isset($_GET['SectionID']))
{
$SectionID = $_GET['SectionID'];


$result = mysql_query("select s.StudentID,attends a where 
 a.SectionID='$SectionID' && ") or die(mysql_error());




check for empty result
if (mysql_num_rows($result) > 0)
 {
    // looping through all results


     $response["history"] = array();
     while ($row = mysql_fetch_array($result)) 
     {
        // temp user array
            $history=array();
            $history["StudentID"] = $row["StudentID"];


        // push single product into final response array
        array_push($response["history"], $history);
        // success
        }



Is This A Good Question/Topic? 0
  • +

Replies To: error in my sql query

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: error in my sql query

Posted 28 October 2012 - 11:46 AM

Some of your mistakes:
  • Using deprecated mysql functions. You should be using PDO or MySQLi prepared statements as it says in the link.
  • Having used insecure functions you failed to scrub user supplied data.
  • You continually reset the $history array to be null, so I'd expect that you only get the last record from the loop.

This post has been edited by CTphpnwb: 28 October 2012 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 maram_al  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-August 12

Re: error in my sql query

Posted 28 October 2012 - 11:50 AM

View PostCTphpnwb, on 28 October 2012 - 11:46 AM, said:

Some of your mistakes:
  • Using deprecated mysql functions. You should be using PDO or MySQLi prepared statements as it says in the link.
  • Having used insecure functions you failed to scrub user supplied data.
  • You continually reset the $history array to be null, so I'd expect that you only get the last record from the loop.

i know that histpry is null i want to insert the date with student id on it can you tell me how
Was This Post Helpful? 0
  • +
  • -

#4 maram_al  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-August 12

Re: error in my sql query

Posted 28 October 2012 - 12:16 PM

her is pic of my table
http://im20.gulfup.com/kOss1.png
and her is my code


<?php

/*
 * Following code will list all the course
 */
 
// array for JSON response
$response = array();
 
// include db connect class
require_once __DIR__ . '/db_connect.php';
 
// connecting to db
$db = new DB_CONNECT();
// get all courses from student table
//if(isset($_GET['SectionID']))
//{
//$SectionID = $_GET['SectionID'];


$result = mysql_query("SELECT StudentID, Date FROM attends WHERE SectionID='77'") or die(mysql_error());


if (mysql_num_rows($result) > 0)
 {
    // looping through all results
 

	 $response["absent"] = array();
	 $absent = array();
	 
while ($row = mysql_fetch_array($result))
 {
  if (!is_array($absent[$row['StudentID']])) {
    $absent[$row['StudentID']] = array();
	  array_push($response["absent"], $absent);
  }
  $absent[$row['StudentID']][] = $row['Date'];
}
	
    $response["success"] = 1;
    // echoing JSON response
    print (json_encode($response));

} 
else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No student found";
 
    // echo no users JSON
    print (json_encode($response));
}
//}
 
?>




i will display each studentid with all his dates that he was absent. The table continue only the absent dates.

For example if you look at the record studentId 1, have 3 dates that he was absent on.

i do an example in section id 77

but there is problem





but there is an error that they did not fund student id 2 with date and her is what happened
( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined offset: 1 in C:\wamp\www\SmsPhp\view_history.php on line 34
Call Stack
#   Time    Memory  Function    Location
1   0.0005  249216  {main}( )   ..\view_history.php:0

( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined offset: 2 in C:\wamp\www\SmsPhp\view_history.php on line 34
Call Stack
#   Time    Memory  Function    Location
1   0.0005  249216  {main}( )   ..\view_history.php:0
{"absent":[{"1":[]},{"1":["1990-09-27","2012-09-27","2012-10-01"],"2":[]}],"success":1}


This post has been edited by maram_al: 28 October 2012 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1