Mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Chazster
Forum Newbie
Posts: 4
Joined: Mon Jun 24, 2002 10:55 am

Mysql

Post by Chazster »

In my Mysql table the primery field is called ID and contains a number. Starts at '1' and goes up for each entry I have.


Say if I called my script like this : - myscript.php?record=2

What code to I need to be able to print record 2?

SO I need some code which connects to the database and table then find the record from the url then print it??

Have I lost you yet? :lol:

Also one of my fields is a message and when I come to print it out in php the data is all on one line - how can I fix this?
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

use:
- mysql_connect()
- mysql_query()
- mysql_fetch_assoc()
- mysql_close()
Chazster
Forum Newbie
Posts: 4
Joined: Mon Jun 24, 2002 10:55 am

:)

Post by Chazster »

Could you give me an example please? :D
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

there are plenty of examples in the manual. please go read it before asking for simple things like this here. thanks.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Try this code and you'll see how it works.

Code: Select all

if ($_SERVERї'REQUEST_METHOD'] == 'get') {
   if (isset($_GETї'record'])) {
      // you need to edit these values to match your mysql setup
      $db_host = "localhost";
      $db_user = "username";
      $db_pass = "password";
      $db_name = "database_name";
      $table = "table_name";

      mysql_connect($db_host, $db_user, $db_pass);
      mysql_select_db($db_name);
      
      $query = "SELECT * FROM $table WHERE id='{$_GETї'record']}'";
      $results = mysql_query($query);

      if (mysql_num_rows($results) == 0)
         die("No results found.");
      else {
         while ($row = mysql_fetch_assoc($results)) {
            foreach ($row as $index=>$value)
               echo $index." = ".$value."<br>";
         &#125;
      &#125;

      mysql_close();
   &#125;
&#125;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Mysql

Post by twigletmac »

Chazster wrote:SO I need some code which connects to the database and table then find the record from the url then print it??
I agree with enygma, he gave you the functions that you could use and if you had bothered to check the manual you would have found a number of examples. You could have even searched this forum and found exactly what you were looking for.
Chazster wrote:Also one of my fields is a message and when I come to print it out in php the data is all on one line - how can I fix this?
You need to use the nl2br() function on the information.

Mac
Chazster
Forum Newbie
Posts: 4
Joined: Mon Jun 24, 2002 10:55 am

Post by Chazster »

Oooooooo Handbags down Ladys.


I thought one of the ideas for a forum is to help people. Don't worry, you'll be happy to know that I will not be asking for help in this forum again.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It is frankly irritating when you send someone in the right direction and they don't attempt to help themselves at all. Needing help with some code that you've written is one thing, basically asking us to write your code without you attempting any of your own is unfair. If you want your code written by someone else pay them.

The frustration arises because what you asked is incredibly basic and there are multitudes of tutorials available which would have been a better place to start. For example searching on Google using the search string 'connect to mysql using php' yields the following results.

I apologise for being rude and don't want to deter anybody from posting but I think you would have had a similar response from most of the PHP forums out there.

Mac
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Ok, yeah, i agree with the fact you should write your own code. But let's not forget that the best way to learn how to code effectively is by example. I did not mind posting this simple query algorithm. Hopefully it is somewhat helpful to the person who originally asked the question.

Reading the manual should be the first thing done, but when you are stuck after that, there is someone out there who is willing to help.

Don't leave the forum Chazter. Just try to research your problem more in full before asking such a generalized question. Good luck with the rest of your coding.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

fair enough protokol, but lets see if you are as helpful when it's the fifth time you have posted the same bit of code to help someone that couldn't be bothered to a bit searching.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

nah, just remember the address which points to the original post and then display that url .. there are ways around typing things a million times guys

http://www.devnetwork.net/forums/viewto ... ight=#5212

pretty easy, huh
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Why should you or I have to search the forum for information that the other person could have found just as easily? I often remember answering the same question before but unlike you I have been involved in so many threads that I can't remember exactly which one was about what (undescriptive subjects like 'mysql' are part of that problem). When you've posted a few hundred times you'll probably understand better. I like being able to help people but there does need to be some giving on their part, not just taking.

Mac
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

fair enough.
Post Reply