
08-30-2002, 11:42 AM
|
|
Web Hosting Master
|
|
Join Date: Jun 2002
Location: United Kingdom
Posts: 1,236
|
|
how to do this with php and mysql?
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
|

08-30-2002, 11:51 AM
|
|
Web Hosting Master
|
|
Join Date: May 2002
Location: UK
Posts: 2,994
|
|
Very simply, if you want to loop through ALL rows to display news2
PHP Code:
$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.
|

08-30-2002, 12:06 PM
|
|
Web Hosting Master
|
|
Join Date: Jun 2002
Location: United Kingdom
Posts: 1,236
|
|
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.
|

08-30-2002, 03:32 PM
|
|
Web Hosting Master
|
|
Join Date: May 2002
Location: UK
Posts: 2,994
|
|
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.
|

08-30-2002, 08:34 PM
|
|
Newbie
|
|
Join Date: Aug 2002
Location: New Zealand
Posts: 8
|
|
these some good tutorials out there just do a search try www.spoono.com
|

09-01-2002, 07:06 PM
|
|
New Member
|
|
Join Date: Sep 2002
Location: lithuania
Posts: 4
|
|
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";
}
?>
|

09-01-2002, 07:20 PM
|
|
New Member
|
|
Join Date: Sep 2002
Location: lithuania
Posts: 4
|
|
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";
}
?>
|

09-01-2002, 08:11 PM
|
|
Aspiring Evangelist
|
|
Join Date: Aug 2002
Location: Long Island
Posts: 427
|
|
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
__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.
|

09-04-2002, 09:26 AM
|
|
Web Hosting Master
|
|
Join Date: Jun 2002
Location: United Kingdom
Posts: 1,236
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|