Page 1 of 1

Mysql

Posted: Mon Jun 24, 2002 1:34 pm
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?

Posted: Mon Jun 24, 2002 1:42 pm
by enygma
use:
- mysql_connect()
- mysql_query()
- mysql_fetch_assoc()
- mysql_close()

:)

Posted: Mon Jun 24, 2002 1:55 pm
by Chazster
Could you give me an example please? :D

Posted: Mon Jun 24, 2002 2:00 pm
by enygma
there are plenty of examples in the manual. please go read it before asking for simple things like this here. thanks.

Posted: Mon Jun 24, 2002 2:08 pm
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;

Re: Mysql

Posted: Tue Jun 25, 2002 2:11 am
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

Posted: Tue Jun 25, 2002 3:49 am
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.

Posted: Tue Jun 25, 2002 4:06 am
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

Posted: Tue Jun 25, 2002 11:20 am
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.

Posted: Tue Jun 25, 2002 12:40 pm
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.

Posted: Tue Jun 25, 2002 2:06 pm
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

Posted: Wed Jun 26, 2002 2:31 am
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

Posted: Wed Jun 26, 2002 9:54 am
by protokol
fair enough.