Page 1 of 1
retrieving data using PHP
Posted: Sat Jul 01, 2006 12:24 am
by janjan376
---------- 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
Correction
Posted: Sat Jul 01, 2006 12:27 am
by janjan376
<form action="select_name.php" method="post">
is being corrected as:
<form action="selectname.php" method="post">
still havng the same problem
Posted: Sat Jul 01, 2006 12:50 am
by bogdan
Hi, I'm totaly newbie here also .
Code: Select all
$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";
try this:
Code: Select all
$query ="SELECT * FROM sender where fname=".$_POST['fname'];
Hope that was it.
Regards, B
Posted: Sat Jul 01, 2006 8:55 am
by janjan376
sorry but it did not work. thanks for trying.
Posted: Sat Jul 01, 2006 9:35 am
by AshrakTheWhite
please use PHP tags
Posted: Sat Jul 01, 2006 9:58 pm
by janjan376
the selectname.php has php tags otherwise it would give different error message from above.
Re: retrieving data using PHP
Posted: Sun Jul 02, 2006 12:00 am
by RobertGonzalez
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 ---------------
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>
---------- selectname.php ------------------
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>";
}
?>
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
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.
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.
Posted: Sun Jul 02, 2006 10:59 pm
by janjan376
$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?
Posted: Sun Jul 02, 2006 11:43 pm
by janjan376
<?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 ?
Posted: Mon Jul 03, 2006 12:10 am
by RobertGonzalez
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.