Forum Moderators: open

Message Too Old, No Replies

problem in retriving a record from mysql using php

         

agecon

3:41 pm on Nov 26, 2007 (gmt 0)

10+ Year Member



I am trying to retrive the record with given search critieria. here it is FacultyIDnumber.But when I try searching I could not get anything. please help me on this issue.I am new to this stuff.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<form method= "post" action="home.php">
<font face = "arial" size=0><b>Faculty ID</font><font size="5"></b>
<input name="submitFacultyIDnumber" size="17" value="<?echo $submitFacultyIDnumber;?>" maxlength="32" /></font>
<input type="submit" value="Submit">
&nbsp;&nbsp;&nbsp;<input type=reset value="Clear" name="reset"></td>
<body>
</body>
</html>

and here the query starts.
this is home.php file

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
$username = "#*$!XX";
$password = "#*$!#*$!XX";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("#*$!xx",$dbhandle)
or die("Could not select #*$!xx");

//execute the SQL query and return records
$ID=$_POST["submitFacultyIDnumber"];

$result = mysql_query("SELECT FacultyIDnumber,name,Office_location,Date_of_hiring,level FROM Faculty
WHERE FacultyIDnumber= '$ID'");

//fetch the data from the database
while ($r=mysql_fetch_array($result)) {
$FacultyIDnumber=$r["FacultyIDnumber"];
$name=$r["name"];
$Office_location=$r["Office_location"];
$Date_of_hiring=$r["Date_of_hiring"];
$level=$r["level"];
echo"$ID";
echo "$Faculty IDnumber<br> $name<br> $Office_location <br> $Date_of_hiring <br>$level <br>";";
}
//close the connection
mysql_close($dbhandle);
?>

<body>
</body>
</html>

[edited by: txbakers at 4:17 pm (utc) on Nov. 26, 2007]
[edit reason] removed username and password [/edit]

Demaestro

4:10 pm on Nov 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First I know you didn't post the URL but putting this info in a forum posting is not a good idea.

$username = "*******";
$password = "*******";
$hostname = "*******";

This along with a URL will allow someone full access to your database.

* Mods can you remove that for him?

Anyway on to your problem... The first thing I see that could be the issue is that the ID is quoted in the statement which would mean that it is a string. I am going to assume that it is an integer and needs to be unquoted.

Can you try this:

$result = mysql_query("SELECT FacultyIDnumber,name,Office_location,Date_of_hiring,level FROM Faculty
WHERE FacultyIDnumber= $ID");

All i did was remove the single quotes around the ID.

Try that, if it doesn't work post back with and error or what ever it returns.

agecon

5:10 pm on Nov 26, 2007 (gmt 0)

10+ Year Member



Thank you for your concern. I realized later that I forgot to do that..I tired your the solution you told, but it still did not work...is anything else wrong in the code?

Demaestro

5:18 pm on Nov 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The only other thing I see is that you are outputting the results between the <head> and the <body> tags.

Try moving this code between the <body></body>

<?php
$username = "#*$!XX";
$password = "#*$!#*$!XX";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("#*$!xx",$dbhandle)
or die("Could not select #*$!xx");

//execute the SQL query and return records
$ID=$_POST["submitFacultyIDnumber"];

$result = mysql_query("SELECT FacultyIDnumber,name,Office_location,Date_of_hiring,level FROM Faculty
WHERE FacultyIDnumber= '$ID'");

//fetch the data from the database
while ($r=mysql_fetch_array($result)) {
$FacultyIDnumber=$r["FacultyIDnumber"];
$name=$r["name"];
$Office_location=$r["Office_location"];
$Date_of_hiring=$r["Date_of_hiring"];
$level=$r["level"];
echo"$ID";
echo "$Faculty IDnumber<br> $name<br> $Office_location <br> $Date_of_hiring <br>$level <br>";";
}
//close the connection
mysql_close($dbhandle);
?>

[edited by: Demaestro at 5:19 pm (utc) on Nov. 26, 2007]

agecon

5:33 pm on Nov 26, 2007 (gmt 0)

10+ Year Member



I tired it but still not working.....it is not even echoing the "connected to mysql"

Demaestro

6:52 pm on Nov 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you code in some javascript alerts into the code? I am not good at PHP so m code is not correct but something like this....

<?php
$username = "#*$!XX";
$password = "#*$!#*$!XX";
$hostname = "localhost";

alert('attempting connection');

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

alert('db handle = ' + $dbhandle);

alert('attempting to select DB');

//select a database to work with
$selected = mysql_select_db("#*$!xx",$dbhandle)
or die("Could not select #*$!xx");

alert('selected db = ' + $selected);

//execute the SQL query and return records
$ID=$_POST["submitFacultyIDnumber"];

alert('submited id = ' + $ID);

alert('attempting to execute query');

$result = mysql_query("SELECT FacultyIDnumber,name,Office_location,Date_of_hiring,level FROM Faculty
WHERE FacultyIDnumber= '$ID'");

alert('result = ' + $result);

alert('attempting to write values from db');

//fetch the data from the database
while ($r=mysql_fetch_array($result)) {

alert('in while loop');

alert('FacultyIDnumber = ' + $r["FacultyIDnumber"]);
$FacultyIDnumber=$r["FacultyIDnumber"];

alert('name= ' + $r["name"]);
$name=$r["name"];

alert('Office_location= ' + $r["Office_location"]);
$Office_location=$r["Office_location"];

alert('Date_of_hiring= ' + $r["Date_of_hiring"]);
$Date_of_hiring=$r["Date_of_hiring"];

alert('level= ' + $r["level"]);
$level=$r["level"];

alert($ID);
echo"$ID";

echo "$Faculty IDnumber<br> $name<br> $Office_location <br> $Date_of_hiring <br>$level <br>";";

alert('while loop end');
}

alert('out of while loop');

//close the connection
mysql_close($dbhandle);
?>

If you can do this it should give you a much better idea of what is happening line by line. If you can report back what it is telling you it will make it easier to debug.