11Tami Posted December 15, 2006 Share Posted December 15, 2006 Hi this is just code that pulls items out of a database. Right now it is pulling a row from the field "names." From the table called "users." From a database. How can I get it to pull only a row with a name of "bobby" for instance? I can't seem to find any information online on how to pull a row only with a particular id. Please let me know, thank you very much.<?php $link = mysql_connect('host', 'username', 'password'); $db_selected = mysql_select_db('database', $link); $query = "SELECT names FROM users";$result = mysql_query($query); while ($row = mysql_fetch_array($result)) {print "<p>$row[names]</p>"; } ?> Quote Link to comment Share on other sites More sharing options...
taith Posted December 15, 2006 Share Posted December 15, 2006 for example...[code]<?php $link = mysql_connect('host', 'username', 'password'); $db_selected = mysql_select_db('database', $link); $result = mysql_query("SELECT names FROM users WHERE `names`='bobby'"); while ($row = mysql_fetch_array($result)){ print "<p>$row[names]</p>"; } ?>[/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 15, 2006 Share Posted December 15, 2006 As taith pointed out, you need to be using a WHERE clause on your query. You do need to decide, though, whether or not you want to match something like "Bobby John" or just match exactly "Bobby" in a case like you referred to. Compare the following:[code]<?php// Case insensitive matchmysql_query("SELECT names FROM users WHERE LOWER(names) = 'bobby'");// Also matches names such as "Bobby John"mysql_query("SELECT names FROM users WHERE LOWER(names) LIKE 'bobby%'");?>[/code]Hope this helps. Quote Link to comment Share on other sites More sharing options...
11Tami Posted December 16, 2006 Author Share Posted December 16, 2006 OK I changed it to make it easier I think. I added a table named "name" with a field named "bobby" with a few things in "bobby."How to do I get whats in field "bobby" to be pulled when a user with the name bobby enters his name into a form text field? Please let me know, thank you. Quote Link to comment Share on other sites More sharing options...
taith Posted December 16, 2006 Share Posted December 16, 2006 all you need to do is create a normal html form, with a field named username and then set the fields and thereya go :)[code]<?$link = mysql_connect('host', 'username', 'password'); $db_selected = mysql_select_db('database', $link); $result = mysql_query("SELECT names FROM users WHERE `names`='$_POST[username]' LIMIT 1"); $row = mysql_fetch_array($result);echo "<p>$row[names]</p>";echo "<p>$row[info]</p>";echo "<p>$row[phon]</p>";echo "<p>$row[etc]</p>";?>[/code] Quote Link to comment Share on other sites More sharing options...
IchBin Posted December 16, 2006 Share Posted December 16, 2006 I thought you had to put quotes around array elements and escape to PHP when doing this stuff?Sorry if this is a dumb question, but I just want to make sure I understand the syntax right.echo "<p>", $row['names'], "</p>"; Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted December 16, 2006 Share Posted December 16, 2006 you dont have to do '.$row[name].' if ur echo is using "" like echo "<P>$row['name']</P>";but if u use '' like this echo '<P>'.$row['name'].'</P>'; Quote Link to comment Share on other sites More sharing options...
taith Posted December 16, 2006 Share Posted December 16, 2006 in old versions of php you do need it, however with php 4.2(?)+ it is not necessary. Quote Link to comment Share on other sites More sharing options...
11Tami Posted December 17, 2006 Author Share Posted December 17, 2006 For some reason its saying the fetch string isn't a valid argument. But it might be because I needed to alter this string.$result = mysql_query("SELECT names WHERE `names`='$_POST[username]' LIMIT 1"); I don't know how to say select the items listed in the actual name fields(bobby) etc. from the table called "names." It didn't read like this the way you had it listed. How to adjust for this? Thanks. Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 18, 2006 Share Posted December 18, 2006 [quote author=11Tami link=topic=118738.msg486687#msg486687 date=1166370221]I don't know how to say select the items listed in the actual name fields(bobby) etc. from the table called "names." It didn't read like this the way you had it listed. How to adjust for this? Thanks.[/quote]Remember this pattern when you're writing SQL queries... [b]SELECT [i]columns[/i] FROM [i]tables[/i] WHERE [i]restrictions apply[/i][/b]. So, in your case, something like this should work:[code]<?php$result = mysql_query("SELECT name FROM names WHERE name = '$_POST[username]' LIMIT 1");?>[/code] Quote Link to comment Share on other sites More sharing options...
11Tami Posted December 29, 2006 Author Share Posted December 29, 2006 Post holiday break. You still around obsidian? Still getting this errorWarning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource, for this line $row = mysql_fetch_array($result);So I'll give you everything I have, maybe you can see the error. Heres the php<?php $link = mysql_connect('mysql9etc', 'login', 'password'); $db_selected = mysql_select_db('login', $link); $result = mysql_query("SELECT name FROM names WHERE 'name' = '$_POST[username]' LIMIT 1");$row = mysql_fetch_array($result);echo "<p>$row[name]</p>";?> Here's the form. <form action="test.php" method="post"><input type="text" name="username" size="14" value"username" /><input type="submit" value="submit" /></form>Then I have a table named "names" and have the column "bobby" in it. With "this is in the bobby field" in the bobby field. When I enter "bobby" into the form, I get the error. Should something else be listed in this field like "username" instead?echo "<p>$row[name]</p>";Please get back to me, thanks very much. Quote Link to comment Share on other sites More sharing options...
trq Posted December 29, 2006 Share Posted December 29, 2006 I think your getting tables, fileds and rows mixed up. Your query suggests that you have a table called [i]names[/i], and a field called [i]names[/i]. In this field you have a row bobby in it.You also should use a die() statement to debugg some....[code=php:0]$result = mysql_query("SELECT name FROM names WHERE name = '{$_POST['username']}' LIMIT 1") or die(mysql_error());[/code]Post your database schema if you can. Quote Link to comment Share on other sites More sharing options...
11Tami Posted December 29, 2006 Author Share Posted December 29, 2006 This must be where the problem is then. I have a table called "names." But then it goes directly into the actual names of bobby etc. That way whatever I put into the "bobby" field etc. is what will be pulled. I copied and pasted this right off the table called "names." It shows the database information at the top from mysql then the field informaton underneath of it. For the field type I used "text," instead of varchar."Server: Database: pulldata Table: names Browse Insert etc.Field Type bobby text mandi text Mike text Then under each name I can insert their information. The inserted information is the information I am trying to pull. Thanks much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.