Jump to content

Archived

This topic is now archived and is closed to further replies.

avo

select data from database

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.

Share this post


Link to post
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 .

Share this post


Link to post
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]

Share this post


Link to post
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.


Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Is there a number printed before the word 'buster' ? Which number?
I think that there is nothing stored in the other columns of your table.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.