/    Sign up×
Community /Pin to ProfileBookmark

Any Tutorials on using PHP with MySQL

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.

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@BigjohnMar 03.2004 — [i]Originally posted by RefreshF5 [/i]

[B]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. [/B][/QUOTE]


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
Copy linkTweet thisAlerts:
@ConorauthorMar 03.2004 — yes i have phpmyadmin. Also is it hard to retrieve the information from the database and have a form write to the db.
Copy linkTweet thisAlerts:
@BigjohnMar 03.2004 — 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
Copy linkTweet thisAlerts:
@ConorauthorMar 03.2004 — CREATE TABLE ncs_news (

news TEXT NOT NULL AUTO_INCREMENT,

);


why doesnt that work?
Copy linkTweet thisAlerts:
@BigjohnMar 03.2004 — [i]Originally posted by RefreshF5 [/i]

[B]CREATE TABLE ncs_news (

news TEXT NOT NULL AUTO_INCREMENT,

);





why doesnt that work? [/B]
[/QUOTE]


I would recommend setting up your tables using phpMYadmin.

It's much easier.

Then you connect to the db like this:
[code=php]
$dbh=mysql_connect ("localhost", "xxxxx_joetest", "password") or die ('Cannot connect to Database - error: ' . mysql_error () );
mysql_select_db("xxxxx_joetest1",$dbh);
[/code]


that makes the connection. then you do a SELECT or INSERT query to extract and display or add data.

John
Copy linkTweet thisAlerts:
@ConorauthorMar 04.2004 — umm all i saw in phpmyadmin was a bix box where i can do sql queries. is there another way to setup tables.
Copy linkTweet thisAlerts:
@ConorauthorMar 04.2004 — 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.
    Copy linkTweet thisAlerts:
    @pyroMar 04.2004 — 
  • 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:


  • [code=php]$sql = "INSERT INTO tablename (id, field1, field2) VALUES ('', data1, data2);";
    mysql_query($sql);[/code]


  • 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:


  • [code=php]$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'][/code]
    Copy linkTweet thisAlerts:
    @ConorauthorMar 04.2004 — 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.
    Copy linkTweet thisAlerts:
    @ConorauthorMar 04.2004 — 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.
    Copy linkTweet thisAlerts:
    @BigjohnMar 04.2004 — [i]Originally posted by RefreshF5 [/i]

    [B]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. [/B][/QUOTE]


    see pyro's number 2.

    You specify the ID field but you insert ''. That invokes 'autoincrement'.

    John
    Copy linkTweet thisAlerts:
    @pyroMar 04.2004 — 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.
    Copy linkTweet thisAlerts:
    @ConorauthorMar 04.2004 — where is the thing to create the the table in phpmyadmin i cant find it.
    Copy linkTweet thisAlerts:
    @BigjohnMar 04.2004 — [i]Originally posted by RefreshF5 [/i]

    [B]where is the thing to create the the table in phpmyadmin i cant find it. [/B][/QUOTE]


    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
    Copy linkTweet thisAlerts:
    @ConorauthorMar 05.2004 — 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?
    Copy linkTweet thisAlerts:
    @BigjohnMar 05.2004 — [i]Originally posted by RefreshF5 [/i]

    [B]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? [/B]
    [/QUOTE]


    you need at least 2 fields. one is the autoincrement, the other is the 'text' field that you fill with your news.

    John
    Copy linkTweet thisAlerts:
    @ConorauthorMar 05.2004 — ah i see so i would add ALTER TABLE news ncs ADD news_text TEXT
    Copy linkTweet thisAlerts:
    @BigjohnMar 05.2004 — [i]Originally posted by RefreshF5 [/i]

    [B]ah i see so i would add ALTER TABLE news ncs ADD news_text TEXT [/B][/QUOTE]

    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
    ×

    Success!

    Help @Conor spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...