Jump to content

Need a small amount of help from a smart php person please.


11Tami

Recommended Posts

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>";
}

?>
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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 match
mysql_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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

  • 2 weeks later...
Post holiday break. You still around obsidian? Still getting this error
Warning: 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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.             
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.