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
1

Popularity: 2% [?]

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Filed Under: Web Programming

Tags:

RSSComments (6)

Leave a Reply | Trackback URL

  1. I feel you are too good to write Genius!Thanks for posting, maybe we can see more on this.

  2. thanks for this fantastic Article. Nice subject to write about on my site. I may set a bookmark to your page.

  3. This is all very new to me and this article really opened my eyes.Many thanks for sharing with us your wisdom.

  4. Pat Mazuera says:

    Book-marked your blog. Appreciate expressing. Absolutely well worth the time far from our research.

  5. Megan says:

    Thank you so much for posting all of the awesome information! I am looking forward to reading more blogs.

  6. Emily says:

    This informal post assited me very much! Saved your blog, very interesting topics everywhere that I see here! I really appreciate the info, thank you.

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

The tutorials and scripts found on bgallz.org are for training purposes only, use to your discretion.