Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Very basic PHP script querying Mysql database, but getting errors
Old 01-13-2010, 02:21 PM Very basic PHP script querying Mysql database, but getting errors
Junior Talker

Posts: 4
Trades: 0
Hi, I am very new to Php and My sql and I am trying to build a small PHP application using MySql database.Its a Railway timetable application for my college project.

The Database is like this .Train no and arriving time in station (24Hour format)



And the PHP script is this (I am not including config.php code here)
Read the comments to know the working.

PHP Code:
=======================
//Scripts accepts two variables using GET method . First is '$s_from' (journey starting from) and 2nd is '$s_to' (journey ends at).

<?php include("includes/config.php");

$s_from=$_GET['sfrom']; //journey starting from
$s_to=$_GET['sto']; // journey upto
$time1=$_GET['time1']; //upper bound of time to list the trains between time1 and time2
$time2=$_GET['time2']; //lower bound of time to list the trains between time1 and time2

$query "SELECT $s_from,$s_to,tr_no FROM `down` WHERE ( $s_from BETWEEN $time1 AND $time2 ) AND $s_to !='' ";

/*
Query - This query selects time at train_no,$s_from, $s_to i.e.
train number and time of the trains , which arrive at the $s_from station 
between $time1 and $time2 AND also reaches the $s_to station (it is done 
by checking if the time colunm of $s_to is populated or not,if its blank then 
it dosen't stop at that station)
*/

$result mysql_query($query) or die("Query failed ($query) " mysql_error());
if(
mysql_num_rows($result))
{
 while(
$r mysql_fetch_assoc($result))
   {

$s_from $r['s_from']; //saving time at first atation in the variable $s_from 
$s_to $r['s_to']; //saving time at first atation in the variable $s_to

echo"$s_from - $s_to"//printing the data
echo "<br />";

    }
}

?>
When executing this page I am getting this error

Code:
Notice: Undefined index: s_from in xxxxxxxxxxxx on line 33

Notice: Undefined index: s_to in xxxxxxxxxxxx on line 34
-
Please help me out
cool_dude_12 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-13-2010, 03:34 PM Re: Very basic PHP script querying Mysql database, but getting errors
Knight13's Avatar
Defies a Status

Posts: 10,289
Name: Knight13
Location: Cleveland, Ohio
Trades: 0
I think you need to do something like this

PHP Code:
if(isset($_GET['sfrom']))
{
   
$s_from $_GET['sfrom'];

Knight13 is offline
Reply With Quote
View Public Profile
 
Old 01-13-2010, 04:11 PM Re: Very basic PHP script querying Mysql database, but getting errors
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
A "notice" is not an error.
It's the way that PHP tells you that it had to interpret a bit of code.
You see this because you have set the error reporting level high enough to see them.

It's a good practice to use them, but even if you have some notice, no need to panic, those are not errors.

Now, in your case, as knight13 pointed out, you used a string that was not included between single quotes nor double quotes.
This means that PHP searched for a constant named "s_from", "s_to" and such.
As it didn't found any, he converted it to a real string, and looked up the array for that string.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 01-13-2010 at 04:13 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-13-2010, 04:13 PM Re: Very basic PHP script querying Mysql database, but getting errors
Junior Talker

Posts: 4
Trades: 0
Thanks for your reply Knight13,I just tried it but it didn't helped
I am stuck trying to fix this from hours now,Tried almost everything I knew


Another weird thing which I noticed is that,If I replace all "s_from", "s_to" with a name of a station,It works fine,Just like it should !! But doesn't work when I pass a station name as a variable to this page

Last edited by cool_dude_12; 01-13-2010 at 04:16 PM..
cool_dude_12 is offline
Reply With Quote
View Public Profile
 
Old 01-13-2010, 04:17 PM Re: Very basic PHP script querying Mysql database, but getting errors
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Then give us the complete page code, so that the message can be matched with a code line.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-13-2010, 05:00 PM Re: Very basic PHP script querying Mysql database, but getting errors
Junior Talker

Posts: 4
Trades: 0
Thanks for your replies

The page code is below
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<?php include("includes/config.php");
if(isset(
$_GET['sfrom']))
{
   
$s_from=mysql_real_escape_string($_GET['sfrom']);
}  

if(isset(
$_GET['sto']))
{
   
$s_to=mysql_real_escape_string($_GET['sto']);
}  

$time1=mysql_real_escape_string($_GET['time1']);
$time2=mysql_real_escape_string($_GET['time2']);

$query "SELECT $s_from,$s_to,Train,Speed FROM down WHERE ( $s_from BETWEEN '$time1' AND '$time2' ) AND $s_to !=''";
$result mysql_query($query) or die("Query failed ($query) " mysql_error());

echo 
"$query";

if(
mysql_num_rows($result))
{
 while(
$r mysql_fetch_assoc($result))
   {
$train $r['Train'];
$speed $r['Speed'];
$s_to $r['s_to'];

echo 
"$train";
echo 
"$speed";
echo 
"$s_to";
echo 
"<br />";

;}
//while

}//if

?>
</body>
</html>
I 'ECHO'ED the SQL query in this page and executed seperately,and It worked fine.

The error message for this page is
Code:
Notice:  Undefined index: s_to in D:\xxxxxxxxxxx\timetable.php on line 34
and Line no 34 refers to
Code:
$s_to = $r['s_to'];

Last edited by cool_dude_12; 01-13-2010 at 05:41 PM..
cool_dude_12 is offline
Reply With Quote
View Public Profile
 
Old 01-13-2010, 05:40 PM Re: Very basic PHP script querying Mysql database, but getting errors
Junior Talker

Posts: 4
Trades: 0
And Finally I fixed it !

It was a silly mistake .Just replaced line 34 with
Quote:
$s_to = $r['s_to'];
With

Quote:
$new_var1="$s_to";
$s_to = $r["$new_var1"];
$s_to = $r['s_to'] was searching for 's_to' (a static text) element in the array instead of the station name i.e. $s_to ( a variable)

Thanks for your time and help everyone !

Last edited by cool_dude_12; 01-13-2010 at 05:43 PM..
cool_dude_12 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Very basic PHP script querying Mysql database, but getting errors
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25313 seconds with 12 queries