display specific row from a texbox value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newbie021
Forum Newbie
Posts: 2
Joined: Thu Feb 21, 2013 4:03 pm

display specific row from a texbox value

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: display specific row from a texbox value

Post by requinix »

Okay: don't use a loop and just call mysql_fetch_array() once.

But which name should it show?
newbie021
Forum Newbie
Posts: 2
Joined: Thu Feb 21, 2013 4:03 pm

Re: display specific row from a texbox value

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: display specific row from a texbox value

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