Page 1 of 1

display specific row from a texbox value

Posted: Thu Feb 21, 2013 4:12 pm
by newbie021
I have this codes here: (index.php)

Code: Select all

$sql="SELECT name FROM tbl_info";

$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
    echo "Name:"." ".$rows['name'];
    echo "<br>";
    echo "<br>";
}
My problem here is, it display all names in the table. The desired output is it must display only one name from the value of the textbox in info.php: (info.php)

Code: Select all

Name: <input type="text" name="txtname">
where txtname is located in another php file.

Thanks in advance.

Re: display specific row from a texbox value

Posted: Thu Feb 21, 2013 4:55 pm
by requinix
Okay: don't use a loop and just call mysql_fetch_array() once.

But which name should it show?

Re: display specific row from a texbox value

Posted: Thu Feb 21, 2013 7:17 pm
by newbie021
I don't get your question there.

But can you please show me some codes on how will I make it not a loop?
I'm sorry but I'm really a newbie in php.

Re: display specific row from a texbox value

Posted: Thu Feb 21, 2013 7:31 pm
by requinix
The "while" thing is a type of loop. Every time the condition you give it is true it will execute whatever it is inside. In your code that means it will execute for every single row that came back from the query.

You don't want it to do that. Loops execute multiple times. You don't want it to execute multiple times. Don't use a loop.

It's mostly a matter of removing code. Take a quick look (because that's all it really takes for this) at the documentation link I gave above and decide what parts of the code you should get rid of.
Hint: you still need the bit of code about mysql_fetch_array().

The other thing to remember is that your query will grab every single name. Again, you don't want that. So now you should be asking yourself "Is there a way to make it only get one row?" Yes there is.

Code: Select all

SELECT name FROM tbl_info LIMIT 1
As for my question, right now the code will show every single name. You don't want it to show all of them. So which one name do you want it to show? Just some random name? The first one in the table, which is what the SQL above will do? The first name where the data matches some criteria or conditions?