1

I'm working on a website, where I want to store some user inputs, in a mySQL database.

I've searched all over, but everyone who writes about a simple example, never showns the complete source code.

I'm totally new to PHP and mySQL, so I really need it to be dead simple... :-)

Thank you in advance...

//Kenneth

curly_brackets
  • 4,841
  • 14
  • 51
  • 97
  • possible duplicate of [Reference: What is a perfect code sample using the mysql extension?](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – marc_s Jun 17 '11 at 11:31

3 Answers3

2

First, you will need to call mysql_connect() to connect to your database which I assume exists. You can use the sample code below:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

Then you will need to select a database using mysql_select_db():

$db = mysql_select_db('db');

Now it's time for a mysql_query()! You will need to know the language SQL to do more advanced queries.

mysql_query('INSERT INTO users (username, password, email) VALUES ("john", "passwd", "john@example.com")', $link);

Good luck!

John Moberg
  • 163
  • 11
0
$conn=mysql_connect('localhost','username','password');
$db= mysql_select_db('yourDBname');
$query="your query here";
mysql_query($query,$conn);
Sujit Agarwal
  • 11,650
  • 10
  • 44
  • 76
-2

You should never use mysql_* it's old and slow. PDO is what you are looking for. It's fast, safe, and easy to use

mario
  • 138,064
  • 18
  • 223
  • 277
skowron-line
  • 4,684
  • 8
  • 44
  • 69
  • Since when is mysql_* slower than PDO? – cypher Jun 17 '11 at 10:48
  • `http://stackoverflow.com/questions/1402017/php-pdo-vs-normal-mysql-connect` 3 post – skowron-line Jun 17 '11 at 10:54
  • This is valid advise. But wrong in its imperative form. PDO is not more secure and faster by itself. Only if you also utilize prepared statements. "Just using PDO" is misleading to newcomers in that regard. And it should really be the ease of use advantages which should be stressed. (Also please avoid msg speak; and if it's a one-liner it should have been [a comment](http://stackoverflow.com/privileges/comment). Anyway +1 because it might be very relevant to OP.) – mario Jun 17 '11 at 11:05