-2

I am very new to php. I am trying to make a sign up page with php. So simple but I can't. I've searched across google how to connect it. They said below.

  <?PHP

  $user_name = "root";
  $password = "";
  $database = "user";
  $server = "127.0.0.1";

  mysql_connect($server, $user_name, $password);

  print "Connection to the Server opened";

  ?>

I've successfully created my user database with full set of data in phpmyadmin. And running Apache and Mysql in control panel. I set the password in phpmyadmin and I changed it in $password="mypassword";. But there is no print on my web page. I think the above code is correct and I am having problems before this state. Such as the location of my database, I don't where to put it or just created on myadmin is fine. Thank you for reading my problem and kindly advice me for the beginner course.

user3517970
  • 63
  • 2
  • 10

3 Answers3

2
<?php
 $user_name = "root";
  $password = "";
  $database = "user";
  $server = "127.0.0.1";
// Create connection
$con=mysqli_connect($server, $user_name, $password, $database );

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Try mysqli instead of mysql and here is why

Community
  • 1
  • 1
Hitesh
  • 4,098
  • 11
  • 44
  • 82
0

no no . . sorry . My html path . . on desktop

PHP, in this context, is a server side programming language. You must request the script from a server that will pass it through a PHP interpreter before sending it to the browser.

Type http://localhost/etc/etc into the browser's address bar. Don't open the PHP program directly in your browser (e.g. by double clicking the file in Windows Explorer).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Here is object not found error. you mean I have to export database to my web page directory? – user3517970 Sep 28 '14 at 19:46
  • @user3517970 — if you mean a 404 error, then you have to use the URL of your PHP script. `/etc/etc` was a placeholder example. The PHP file needs to be accessible to your webserver. The database server needs to be running on the IP address you specified in your PHP program. – Quentin Sep 28 '14 at 19:46
  • I exported user.sql from phpmyadmin and put it to my signup webpage folder. Is this correct ? – user3517970 Sep 28 '14 at 19:50
  • @user3517970 — No. I just told you that you didn't have to do that. Your **PHP script** has to be accessible to your webserver. Your PHP script will communicate with your database server. Any export of your database is useful only for backup purposes. – Quentin Sep 28 '14 at 19:51
  • sorry for my knowledge. you can point me a reference for a beginner guide. I am sorry. – user3517970 Sep 28 '14 at 19:53
  • thank you for your patient answering. However the answers of the others are skillful, you are the best trainer for beginners like me. Thank you so much. I got it ! – user3517970 Sep 28 '14 at 20:08
0

If you are going to use procedural style function calls with the mysqli interface:

$con = mysqli_connect('localhost','myuser','mypassword','mydb')
       or die("Error " . mysqli_error($con)); 

If you are going to use object oriented style with mysqli interface:

$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'mydb');

Reference: http://php.net/manual/en/mysqli.construct.php


If you are going to use PDO interface

$dbh = new PDO('mysql:host=localhost;dbname=mydb', $user, $pass);

Reference: http://php.net/manual/en/pdo.connections.php

spencer7593
  • 106,611
  • 15
  • 112
  • 140
  • While rewriting the code to use a modern MySQL API is good practise, this doesn't address the problem. – Quentin Sep 28 '14 at 19:53
  • 1
    @Quentin: What this answer addresses is the question in the title of the question "how to connect php with mysql". It doesn't address the questions: "How do I start the webserver?", "How do I get the webserver to run a PHP script?", or "Why is my webserver responding with 404?" or "Why does hello_world.php page not display anything?" And I don't think I'm suggesting "rewriting code"; I'm suggesting that the OP write code that won't need to be rewritten later." – spencer7593 Sep 28 '14 at 20:13
  • The idea is that you understand the actual problem and not simply literally answer the title. – Quentin Sep 28 '14 at 20:16