hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : how to do this with php and mysql?
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

how to do this with php and mysql?

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 08-30-2002, 11:42 AM
matt2kjones matt2kjones is offline
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

Reply With Quote


Sponsored Links
  #2  
Old 08-30-2002, 11:51 AM
Rich2k Rich2k is offline
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.

Reply With Quote
  #3  
Old 08-30-2002, 12:06 PM
matt2kjones matt2kjones is offline
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.

Reply With Quote
Sponsored Links
  #4  
Old 08-30-2002, 03:32 PM
Rich2k Rich2k is offline
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.

Reply With Quote
  #5  
Old 08-30-2002, 08:34 PM
Duey Duey is offline
Newbie
 
Join Date: Aug 2002
Location: New Zealand
Posts: 8
these some good tutorials out there just do a search try www.spoono.com

Reply With Quote
  #6  
Old 09-01-2002, 07:06 PM
kailis kailis is offline
New Member
 
Join Date: Sep 2002
Location: lithuania
Posts: 4
script

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";
}
?>

Reply With Quote
  #7  
Old 09-01-2002, 07:20 PM
kailis kailis is offline
New Member
 
Join Date: Sep 2002
Location: lithuania
Posts: 4
error :))

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";
}
?>

Reply With Quote
  #8  
Old 09-01-2002, 08:11 PM
jtrovato jtrovato is offline
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.

Reply With Quote
  #9  
Old 09-04-2002, 09:26 AM
matt2kjones matt2kjones is offline
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

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Parallels Now Offering Licenses for CloudLinux OS Web Hosting News 2011-10-26 17:29:25
Web Hosting Sales and Promos Roundup - October 7, 2011 Web Hosting News 2011-10-07 19:56:18
Web Hosting Software Firm CloudLinux Releases CloudLinux OS Version 6.1 Web Hosting News 2011-09-13 14:08:49
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?