Retrieving Name instead of ID
Posted by: Jan (199.243.180.---)
Date: June 29, 2006 06:56PM

---------------------

<form action="select_name.php" method="POST">
<input type="text" name="fname">

--------------------


$query ="SELECT * FROM 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>;
}


----

The above script works if I changed to ID instead of FNAME. It fails when I try to change and search by FNAME(firstname) and it gives me the below error messages. My Primary key is set on ID, should I set also one for FNAME?


Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\select_name.php on line 22


Please help

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: Xajel (82.194.62.---)
Date: June 30, 2006 05:17PM

did you put the field name between two quotes ??

I mean some thing like this

$row["FNAME"]

because $row[FNAME] will not work unless the FNAME was a predefined constant, maybe I didn't test it...

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: Jan (199.243.180.---)
Date: June 30, 2006 06:16PM

If the scripts above has variable ID, it works, but if I change all the ID variables (not including variable inside the 'While') to fname so I can search by the Firstname, I got an error.

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: Jan (---.sympatico.ca)
Date: July 01, 2006 06:22AM

when I changed the php script to:

$query ="SELECT * FROM sender where fname='".$_POST['fname']."'";
$result = mysql_query($query);

while($row = mysql_fetch_row($result))
{
$id=$row[0];
$fname=$row['fname'];
$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 don't get an error messages however I am not getting anything at all. Please help.

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: Xajel (82.194.62.---)
Date: July 01, 2006 10:38AM

first, try to use the count() function like this after the while() function :-

echo count($row);

this will print the total rows in the array that created from the Query... if the output was 0 then there's something wrong with the query.



a security hint : NEVER AND NEVER put a $_POST or $_GET variables directly inside a MySQL Query without some variable changing... it's one of the easiest ways to hack the db.

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: Jan (---.sympatico.ca)
Date: July 02, 2006 04:43AM

i tried

echo count($row);

and got "1", so that means query is okay. Do I have to make fname column as a primary key?




Please help.

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: CyberSpatium (67.170.181.---)
Date: July 02, 2006 02:23PM

There are a few reasons why your php code did not work. You have some some coding and some syntax errors in your script. I have edited your script to fix these problems.

Also, in your while loop, you are using mysql_fetch_row where you should have used mysql_fetch_array. mysql_fetch_row only returns one row of data, that is why your while loop only returned one result.

So, here is your edited and fixed code.

$host = 'localhost';
$user = 'root';
$pass = '_Your_Password_Here_';
$database = '_Your Database_Here_';

$dbconnect = mysql_connect ($host, $user, $pass) or die ('Unable to connect to the database.');
$dblink = mysql_select_db ($databse, $dbconnect);

$query ="SELECT * FROM sender WHERE fname = '" . $_POST['fname'] . "'";
$result = mysql_query($query, $dbconnect);

while($row = mysql_fetch_array($result, $dbconnect))
{
echo "id : " . $row[0] . " <br>";
echo "Name : " . $row[1] . " " . $row[2] . " <br>";
echo "Date : " . $row[3] . " <br>";
echo "Street & Apt # : " . $row[4] . " <br>";
echo "Street name : " . $row[5] . " <br>";
echo "city : " . $row[6] . " <br>";
}

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: Jan (---.sympatico.ca)
Date: July 03, 2006 06:08PM

I have followed everything you said as below. I did change the mysql_fetch_row into mysql_fetch_array before because i know i was dealing with string.


<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'sendrec';

$dbconnect = mysql_connect ($host, $user, $pass) or die ('Unable to connect to the database.');
$dblink = mysql_select_db ($databse, $dbconnect);

$query ="SELECT * FROM sender WHERE fname = '" . $_POST['fname'] . "'";
$result = mysql_query($query, $dbconnect);

while($row = mysql_fetch_array($result, $dbconnect))
{
echo "id : " . $row[0] . " <br>";
echo "Name : " . $row[1] . " " . $row[2] . " <br>";
echo "Date : " . $row[3] . " <br>";
echo "Street & Apt # : " . $row[4] . " <br>";
echo "Street name : " . $row[5] . " <br>";
echo "city : " . $row[6] . " <br>";
}
?>

---------------- result ------------------------------

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\select_name.php on line 13

--------------------------------------------------------


this is my line 13:

while($row = mysql_fetch_array($result, $dbconnect))

BUT I HAVE A GOOD NEWS !!!!!!!


I noticed your :

$dblink = mysql_select_db ($databse, $dbconnect);

has a mispelling: $databse should be $database


GUESS WHAT................. YOUR CODE WORKS. Thank You Very Much. I really appreciated. Thank you for your patience. You're Good!!!

Options: ReplyQuote
Re: Retrieving Name instead of ID
Posted by: CyberSpatium (67.170.181.---)
Date: July 04, 2006 07:12AM

sorry about the typo, I did not test this code, just wrote it real quick here and just posted it.

Options: ReplyQuote


Sorry, only registered users may post in this forum.