display data from a mysql database

I am new to php programming but have a programming background so I understand pretty much what to do. However, I am probably missing something here. I have an html/php webpage that is supposed to show me data from my membership database Chapters table. The code works fine except that it doesn’t display the data

[code]

Display Data from DB table { border:2px solid red; background-color: #ffc; }

th {
border-bottom: 5px solid #000;
}

td {
border-bottom: 2px solid #666;
}

Display Data from DB

[/code]
[php]<?php
//make the connection
$DB_Server = “daughtershcorg.ipagemysql.com”; //MySQL Server
$DB_Username = “daughtershc”; //MySQL Username
$DB_Password = “holyCross2009$”; //MySQL Password
$DB_DBName = “membership”; //MySQL Database Name
$DB_TBLName = “Chapters”; //MySQL Table Name

//create MySQL connection
$sql = “Select * from $DB_TBLName”;
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die(“Could not connect to MySQL:
” . mysql_error() . “
” . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
or die(“Couldn’t select database:
” . mysql_error(). “
” . mysql_errno());
//execute query
$result = mysql_query($sql)
or die(“Couldn’t execute query:
” . mysql_error(). “
” . mysql_errno());
echo ‘Connected successfully’;[/php]

Everything works to this point and I get ‘Connected successfully’

the rest of the code:
[php]while($data = mysql_fetch_row($result)){

echo("

");
}

?>[/php]

[code]

Chapter Number Chapter Name Church Name City State
$data[ChapterNumber] $data[ChapterName] $data[ChurchName] $data[City] $data[State]
[/code]

Now I get an empty table. I hope someone can help me solve this issue.

Thanks, Sue

Do you have data in your tables to display?

maybe I’ve missed something but I don’t see how you are directing your MySQL query to display the rows from the selected table to your output page. I don’t see any connection between the html page and the php page. Also do you have an input form that you are using to populate the table in your database? If not then you wont have any data to display unless your are dropping the data in manually via sql commands or phpmyadmin.

Sue try this, but before this will work you MUST have data in your database. If you need help doing that let me know.

[php]

Untitled 2
<?php // Make your coding easier and create a db connect script and place it in an includes folder // and just add the following line to all your php scripts that require database connection

//make the connection
$DB_Server = “daughtershcorg.ipagemysql.com”; //MySQL Server
$DB_Username = “daughtershc”; //MySQL Username
$DB_Password = “holyCross2009$”; //MySQL Password
$DB_DBName = “membership”; //MySQL Database Name
$DB_TBLName = “Chapters”; //MySQL Table Name

//create MySQL connection
$sql = “Select * from $DB_TBLName”;
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die(“Could not connect to MySQL:
” . mysql_error() . “
” . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
or die(“Couldn’t select database:
” . mysql_error(). “
” . mysql_errno());
//execute query
$result = mysql_query($sql)
or die(“Couldn’t execute query:
” . mysql_error(). “
” . mysql_errno());

// This is where you select your table in your database you are already connected to.

$sql = “SELECT * FROM chapters”;
$result = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_array($result)){

// Before we close out of PHP, lets define all of our variables so they are easier to remember and work with,
// you can skip this though if you just want to directly reference each row.

$chapternumber= $row[‘ChapterNumber’];
$chaptername= $row[‘ChapterName’];
$churchname = $row[‘ChurchName’];
$city = $row[‘City’];
$state= $row[‘State’];

}
?>

DATABASE OUTPUT

 
<tr><th class="auto-style2">Chapter Number<div style="width: 122px"><?php echo "$chapternumber" ?>
	</div>
	</th><th class="auto-style2">Chapter Name<div style="width: 177px"><?php echo "$chaptername" ?>
	</div>
	</th><th class="auto-style2">Church Name<div style="width: 193px"><?php echo "$churchname" ?>
	</div>
	</th><th class="auto-style2">City<div style="width: 199px"><?php echo "$city" ?>
	</div>
	</th><th class="auto-style2">State<div style="width: 232px"><?php echo "$state" ?>
	</div>
	</th></tr>
[/php]

Disregard the notes in the code above I was in a hurry and forgot to remove them after I made changes to not use and includes folder.

Ok so I created you an example input form and a page to process the form to MySQL. Copy the codes below and make a separate page with the correct page name for each page and upload them all to the same folder and you will have a working input page and and output page from your MySQL database. These are just examples not meant for production!

This is your first page call it insert.html

[code]

Untitled 2 .auto-style1 { text-align: right; } .auto-style2 { text-align: center; } .auto-style3 { font-family: "Rosewood Std Regular"; font-size: xx-large; }

Chapter Membership Input Form

Insert Data Into mySQL Database
Chapter Number :
Chapter Name :
Church Name :
City :
State :
[/code]

This is the second page. Call it insert_ac.php

[php]

Untitled 2 <?php

$host=“daughtershcorg.ipagemysql.com”; // Host name
$username=“daughtershc”; // Mysql username
$password=“holyCross2009$”; // Mysql password
$db_name=“membership”; // Database name
$tbl_name=“Chapters”; // Table name

// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

// Get values from form
$ChapterNumber=$_POST[‘ChapterNumber’];
$ChapterName=$_POST[‘ChapterName’];
$ChurchName=$_POST[‘ChurchName’];
$City=$_POST[‘City’];
$State=$_POST[‘State’];

// Insert data into mysql
$sql=“INSERT INTO $tbl_name(ChapterNumber, ChapterName, ChurchName, City, State)VALUES(’$ChapterNumber’, ‘$ChapterName’, ‘$ChurchName’, ‘$City’, ‘$State’)”;
$result=mysql_query($sql);

// if successfully insert data into database, displays message “Successful”.
if($result){
echo “Successful”;
echo “
”;

// make sure you keep insert_ac.php and insert.html in the same folder if not
// then you will need to change the link below to the correct path of your insert.html file

echo “Back to main page”;
}

else {
echo “ERROR”;
}
?>

<?php // close connection mysql_close(); ?> [/php]

This is your 3rd page. Call it Chapter_output.php

[php]

Untitled 2
<?php // Make your coding easier and create a db connect script and place it in an includes folder // and just add the following line to all your php scripts that require database connection

//make the connection
$DB_Server = “daughtershcorg.ipagemysql.com”; //MySQL Server
$DB_Username = “daughtershc”; //MySQL Username
$DB_Password = “holyCross2009$”; //MySQL Password
$DB_DBName = “membership”; //MySQL Database Name
$DB_TBLName = “Chapters”; //MySQL Table Name

//create MySQL connection
$sql = “Select * from $DB_TBLName”;
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die(“Could not connect to MySQL:
” . mysql_error() . “
” . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
or die(“Couldn’t select database:
” . mysql_error(). “
” . mysql_errno());
//execute query
$result = mysql_query($sql)
or die(“Couldn’t execute query:
” . mysql_error(). “
” . mysql_errno());

// This is where you select your table in your database you are already connected to.

$sql = “SELECT * FROM chapters”;
$result = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_array($result)){

// Before we close out of PHP, lets define all of our variables so they are easier to remember and work with,
// you can skip this though if you just want to directly reference each row.

$chapternumber= $row[‘ChapterNumber’];
$chaptername= $row[‘ChapterName’];
$churchname = $row[‘ChurchName’];
$city = $row[‘City’];
$state= $row[‘State’];

}
?>

DATABASE OUTPUT

 
<tr><th class="auto-style2">Chapter Number<div style="width: 122px"><?php echo "$chapternumber" ?>
	</div>
	</th><th class="auto-style2">Chapter Name<div style="width: 177px"><?php echo "$chaptername" ?>
	</div>
	</th><th class="auto-style2">Church Name<div style="width: 193px"><?php echo "$churchname" ?>
	</div>
	</th><th class="auto-style2">City<div style="width: 199px"><?php echo "$city" ?>
	</div>
	</th><th class="auto-style2">State<div style="width: 232px"><?php echo "$state" ?>
	</div>
	</th></tr>
[/php]

One other thing, I would advise you to change your password on your MySQL database as if the one you have listed in this post is the correct password to access your database then half the world now has access to it! :o

Thank you, vegas, for helping with my code. It is at least pulling data from the database now. Yes, there are records in the database-63 so I did not need the input form at this time. When I need that form, I will probably refer to it. However, I am only getting one record and it is the last one. The while statement does not appear to be working. It is not giving me an array. And I did change my password, thanks.

Try this Sue,

replace this

[php]while($row = mysql_fetch_array($result)){[/php]

With this

[php]while($row = mysql_fetch_assoc($result)){

foreach($row as $key => $val){

echo $key . ": " . $val . “
”;
[/php]

you will need to format the output to your page but this should output everything in your rows

Well I was definitely able to output the data but it came in as a list which seems exactly whatecho $key . ": " . $val . "<BR />"; is telling it to do. So I get a list of label:data down the page.

I tried to format the table to pull the data but all I got was the header row.

Here is my table:

<center>
<h6 class="auto-style1"><p>DATABASE OUTPUT</p></h6>
&nbsp;<table border="1" style="width: 961px">

	<tr><th class="auto-style2">Chapter Number  </th>
		<th class="auto-style2">Chapter Name </th>
		<th class="auto-style2">Church Name </th>
		<th class="auto-style2">City </th>
		<th class="auto-style2">State </th>
	</tr>
	<tr><td class="auto-style2"> <?php echo $row["$chapternumber"] ?> </td>
		<td class="auto-style2"> <?php echo $row["$chaptername"] ?> </td>
		<td class="auto-style2"> <?php echo $row["$Churchname"] ?> </td>
		<td class="auto-style2"> <?php echo $row["$city"] ?> </td>
		<td class="auto-style2"> <?php echo $row["$state"] ?> </td> 
	</tr>

	
        
</table>
        
</center>

Obviously I must have put in the wrong php code ($row) but I tried everything I could think of – $key, $val, and $result. All show the same thing. I am sure I have the html correct but not the php. As I said before I am new to this and am trying to figure out what I have done wrong. Thanks for your help.

I’m working on it!

I removed all this it was no good, continue on below that works good!

Sue don’t mess with the stuff above just use the following script and you will be good, just play around with the formatting and row colors until you find something you like.
make sure you add your database info!

[php]<?php

$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=“membership”; // Database name
$tbl_name=“chapters”; // Table name

// Connect to server and select databse
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

$sql=“SELECT * FROM $tbl_name”;
$result=mysql_query($sql);

// Define $color=1
$color=“1”;
// Adjust the width, cell padding and cell spacing here //

echo ‘

’;
while($rows=mysql_fetch_array($result)){

// adjust the 1st row color here //

// If $color==1 table row color = #CCCFFF
if($color==1){
echo "

";

// Set $color==2, for switching to other color
$color=“2”;
}

// adjust the 2nd row color here//

// When $color not equal 1, use this table row color
else {
echo "

";

// Set $color back to 1
$color=“1”;
}

}
echo ‘

".$rows['ChapterNumber']." ".$rows['ChapterName']." ".$rows['ChurchName']." ".$rows['City']." ".$rows['State']."
".$rows['ChapterNumber']." ".$rows['ChapterName']." ".$rows['ChurchName']." ".$rows['City']." ".$rows['State']."
’;
mysql_close();
?>
[/php]

Thank you, vegas!

The simpler code works great and is more understandable. I think the trick was this code
[php]

".$rows[‘ChapterNumber’]."".$rows[‘ChapterName’]."".$rows[‘ChurchName’]."".$rows[‘City’]."".$rows[‘State’]."/php]

I experimented a tad and was able to add a header row.

It has a small issue in the display but it is something I can live with until I have some time to play with it. I am going to mark this issue “solved”.

Sponsor our Newsletter | Privacy Policy | Terms of Service