retrieving data using PHP

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
janjan376
Forum Newbie
Posts: 6
Joined: Sat Jul 01, 2006 12:11 am

retrieving data using PHP

Post 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
janjan376
Forum Newbie
Posts: 6
Joined: Sat Jul 01, 2006 12:11 am

Correction

Post by janjan376 »

<form action="select_name.php" method="post">

is being corrected as:

<form action="selectname.php" method="post">

still havng the same problem
bogdan
Forum Commoner
Posts: 27
Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro

Post 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
janjan376
Forum Newbie
Posts: 6
Joined: Sat Jul 01, 2006 12:11 am

Post by janjan376 »

sorry but it did not work. thanks for trying.
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

please use PHP tags
janjan376
Forum Newbie
Posts: 6
Joined: Sat Jul 01, 2006 12:11 am

Post by janjan376 »

the selectname.php has php tags otherwise it would give different error message from above.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: retrieving data using PHP

Post 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.
janjan376
Forum Newbie
Posts: 6
Joined: Sat Jul 01, 2006 12:11 am

Post 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?
janjan376
Forum Newbie
Posts: 6
Joined: Sat Jul 01, 2006 12:11 am

Post 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 ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply