retrieving data using PHP
Moderator: General Moderators
retrieving data using PHP
---------- selectname.htm ---------------
<html>
<head>
<title>selectname</title>
</head>
<body>
<form action="select_name.php" method="post">
<input type="text" name="fname">
<input type="submit" value="retrieve it">
</form>
</body>
</html>
---------- selectname.php ------------------
$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$id=$row[0];
$fname=$row[1];
$lname =$row[2];
$date_entry =$row[3];
$str_apt =$row[4];
$str_name =$row[5];
$city =$row[6];
echo "id : S$id <br>";
echo "Name :$fname $lname <br>";
echo "Date : $date_entry <br>";
echo "Street & Apt # : $str_apt <br>";
echo "Street name : $str_name <br>";
echo "city : $city <br>";
#<br><br>;
}
------------------------------------------------------------
I was trying to retrieve the data by using the firstname instead of an ID # ( Anyways, I tried it with ID# by changing fname with ID- it worked okay) but with the above script, I only get a blank result. However if I change the following script:
$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";
into
$query ="SELECT * FROM sender where fname=$_POST[fname]";
I got the following error:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\selectname.php on line 19
this is my line 19:
while($row = mysql_fetch_row($result))
Please help!. Thank you
<html>
<head>
<title>selectname</title>
</head>
<body>
<form action="select_name.php" method="post">
<input type="text" name="fname">
<input type="submit" value="retrieve it">
</form>
</body>
</html>
---------- selectname.php ------------------
$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$id=$row[0];
$fname=$row[1];
$lname =$row[2];
$date_entry =$row[3];
$str_apt =$row[4];
$str_name =$row[5];
$city =$row[6];
echo "id : S$id <br>";
echo "Name :$fname $lname <br>";
echo "Date : $date_entry <br>";
echo "Street & Apt # : $str_apt <br>";
echo "Street name : $str_name <br>";
echo "city : $city <br>";
#<br><br>;
}
------------------------------------------------------------
I was trying to retrieve the data by using the firstname instead of an ID # ( Anyways, I tried it with ID# by changing fname with ID- it worked okay) but with the above script, I only get a blank result. However if I change the following script:
$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";
into
$query ="SELECT * FROM sender where fname=$_POST[fname]";
I got the following error:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\selectname.php on line 19
this is my line 19:
while($row = mysql_fetch_row($result))
Please help!. Thank you
Correction
<form action="select_name.php" method="post">
is being corrected as:
<form action="selectname.php" method="post">
still havng the same problem
is being corrected as:
<form action="selectname.php" method="post">
still havng the same problem
Hi, I'm totaly newbie here also .
try this:
Hope that was it.
Regards, B
Code: Select all
$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";Code: Select all
$query ="SELECT * FROM sender where fname=".$_POST['fname'];Regards, B
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: retrieving data using PHP
Please read the forum rules and wrap your code postings in the appropriate BBCode tags. for PHP code use [ syntax="php" ] and for html use [ syntax="html"]. Remember to close your tags. This is what your code should look like...
---------- selectname.htm ---------------
---------- selectname.php ------------------
Also, whenever you reference a value other than an integer in a mysql query, put single quotes around that value (for example, WHERE name = 'Fred' or WHERE id = 4).
Post back with what you find.
---------- selectname.htm ---------------
Code: Select all
<html>
<head>
<title>selectname</title>
</head>
<body>
<form action="select_name.php" method="post">
<input type="text" name="fname">
<input type="submit" value="retrieve it">
</form>
</body>
</html>
Code: Select all
<?php
$fname = $_POST['fname'];
echo $fname . ' is what was posted...<br />'; // Tests what was sent
$query ="SELECT * FROM sender where fname='$name'";
$result = mysql_query($query) or die("There was an error in the query: " . mysql_error()); // Use error checking to help you out
while($row = mysql_fetch_row($result))
{
$id=$row[0];
$fname=$row[1];
$lname =$row[2];
$date_entry =$row[3];
$str_apt =$row[4];
$str_name =$row[5];
$city =$row[6];
echo "id : S $id <br>";
echo "Name :$fname $lname <br>";
echo "Date : $date_entry <br>";
echo "Street & Apt # : $str_apt <br>";
echo "Street name : $str_name <br>";
echo "city : $city <br>";
}
?>
I rewrte your code a bit, with some comments. You really need to do someo error checking to make sure you are passing proper data and to make sure your queries are runing correctly. Run what's above and see what is coming out. I would bet that because you are using a string to search for and you are ujsing equal in the WHERE clause, that your result set is coming back as a zero because of a text-case issue or something of that nature.janjan376 wrote:I was trying to retrieve the data by using the firstname instead of an ID # ( Anyways, I tried it with ID# by changing fname with ID- it worked okay) but with the above script, I only get a blank result. However if I change the following script:
$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";
into
$query ="SELECT * FROM sender where fname=$_POST[fname]";
I got the following error:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\selectname.php on line 19
this is my line 19:
while($row = mysql_fetch_row($result))
Please help!. Thank you
Also, whenever you reference a value other than an integer in a mysql query, put single quotes around that value (for example, WHERE name = 'Fred' or WHERE id = 4).
Post back with what you find.
$query ="SELECT * FROM sender where fname=$_POST['fname']";
echo "$fname";
Doing the above script gives me the following error:
---------------------------
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\select_name.php on line 16
-----------------------------
but with the following code, If i typed in celine:
$query ="SELECT * FROM sender where fname=$_POST[fname]";
echo "$fname";
this is the output
-------------------------------------
celine
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\select_name.php on line 31
-----------------------------------
and for the 'Community' how do i go there?
echo "$fname";
Doing the above script gives me the following error:
---------------------------
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\select_name.php on line 16
-----------------------------
but with the following code, If i typed in celine:
$query ="SELECT * FROM sender where fname=$_POST[fname]";
echo "$fname";
this is the output
-------------------------------------
celine
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\select_name.php on line 31
-----------------------------------
and for the 'Community' how do i go there?
<?php
include 'config.php';
include 'opendb.php';
$query ="SELECT * FROM sender where fname='$fname'";
echo "$fname<br>";
$result=mysql_query($query);
echo "$result<br><br>";
while($row = mysql_fetch_row($result))
{
#$id=$row[0];
$fname=$row[1];
#$lname =$row["lname"];
#$date_entry =$row[3];
#$str_apt =$row[4];
#$str_name =$row[5];
#$city =$row[6];
echo "id : S$id <br>";
echo "Name :$fname<br>";
#echo "Name :$fname $lname <br>";
#echo "Date : $date_entry <br>";
#echo "Street & Apt # : $str_apt <br>";
#echo "Street name : $str_name <br>";
#echo "city : $city <br>";
echo"<br><br>";
}
echo "$result";
include 'closedb.php';
?>
---- result ---
celine
Resource id #5
Resource id #5
-----------------------------
as you may notice, i echoed the $fname, $query and $result. Celine id is 43, where is the message 'Resource id #5' coming from ?
include 'config.php';
include 'opendb.php';
$query ="SELECT * FROM sender where fname='$fname'";
echo "$fname<br>";
$result=mysql_query($query);
echo "$result<br><br>";
while($row = mysql_fetch_row($result))
{
#$id=$row[0];
$fname=$row[1];
#$lname =$row["lname"];
#$date_entry =$row[3];
#$str_apt =$row[4];
#$str_name =$row[5];
#$city =$row[6];
echo "id : S$id <br>";
echo "Name :$fname<br>";
#echo "Name :$fname $lname <br>";
#echo "Date : $date_entry <br>";
#echo "Street & Apt # : $str_apt <br>";
#echo "Street name : $str_name <br>";
#echo "city : $city <br>";
echo"<br><br>";
}
echo "$result";
include 'closedb.php';
?>
---- result ---
celine
Resource id #5
Resource id #5
-----------------------------
as you may notice, i echoed the $fname, $query and $result. Celine id is 43, where is the message 'Resource id #5' coming from ?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You can't run a string through a query WHERE clause without wrapping it in single quotes. You really shouldn't leave off the quotes in the array index (the fname part of $_POST). A resource id is what is returned by a call to mysql_query. You use that result resource in the calls to mysql_fetch_*. That fetch is what actually returns the data held in the query resource id.
At this point I would seriously read the PHP Manual section on MySQL. Get familiar with the proper way to execute queries and return results. You should also NOT use raw post or get data in your queries. Read them into a var, validate the var value, then run it through the query. And make sure to error check each area where there is a possible error so if there is one you'll know what it is.
At this point I would seriously read the PHP Manual section on MySQL. Get familiar with the proper way to execute queries and return results. You should also NOT use raw post or get data in your queries. Read them into a var, validate the var value, then run it through the query. And make sure to error check each area where there is a possible error so if there is one you'll know what it is.