Click to See Complete Forum and Search --> : Any Tutorials on using PHP with MySQL
Conor
03-03-2004, 03:07 PM
im starting work on a new site and im looking to do something where i have a form that writes to a MYsql database then on my main page ill retrieve that from the database for my news content. Is there anywhere i can learn how to work with the database to do what i want. Like what type of table i would have to create to have it like auto increment so i can store many news things or maybe one of you can help me thank you.
Bigjohn
03-03-2004, 03:24 PM
Originally posted by RefreshF5
im starting work on a new site and im looking to do something where i have a form that writes to a MYsql database then on my main page ill retrieve that from the database for my news content. Is there anywhere i can learn how to work with the database to do what i want. Like what type of table i would have to create to have it like auto increment so i can store many news things or maybe one of you can help me thank you.
Autoincrement tables are easy to make. Does your host provide phpMyAdmin? You can use that to create your database and the tables that will store your data.
look at www.phpfreaks.com for tutorials.
John
Conor
03-03-2004, 03:27 PM
yes i have phpmyadmin. Also is it hard to retrieve the information from the database and have a form write to the db.
Bigjohn
03-03-2004, 04:50 PM
No. I've been working on a site for 3 weeks, and it dynamically builds tables based on user selections, etc.
I am currently inputting the data through phpMYadmin, but plan to do a form with a password soon.
you'll get a lot of help here, and at phpfreaks.com, and webdeveloperworld.com too.
John
Conor
03-03-2004, 05:23 PM
CREATE TABLE ncs_news (
news TEXT NOT NULL AUTO_INCREMENT,
);
why doesnt that work?
Bigjohn
03-03-2004, 05:52 PM
Originally posted by RefreshF5
CREATE TABLE ncs_news (
news TEXT NOT NULL AUTO_INCREMENT,
);
why doesnt that work?
I would recommend setting up your tables using phpMYadmin.
It's much easier.
Then you connect to the db like this:
$dbh=mysql_connect ("localhost", "xxxxx_joetest", "password") or die ('Cannot connect to Database - error: ' . mysql_error () );
mysql_select_db("xxxxx_joetest1",$dbh);
that makes the connection. then you do a SELECT or INSERT query to extract and display or add data.
John
Conor
03-03-2004, 06:11 PM
umm all i saw in phpmyadmin was a bix box where i can do sql queries. is there another way to setup tables.
Conor
03-03-2004, 08:22 PM
ok so correct me if im wrong but this is what id do?
1.make a table in my database with autoincrement field(if anyone has the syntax that would be great)
2. Make a form that inserts my little news think into the database(clarification on how i would do this would be awesome)
3. on my main page to something that echo's the SELECT * from table_name
so is this correct/anyone know how to do it and can help me.
1. You'll have to clarify. Do you know how to make a table, and just need to know how to set one of the fields to auto increment?
2. This is mostly going to depend on what you are inserting, but a basic query might look like this:
$sql = "INSERT INTO `tablename` (`id`, `field1`, `field2`) VALUES ('', `data1`, `data2`);";
mysql_query($sql);
3. Again, it will depend on what you need to do, but here's how to build an array of all the data in a table:
$sql = "SELECT * FROM `tablename`;";
$results = mysql_query($sql);
$data = mysql_fetch_array($results);
# $data is the array of the results
# $data['field1'] or $data['field2']
Conor
03-04-2004, 06:06 AM
yeas i know how to make a table and just need to know how to make it auto increment so it can store all my little news stories.
Conor
03-04-2004, 06:06 AM
yeas i know how to make a table and just need to know how to make it auto increment so it can store all my little news stories.
Bigjohn
03-04-2004, 06:11 AM
Originally posted by RefreshF5
yeas i know how to make a table and just need to know how to make it auto increment so it can store all my little news stories.
see pyro's number 2.
You specify the ID field but you insert ''. That invokes 'autoincrement'.
John
Yes, as long as you set the `id` field to auto increment. When creating the field (in phpMyAdmin) there should be a drop-down on the right with this option. It's usually a good idea to include an id field, which auto increments. I also set this to be my primary field. Makes it easy, that way.
Conor
03-04-2004, 02:13 PM
where is the thing to create the the table in phpmyadmin i cant find it.
Bigjohn
03-04-2004, 05:57 PM
Originally posted by RefreshF5
where is the thing to create the the table in phpmyadmin i cant find it.
In myPHPadmin there is, on the first screen, the 'create new database' link.
below that is 'databases'.
click that. in the left column will appear your database name
Click that
you'll see any tables that already exist, or, a box that says 'create a new table'.
John
Conor
03-04-2004, 06:05 PM
CREATE TABLE `news_ncs` (
`news` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY ( `news` )
);
thats the code it gave me for the table i created
will that work for the news thing i want to do?
Bigjohn
03-04-2004, 06:23 PM
Originally posted by RefreshF5
CREATE TABLE `news_ncs` (
`news` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY ( `news` )
);
thats the code it gave me for the table i created
will that work for the news thing i want to do?
you need at least 2 fields. one is the autoincrement, the other is the 'text' field that you fill with your news.
John
Conor
03-04-2004, 06:26 PM
ah i see so i would add ALTER TABLE news ncs ADD news_text TEXT
Bigjohn
03-04-2004, 07:38 PM
Originally posted by RefreshF5
ah i see so i would add ALTER TABLE news ncs ADD news_text TEXT
right.
Now, understand that while myPHPadmin shows you the code, it's already been executed. So you don't have to do anything else, except add a user to the db so you can access it from a webpage.
John