简体   繁体   中英

Trying to connect database MySQL from PHP (Not Successful)

I'm following a tutorial on the net on PHP and MySQL.

I'm using Linux. I'm trying to establish a connection to a database but it is not working. I have my database:

create database test;
create table user(name text, pass text);
insert into user values('john', '123');

and then my php:

<?php

    $_host = "localhost";
    $_dbuser = "root"
    $_dbpass = "";
    $_dbname = "test";

    @mysql_connect("$_host", "$_dbuser", "$_dbpass") or die("could not connect");
    @mysql_select_db("$_dbname") or die("no database");

    echo "connection stablished";

?> 

And the output of my file is just a blank tab on the browser. What should I do to solve this? What am I doing wrong?

Thank you in advance. I'm very new to web programming.

$_host = "localhost";
$_dbuser = "root"
$_dbpass = "";
$_dbname = "test";

$link = mysqli_connect($_host, $_dbuser, $_dbpass, $_dbname);

if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo "connection stablished";

Now...

  1. Use mysqli_* functions as mysql_* are deprecated.
  2. You will need $link variable later for queries.
  3. As you're newbie read this: How can I prevent SQL injection in PHP?
  4. Prepared statements FTW! Remember!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM