Home » Code

Mysql Rows from a Table

If you have a mysql database you can connect to, you can create tables, rows, columns, and do many other mysql functions through PHP. Let’s say we have a mysql database named “my_database”. We will connect to it using the php function “mysql_connect()” and draw information from the tables.

Config.php:

<?php
// Config.php

$username = "my_username";
$password = "my_password";
$db_name = "my_database";

$connect = mysql_connect("localhost",$username,$password) or die('Could not connect to database.');
$select_db = mysql_select_db($db_name) or die('Unable to select specified database.');

?>

This is usually placed in a config.php file and is included or required on other pages that execute mysql queries.

Now let’s move on to our query file. Let’s say we have a table named “table1.” We will insert the config file to have the connection to the database, and then select columns from the table1.

<?php
// Query.php

require_once('config.php');

$sql = mysql_query("SELECT col1,col2,col3 FROM table1") or die(mysql_error());
$count = mysql_num_rows($sql);

// check to see if there are any rows returned.
if($count > 0){
// we have some!

// this is used to grab all rows in the table.
while($row = mysql_fetch_array($sql)){
echo 'Col1: '.$row['col1'].'<br/>';
echo 'Col2: '.$row['col2'].'<br/>';
echo 'Col3: '.$row['col3'].'<br/><br/>';
}
} else {
// nothing found.
echo 'No rows found.';
}

?>

Now, this will return simple HTML of each row with col1: (its value), col2: (its value), etc. We can get fancy and use a table to organize our information better. Here is an example:

if($count > 0){
// we have some, make the table!
echo '<table width="100%" align="center" border="1" cellpadding="3" cellspacing="0">';
echo '<tr><td align="left"><strong>Col1</strong></td><td align="left"><strong>Col2</strong></td><td align="left"><strong>Col3</strong></td></tr>';

// this is used to grab all rows in the table.
while($row = mysql_fetch_array($sql)){
echo '<tr>';
echo '<td align="left"> '.$row['col1'].'</td>';
echo '<td align="left"> '.$row['col2'].'</td>';
echo '<td align="left"> '.$row['col3'].'</td>';
echo '</tr>';
}
} else {
// nothing found.
echo 'No rows found.';
}

This will display something like this:

Col1 Col2 Col3
Row1: value1 Row1: value2 Row1: value3
Row2: value1 Row2: value2 Row2: value3
Row3: value1 Row3: value2 Row3: value3
0

Popularity: 2% [?]

Share/Bookmark this!

6 Comments

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on November 2, 2009 at 10:50 AM and filed under Code category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors