View Full Version : Database
Weatherdemon
6-20-03, 03:47 PM
I am completely new to SQL and PHP but I am wanting to get this set up.
I got MySQL working and a database created. I got phpadmin installed and set up several tables.
Now, the first of many questions I am sure I will have.
What is the best way to get data inserted into these tables?
Atomic-Design
6-20-03, 05:14 PM
Script wise, the best way to insert data through PHP is:
mysql_query("INSERT INTO tablename (row1, row2, row3) values ('value1','value2','value3')";
You can use PHPMyAdmin to manually enter info, just like MySQL-Front or Mascon.
particleman
6-21-03, 01:13 PM
A word of caution: use addslashes() when sending text and stripslashes() when retrieving it. See this post for more details: http://forum.powweb.com/showthread.php?s=&threadid=21865
good luck.
pm
Atomic-Design
6-21-03, 09:39 PM
Good call. You can also use magic quotes by turning them on in your php config file.
Weatherdemon
6-23-03, 10:49 AM
You can use PHPMyAdmin to manually enter info, just like MySQL-Front or Mascon
I can't find in PHPMyAdmin where to do this and MYSQL-Front says the feature isn't enabled yet. It says, "Editing tables with spaces in tablenames does not funstion yet."
Nevermind, Macon is working for me.
Atomic-Design
6-23-03, 04:05 PM
Originally posted by Weatherdemon
I can't find in PHPMyAdmin where to do this and MYSQL-Front says the feature isn't enabled yet. It says, "Editing tables with spaces in tablenames does not funstion yet."
Nevermind, Macon is working for me.
MySQL-Front is a lot newer, graphical, and powerful then Mascon. I must say, I use Mascon 95% of the time. The layout is just so original and worthy once you get used to it!
(in MySQL-Front, just find your table, click on the "data" tab, and u can manually enter data into the fields just like in Mascon. Basically the same thing (whereas you have to hit the checkmark to save the changes)
Weatherdemon
6-23-03, 04:26 PM
OK, Thanks!
Got the database, got the tables, got the data!
Now I need to learn to pull the table and data into a web page.
Atomic-Design
6-23-03, 04:29 PM
Try this:
<!-- Start the custom looping from MySQL // -->
<?
$result = mysql_query("SELECT * FROM amw_news");
while ( $a_row = mysql_fetch_assoc($result) ) {
echo '<table width="425" height="43" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
<tr>
<td height="21" bordercolor="#8A0000" bgcolor="#CCCCCC"><strong><font color="#000000" face="Times New Roman, Times, serif">';
echo $a_row['Subject'];
echo ' </font></strong></td>
</tr>
<tr>
<td bordercolor="#FFFFFF" bgcolor="#F4F2AA">
<font face="Geneva, Arial, Helvetica, san-serif">';
echo $a_row['Message'];
echo '</font></td>
</tr>
</table>
<br>';
}
?>
<!-- End of custom looping // -->
Basically after you run the loop, you can find the field by printing $a_row['TheFieldName'];
Weatherdemon
6-23-03, 04:54 PM
I have a database named TUL with several tables football, basketball, etc. Each table has multiple columns, date, opponent, time, etc. with muliple rows of data for each column.
That would be:
DB: TUL
Table: Football Schedule with certain columns indexed
columns and rows
I want to import the Football schedule into a web page. Once ont he web page I would to be able to click on "Results" or other column names and have the table sorted by results. "Results" is an indexed column.
(I didn't understand what the loop thing did.)
Atomic-Design
6-23-03, 05:03 PM
It's fairly simple to implement the sorting, not by PHP, but by query. Here is a foundation:
$username = "Your Username";
$password = "Your Password";
$db = "Your Database Name";
$host = "mysql02.powweb.com";
$link = mysql_connect($host,$username,$password);
if (!$link)
die("Could not connect to database");
mysql_select_db($db,$link)
or die("Database couldn't get selected");
echo "<table>";
$result = mysql_query("SELECT * FROM tablename");
while ( $a_row = mysql_fetch_assoc($result) ) {
echo "<tr><td>Database Result</td><td>2nd Result</td></tr>";
echo "<tr><td>";
echo $a_row['Field'];
echo "</td></tr>";
}
echo "</table>";
?>
Would help more, but I'm running LATE!
Weatherdemon
6-24-03, 10:07 AM
I put that in a PHP file and it just returned the text of the file.
Sorry, but this is very new to me.
HalfaBee
6-24-03, 10:41 AM
Atomic forgot the opening tag.
Put <?php before the script.
HalfaBee
Weatherdemon
6-24-03, 01:38 PM
Now I get a blank page.
HalfaBee
6-24-03, 05:57 PM
More than likely no results were returned.
You should add this to the mysql_query line, just in case of SQL mistakes.
$result = mysql_query("SELECT * FROM tablename") or die( 'Invalid SQL '.mysql_error() );
if( mysql_num_rows($result)==0 )
echo 'No results';
HalfaBee
Atomic-Design
6-24-03, 06:37 PM
A blank page means it didn't loop, meaning no results were found. So Halfa was correct.
Weatherdemon
6-24-03, 09:41 PM
Thanks, I got:
Database Result 2nd Result
August
Database Result 2nd Result
September
Database Result 2nd Result
September
Database Result 2nd Result
September
Database Result 2nd Result
October
Database Result 2nd Result
October
Database Result 2nd Result
October
Database Result 2nd Result
October
Database Result 2nd Result
November
Database Result 2nd Result
November
Database Result 2nd Result
November
Database Result 2nd Result
November
Slowly but surely!
How do I get this info to pull up in a web page where I want it?
Atomic-Design
6-24-03, 10:27 PM
Here is modified code for your needs. Try copy and pasting it where you need it.
<?php
$username = "Your Username";
$password = "Your Password";
$db = "Your Database Name";
$host = "mysql02.powweb.com";
$link = mysql_connect($host,$username,$password);
if (!$link)
die("Could not connect to database");
mysql_select_db($db,$link)
or die("Database couldn't get selected");
echo "<table>";
$result = mysql_query("SELECT * FROM tablename") or die( 'Invalid SQL '.mysql_error() );
if( mysql_num_rows($result) == 0 ) {
echo 'No results';
die; // Kills script
}
echo "<tr><td><b>Month</b></td><td><b>Birthdays</b></td></tr>";
while ( $a_row = mysql_fetch_assoc($result) ) {
echo "<tr><td>";
echo $a_row['Month'];
echo "</td><td>";
echo "$a_row['Birthdays'];
echo "</td></tr>";
}
echo "</table>";
?>
Let's say you want to generate a table that has the month then the number of birthdays next to it. HTML would be:
<table>
<tr>
<td><b>Month</b></td>
<td><b>Birthdays</b></td>
</tr>
<tr>
<td>December</td>
<td>15</td>
</tr>
etc...
See how the first row is just static information? This is found in the PHP code here:
$result = mysql_query("SELECT * FROM tablename");
echo "<tr><td><b>Month</b></td><td><b>Birthdays</b></td></tr>";
That sets up the basis. Then it loops through your data and puts it underneath those entries.
You can modify this for your needs just by that basic understanding.
(btw, this part is the looping)
while ( $a_row = mysql_fetch_assoc($result) ) {
echo "<tr><td>";
echo $a_row['Month'];
echo "</td><td>";
echo "$a_row['Birthdays'];
echo "</td></tr>";
}
Weatherdemon
6-25-03, 10:54 AM
Man, I will owe eon once I get this figured out!
Anyway, here is what I get with that code and my parameters.
Some of the data isn't falling under the right columns. I know the line that probably has the problem but I don't know what the problem is.
Here is the code I used:
echo "<tr><td><b>Month</b></td><td><b>Date</b></td><td><b>Opponent</b></td><td><b>Location</b></td><td><b>Time</b></td><td><b>Score</b></td><td><b>W/L</b></td><td><b>Broadcast</b></td></tr>";
while ( $a_row = mysql_fetch_assoc($result) ) {
echo "<tr><td>";
echo $a_row['GameMonth'];
echo "</td><td>";
echo $a_row['GameDate'];
echo "</td></tr>";
echo $a_row['GameOpponent'];
echo "</td></tr>";
echo $a_row['GameLocation'];
echo "</td></tr>";
echo $a_row['GameTime'];
echo "</td></tr>";
echo $a_row['GameScore'];
echo "</td></tr>";
echo $a_row['GameWinLoss'];
echo "</td></tr>";
echo $a_row['GameBroadcast'];
echo "</td></tr>";
Here is what it creates:
http://www.tulsagoldenhurricane.com/ftballtest.php
HalfaBee
6-25-03, 06:00 PM
On the lines where you have </td></tr>, you need to remove the </tr>, except for the last one.
HalfaBee
Weatherdemon
6-25-03, 10:28 PM
Sweet! Got it!
Baby steps!!!
OK, now the data appears in a table like format on a web page.
How do I set it so it goes into an actual table like this one,
http://www.tulsagoldenhurricane.com/tuftballsch.htm
where you can click on Date, Opponent, etc and have the schedule organize it's self based on the Date, opponent name, or Win or loss?
As I tried to say in a previous post, I am going to owe you all one! :o)
vBulletin v3.6.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.