Jump to content

select data from database


avo

Recommended Posts

Hi All

Is it possible for a little help

all im trying to do is select data from my database and display it i want to select all the data from a colum what is named username if it as a name in there called buster to read all that colum all this is connected to a form button so when this button is pressed it will run through the bellow code

my code
[code]
<?

include "db.php";

if($_POST['subEdit']){
    mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname);
    $sql = mysql_query("SELECT * FROM memlist WHERE username=buster") ;
    }
    while($result = mysql_fetch_array($sql))
    {
    $username =  "" .$result["username"];
    $name =  "" .$result["name"];
    $lname =  "" .$result["lname"];
    $waddress =  "" .$result["waddress"];
    $pnumber =  "" .$result["pnumber"];
    $mnumber =  "" .$result["mnumber"];
    $email =  "" .$result["email"];
    $ainfo =  "" .$result["ainfo"];
    $dateinfo =  "" .$result["dateinfo"];
}

?>

HTML code....

<? echo $username; ?>
[/code]

Thanks in advance.
Link to comment
Share on other sites

Hi sorry

That would help wouldnt it.

it dont seem to gather the information then allow me output this with

[code]

<? echo $username;
echo $email;

?>
[/code]
ect . i wanted to output all the fields in the collum with the name buster

thanks again hope this helps .
Link to comment
Share on other sites

This code:
[code]

<? echo $username;
echo $email;

?>
[/code]
does not print anything in your browser window?

You could include it in the while loop so that you print all rows if there is more than one "buster" in the table.
[code]
<?

include "db.php";

if($_POST['subEdit']){
    mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname);
    $sql = mysql_query("SELECT * FROM memlist WHERE username=buster");
    }
    while($result = mysql_fetch_array($sql))
    {
    $username =  "" .$result["username"];
    $name =  "" .$result["name"];
    $lname =  "" .$result["lname"];
    $waddress =  "" .$result["waddress"];
    $pnumber =  "" .$result["pnumber"];
    $mnumber =  "" .$result["mnumber"];
    $email =  "" .$result["email"];
    $ainfo =  "" .$result["ainfo"];
    $dateinfo =  "" .$result["dateinfo"];
    echo $username . $email . '<br />';
}

?>
[/code]

May be adding single quotes can help:
[code]
$sql = mysql_query("SELECT * FROM memlist WHERE username='buster'");
[/code]

Also, are you sure that a member with username = 'buster' exists in the table?
Tip: echo the number of rows returned by the query.
[code]
echo mysql_num_rows($sql) . '<br />';
[/code]
Link to comment
Share on other sites

Hi thankyou


i've got 5 rows and 9 colums in my db table
what the bellow code is doing and working is reading all the db then im outputing the array in an echo prop inside html

ok if i use this code all works fine (or as expected outputs last row and collum of db)

[code]

if($_POST['subEdit']){
    mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname);
    $sql = mysql_query("SELECT * FROM memlist ") ;
    }
    while($result = mysql_fetch_array($sql))
    {
    $username =  "" .$result["username"];
    $name =  "" .$result["name"];
    $lname =  "" .$result["lname"];
    $waddress =  "" .$result["waddress"];
    $pnumber =  "" .$result["pnumber"];
    $mnumber =  "" .$result["mnumber"];
    $email =  "" .$result["email"];
    $ainfo =  "" .$result["ainfo"];
    $dateinfo =  "" .$result["dateinfo"];

}

[/code]

PHP Inside HTML CODE....

[code]
<?
        echo $username;
    echo $name;
    echo $lname;
    echo $waddress;
    echo $pnumber;
    echo $mnumber;
    echo $email;
    echo $ainfo;
    echo $dateinfo;
?>
[/code]

But with this code bellow what i was hopeing for is that it would look at the row with the name buster in it then select all the colum then output that colum using


[code]
<?
        echo $username;
    echo $name;
    echo $lname;
    echo $waddress;
    echo $pnumber;
    echo $mnumber;
    echo $email;
    echo $ainfo;
    echo $dateinfo;
?>
[/code]

the code i tryed to do this with was:

[code]
if($_POST['subEdit']){
    mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname);
    $sql = mysql_query("SELECT * FROM memlist WHERE username=buster") ;
    }
    while($result = mysql_fetch_array($sql))
    {
    $username =  "" .$result["username"];
    $name =  "" .$result["name"];
    $lname =  "" .$result["lname"];
    $waddress =  "" .$result["waddress"];
    $pnumber =  "" .$result["pnumber"];
    $mnumber =  "" .$result["mnumber"];
    $email =  "" .$result["email"];
    $ainfo =  "" .$result["ainfo"];
    $dateinfo =  "" .$result["dateinfo"];
[/code]

username is the name of the first colum the second colum name is lname and so on so i hoped it would look at the username colum find buster then store in an arry all that colum then allow me to output it with the above echo $username ect ect ../ but this is not displaying any output for me

any hellp appriciated.


Link to comment
Share on other sites

Is this doing what you want:
[code]
<?

include "db.php";

if($_POST['subEdit']){
    mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname);
    $sql = mysql_query("SELECT * FROM memlist WHERE username='buster'");
    }
    echo mysql_num_rows($sql);
    while($result = mysql_fetch_array($sql))
    {
    echo  "" .$result["username"] . " " .$result["name"] .  " " . $result["lname"] .  " " .$result["waddress"] .  " " .$result["pnumber"] .  " " .$result["mnumber"] .  " " .$result["email"] .  " " .$result["ainfo"] .  "" .$result["dateinfo"] . '<br />';
}

?>
[/code]
Link to comment
Share on other sites

Hi

The code above echos out buster what is stored in the username but i also want to echo out

email
lname
waddress

ect ect...

which is stored on the same line in the db as the username = buster


hope this helps

thank you.
Link to comment
Share on other sites

HI thanks


The number was 1

but i have information inside in my db table
1st colum = username
2nd colum = name
3rd colum = lname


then there is 5 rows of diffrent information,

when buster is found in the username colum i also want it to get lname , name ect ect if i go into mysql the information i want to get i read username on the top down the row see buster then its all the right side iformation i want to be able to store in an array.


thanks you.
Link to comment
Share on other sites

For the moment I gues that you have a row in your table where the column username has the value 'buster. And I think that all the other columns for that row are empty, they don't have a value.
Check this first.

If you want to store all column values into an array you can try this:
[code]
while($result = mysql_fetch_array($sql))
    {
    $myarray = array();
    $myarray[] =  $result["username"];
    $myarray[] =  $result["name"];
    $myarray[] =  $result["lname"];
    $myarray[] =  $result["waddress"];
    $myarray[] =  $result["pnumber"];
    $myarray[] =  $result["mnumber"];
    $myarray[] =  $result["email"];
    $myarray[] =  $result["ainfo"];
    $myarray[] =  $result["dateinfo"];
}
[/code]
But remember that $result is already an array with all your columns in it.
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.