/    Sign up×
Community /Pin to ProfileBookmark

not displaying output from database

I am new to php…
following is the code which should give result from database, but it is not showing it.
<tr>
<td> Student ID: <label id=”Stud_ID1″ for=”ID”></label></td>
<?php echo $_POST[‘Stud_ID’];?>
<?php
define (‘DB_HOST’,’204.158.158.29′); // Host name
define(‘DB_USER’,’rakesh’); // Mysql username
define(‘DB_PASSWORD’,’ww23′); // Mysql password
define(‘DB_NAME’,’start’); // Database name

// Connect to server and select database.
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die(“Could not connect”);
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected)
{
die(“Can not use”.DB_NAME.’:’.mysql_err());
}
$query2 =mysql_query(“SELECT Stud_ID FROM Student WHERE Stud_ID =’$Stud_ID'”) or die(‘wrong query’.mysql_error());

?>

<td> <label id=”degree”>Degree</label></td>
</tr>
<tr>
<td> <label id=”name”> Student Name:</label></td>
<?php
$Stud_ID = $_POST[‘Stud_ID’];
$query2 =mysql_query(“SELECT Stud_Lastname, Stud_Firstname FROM Student WHERE Stud_ID =’$Stud_ID'”) or die(‘wrong query’.mysql_error());
while($row = mysql_fetch_array($query2,0))
{
echo $row[‘Stud_Lastname’] . ” ” . $row[‘Stud_Firstname’];
}
?>

<td><label id=”advior”>Advisor</label> </td>
</tr>

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@hamburglarJan 09.2012 — Is the variable $stud_id actually being set? have you tried to echo that?

If nothing is showing and there is no error then the mysql statements might not be retrieving any rows.
Copy linkTweet thisAlerts:
@swapna10authorJan 09.2012 — Yes I have already declared it..

$Stud_ID = $_POST['Stud_ID'];
Copy linkTweet thisAlerts:
@DasherJan 09.2012 — while($row = mysql_fetch_array($query2,0)) // the 0 may cause no data to be returned.

Should be

while($row = mysql_fetch_array($query2,MYSQL_ASSOC))

or

while($row = mysql_fetch_array($query2))
Copy linkTweet thisAlerts:
@swapna10authorJan 09.2012 — I did change it to ($row = mysql_fetch_array($query2)), but it is still giving me following errors -

Notice: Undefined index: Stud_ID in /opt/lampp/htdocs/Student_Mainpage.php on line 11

That line is - [B]line 11 - <?php echo $_POST['Stud_ID'];?>[/B]

Notice: Undefined variable: Stud_ID in /opt/lampp/htdocs/Student_Mainpage.php on line 31

That line is - [B]$query2 =mysql_query("SELECT Stud_ID FROM Student WHERE Stud_ID ='$Stud_ID'") or die('wrong query'.mysql_error()); [/B]

Notice: Undefined index: Stud_ID in /opt/lampp/htdocs/Student_Mainpage.php on line 39

That line is - [B] $Stud_ID = $_POST['Stud_ID'];[/B]
Copy linkTweet thisAlerts:
@WebWarriorJan 09.2012 — If anything, I would say that the $_POST array doesn't have the index/value for the Stud_ID. Hence the error you are getting. I would double-check to see if the value for Stud_ID is properly passed (via POST method). The problem is likely with the script that passes the Stud_ID. If it's a form, I would check that the name for the input field is properly set (i.e. name="Stud_ID").
Copy linkTweet thisAlerts:
@DasherJan 09.2012 — I don't know why you have the first $query2 in the code you never retrieve any data from it. And $Stud_ID is not filled until after it. You don't show the code that actually posts the $_POST[] data, what does the echo $_POST['Stud_ID'] print?

Also why not just put the echo inside the rest of the php code? i.e.

[code=php]<?php

$Stud_ID = $_POST['Stud_ID'];
echo $Stud_ID . "<br>";

define ('DB_HOST','204.158.158.29'); // Host name
define('DB_USER','rakesh'); // Mysql username
define('DB_PASSWORD','ww23'); // Mysql password
define('DB_NAME','start'); // Database name

// Connect to server and select database.
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die("Could not connect");
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected)
{
die("Can not use".DB_NAME.':'.mysql_err());
}
/*
No need for the next line... no data is retrieved from it.
$query2 =mysql_query("SELECT Stud_ID FROM Student WHERE Stud_ID ='$Stud_ID'") or die('wrong query'.mysql_error());
*/
// rest of code follows...
?>

[/code]
Copy linkTweet thisAlerts:
@swapna10authorJan 10.2012 — I have made changes according to your suggestions. It is not giving any error thanks for that. But it is still not giving any output too.... confused now... I dont know what to do? Any suggestion for why it is not giving output?
Copy linkTweet thisAlerts:
@DasherJan 10.2012 — Try this.. restructure you query a little.

[code=php]from

$query2 =mysql_query("SELECT Stud_Lastname, Stud_Firstname FROM Student WHERE Stud_ID ='$Stud_ID'") or die('wrong query'.mysql_error());

to

$query = "SELECT Stud_Lastname, Stud_Firstname FROM Student WHERE Stud_ID ='$Stud_ID'";

echo $query . "<br>";

$query2 = mysql_query($query) or die('wrong query'.mysql_error());[/code]


Make sure the actual query and Stud_ID appears as you would expect it too.

Make sure the column names are exactly right as compared to the table design.

Does the query work when you use phpMyAdmin? It is a great tool for debugging queries.
Copy linkTweet thisAlerts:
@LaffinTooJan 10.2012 — Notice: Undefined index: Stud_ID in /opt/lampp/htdocs/Student_Mainpage.php on line 11

That line is - line 11 - <?php echo $_POST['Stud_ID'];?>[/quote]

Form/Field was empty on submission, where is your validation of form fields?

if you change this line$Stud_ID = $_POST['Stud_ID']; to $Stud_ID = 1;
do you get expected results?

if so check your form
Copy linkTweet thisAlerts:
@ssystemsJan 16.2012 — [CODE]
echo '<pre>';
print_r($_POST);
echo '</pre>';
[/CODE]


Keep in mind PHP is case sensitive.
×

Success!

Help @swapna10 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...