matt2kjones
08-30-2002, 11:42 AM
OK i have a mysql database called 'tdd'
inside that database i have a table called 'news'
and within that table i have four fields called:
news1
news2
news3
and news4
Say, for instance, my username and password for mysql were simply 'username' and 'password'
and the database was running on localhost
what would be the php code to display the info within say, for example, news2
Thanx allot
Matt
Rich2k
08-30-2002, 11:51 AM
Very simply, if you want to loop through ALL rows to display news2
$cn = mysql_connect('localhost','username','password');
$db = mysql_select_db('tdd', $cn);
$result = mysql_query ("SELECT * FROM news");
if ($row = mysql_fetch_array($result)) {
do {
echo $row["news2"];
} while($row = mysql_fetch_array($result));
}
Note: I have not included any form of error checking in this code... simply the code to select and display all news2 variables.
matt2kjones
08-30-2002, 12:06 PM
ok and how would i go about storing things into a database
say all the settings were the same
and i have a variable called $test_var which contains the text 'hello'
how would you store what is in that variable into the field 'news2'
Thanx allot
Matt.
Rich2k
08-30-2002, 03:32 PM
I could explain it all but it would probably be quicker for you to digest the content on the pages at php.net http://www.php.net/manual/en/ref.mysql.php
The discussion notes at the bottom are very good at giving sample code.
these some good tutorials out there just do a search try www.spoono.com
kailis
09-01-2002, 07:06 PM
Very simply.
<?
$cn = mysql_connect('localhost','username','password');
$db = mysql_select_db('tdd', $cn);
$result = mysql_query ("SELECT * FROM news");
while ($row = mysql_fetch_assoc ($result))
{
$text = ["news2"];
echo "$text";
}
?>
kailis
09-01-2002, 07:20 PM
error!!! now is okay:)
<?
$cn = mysql_connect('localhost','username','password');
$db = mysql_select_db('tdd', $cn);
$result = mysql_query ("SELECT * FROM news");
while ($row = mysql_fetch_assoc ($result))
{
$text = $row["news2"];
echo "$text";
}
?>
jtrovato
09-01-2002, 08:11 PM
Matt,
What are you trying to do? They are many different ways of doing this.
Do these fields contain data already that you want to over write?
Do you want to add new news whenever you query the database?
I like building my database tables with a key field. For me it makes editing the database easier based on this unique key field. If you want to just store 4 basic fields and that’s all you need then the following might be a good way to update the fields
<?
// **********************
// Reading from the database
// **********************
$connect = mysql_connect (‘localhost’,’username’,’password’);
$db = mysql_select_db (“tdd”,$connect);
$result = mysql_query (“SELECT * FROM news”);
$row = mysql_fetch_object ($result);
$test_var1 = $row->news1;
$test_var2 = $row->news2;
$test_var3 = $row->news3;
$test_var4 = $row->news4;
// **********************
// Updating the database
// **********************
$connect = mysql_connect (‘localhost’,’username’,’password’);
$db = mysql_select_db (“tdd”,$connect);
$result = mysql_query (“UPDATE news SET news1= \’”.$test_var1.”\', news2 =”\’”.$test_var2.”\' , news3 = \’”.$test_var3.”\', news4= \’”.$test_var4.”\' “ );
?>
Good luck, if you have any more questions feel free to ask
John
matt2kjones
09-04-2002, 09:26 AM
ok thanx everyone
this is what i hope to achive
i have a mysql database, and a site that needs content updating constantly
i want my site to display data from the first 6 rows, onto the site.
Then have a page a page which is password protected, where basically i just have a text box where i can type in a new row of data, hit submit, and it displays that on the site., then the rest move down
is this hard?
Thanx again
Matt