Jump to content

[SOLVED] Database reading..


czukoman20

Recommended Posts

I have a mysql database named userdata setup. like this

 

Database = userdata

Under userdata are the columns

buddy and username

 

what i want to do is make a page that has a textbox with a button on it that says view data.

when you type one of the usernames in the textbox then you would hit view data.

 

when view data is hit. it will search the database for every row in the column username that = textbox

then display all of the rows it found

 

I know how to connect to the database.. i just need help searching the database and displaying the data

 

thanks

Link to comment
Share on other sites

Pretty basic sql statement like hvle said, then to expand on it:

 

<?php
    if (isset($_POST['submit']))
    {
        $txt_box = $_POST['txt_box'];

        $sql = "SELECT username FROM userdata WHERE username = '" . $txt_box . "' ORDER BY username ASC";
        $query = mysql_query($sql)
            OR DIE(mysql_error());

        while ($row = mysql_fetch_assoc($query))
        {
                $usr_name = $row['username'];
                echo $usr_name . '<br />';
        }
    }
    else
    {
?>

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Basic</title>
    </head>
    <body>
        <form name="search" method="POST">
            <input name="txt_box" type="text" /><br />
            <input name="submit" type="submit" value="Find User" />
        </form>
    </body>
</html>

<?php
}
?>

 

Of course you will also want to do your input validation to make sure what they enter is what you expect (a username)

Link to comment
Share on other sites

Pretty basic sql statement like hvle said, then to expand on it:

Code:

<?php
    if (isset($_POST['submit']))
    {
        $txt_box = $_POST['txt_box'];

        $sql = "SELECT username2 FROM userdata WHERE username2 = '" . $txt_box . "' ORDER BY username2 ASC";
        $query = mysql_query($sql)
            OR DIE(mysql_error());

        while ($row = mysql_fetch_assoc($query))
        {
                $usr_name = $row['username2'];
                $usr_bud = $row['buddy];
                echo $usr_name . $usr_con'<br />';
        }
    }
    else
    {
?>

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Basic</title>
    </head>
    <body>
        <form name="search" method="POST">
            <input name="txt_box" type="text" /><br />
            <input name="submit" type="submit" value="Find User" />
        </form>
    </body>
</html>

<?php
}
?>

 

 

ok well now the issue is. when i give the name of the username2. it finds it just right.. but doesnt look at the other column (buddy) 

 

 

so the username is guest and his buddies are buddy1 buddy2

when i type guest in the box

 

all i see is

 

guest

guest

 

when its supposed to say

 

guest buddy1

guest buddy2

 

 

 

 

Link to comment
Share on other sites

Also, your going to get an error with:

echo $usr_name . $usr_con'<br />'; //improper use of the concatenation operater

 

See:

//should be
echo $usr_name . ' ' . $usr_con . '<br />';

//however, in this case it is just easier to enclose with double quotes
echo "$user_name $usr_con<br/>";

 

By the way, where is $usr_con coming from?

 

 

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.