Jump to content

Display User Info (shipping address & user name etc)?


popeye IV

Recommended Posts

Hi Christian, yes you can.

 

Are you using a mysql database to store each users info?

If you are, then retrieving the results are fairly simple.

 

Lets say you have a table called users and the users info is stored in that table.

You would typically retrieve things this way: (this tutorial is not mine, it was taken from www.webmasterworld.com to give you an example)

 

1. connect to mysql

2. select the db

3. build our mysql query from form values

4. retrieve the info from mysql

5. display, if it is there

 

required reading

PHP mysql Functions - all the functions below are listed here

PHP Predefined Variables - for looking at $_POST and $_GET arrays necessary for accessing data sent from a form

 

1. connect to mysql

resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

 

this function returns a link identifier to mysql that we can then use to communicate with our database. If this function bombs, it's all over because we can't talk to the db.

 

$host = "localhost";

$user = "mysqlusername";

$pass = "mysqlpassword";

 

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");

 

so we are going to connect to mysql and store the returned link identifier in the variable $connection by passing the host (in this case localhost), username and password, which are stored in variables, to the mysql_connect function. The last part specifies that is it fails stop the script and show me the error number and text description of the error returned from mysql.

 

I use both the error number and text for my die statement. In case the error description is vague I can always search it on my search engine of choice.

 

2. select the db

 

assuming we are now connected to mysql we need to tell it what database to use.

 

bool mysql_select_db ( string database_name [, resource link_identifier])

 

$dbname = "ourdatabase";

mysql_select_db($dbname);

 

tell mysql to use the database name stored in $dbname. You will notice that the link_identifier is an optional parameter. The mysql functions always assume you mean for them to use the last opened identifier. You only need to pass the link_identifier if you have more than one connection open for any given script.

 

3. build our mysql query from form values

 

As far as queries go you will have to familiarize yourself with mysql but we are doing something simple here so you can go to http://www.mysql.com and read to your heart's content later.

 

I always build my queries before I pass them to the mysql function. It gives me more options for debugging bad queries. We now need to do a select query using the two fields that were entered into our form.

 

Our form method was set to post so we will access the values using the "name" of our input fields in the $_POST array.

 

$sql = "select * from ourtablename where id=" . $_POST['manufacturer'] . " and manufacturer='" . $_POST['manufacturer'] . "'";

echo $sql;

 

since we are trying to learn something here I use echo to show the contents of the variable. Seeing it in full will help you understand how it was built. The period between some parts of the string is the concatenation operator. We are essentially gluing all the parts together into the variable to create a long string as you will see when you echo the contents of the variable.

 

4. retrieve the info from mysql

 

We need to fire the query at mysql.

 

resource mysql_query ( string query [, resource link_identifier])

 

this returns a resource so we need to have it go to a variable.

 

$query = mysql_query($sql);

 

5. display, if it is there

 

now we need to retrieve the information from the resource. There may, or may not, be any data there. How this part is handled really depends on how the search is built. You can limit the search to only search existing manufacturers. For the sake of simplicity we will assume that we found the manufacturer we were looking for.

 

array mysql_fetch_array ( resource result [, int result_type])

 

while ($row = mysql_fetch_array($query)) {

echo "<p>",$row['id'],": ",$row['manufacturer'];

}

 

So this is a little more difficult. A while loop will execute as long as the statement is true. Therefore, as long as there is a row available it will do whatever is between the { }. In this case we may have returned more than one row. If we return 5 rows then it will loop 5 times and show 5 ids and manufacturers. Each time we call mysql_fetch_array it will grab one row and then we will process that line and go back to the top and do it again for the next row.

 

$row will be an array that is why we need to access the values in the array with $row['id']. We are looking for the value from the id column or from the manufacturer column. We then just echo them to the browser. You will probably want to format them better than I did.

 

You also can see that for echo I used commas between the different parts not the dot. You can use the dot but then echo has to put them all together before it can spit them out. This way it just spits the pieces out as it goes and is much faster.

 

So that leaves us with this code for our script

 

<?

$host = "localhost";

$user = "mysqlusername";

$pass = "mysqlpassword";

$dbname = "ourdatabase";

 

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");

mysql_select_db($dbname);

 

$sql = "select * from ourtablename where id=" . $_POST['manufacturer'] . " and manufacturer='" . $_POST['manufacturer'] . "'";

echo $sql;

 

$query = mysql_query($sql);

 

while ($row = mysql_fetch_array($query)) {

echo "<p>",$row['id'],": ",$row['manufacturer'];

}

 

?>

 

 

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.