0

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.

Telmo Vaz
  • 189
  • 1
  • 2
  • 11
  • Don't use mysql_*; use mysqli_*. The mysql_* functions are deprecated. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1 – elixenide Nov 11 '13 at 21:50
  • `$_dbname = test";`. You have not enclosed the string in double quotations at both ends. Should read `$_dbname = "test";` – Lock Nov 11 '13 at 21:51
  • thank you for your corrections. I've corrected it but still gives me the blank page. Must be something else – Telmo Vaz Nov 11 '13 at 21:56
  • Now you edited your question. If you don't see `could not connect` from your `die`, then you have problems elsewhere. – Funk Forty Niner Nov 11 '13 at 21:56
  • 1
    You should remove the "@" as you shouldn't be suppressing your errors, whether you have the `die()` clause or not. – War10ck Nov 11 '13 at 22:02
  • In your example, your password is blank and your logging in as root. Does your setup allow a root login? Does it allow an empty password? Are you using wamp or are you on a real server? I assume you are getting a connection, as it should die() with your error message if it wasn't. Try running a query afterwards and die() to see where you land... Looking at your code, the echo at the end should be outputting to the screen. What is your error reporting level? Maybe an error is getting suppressed? Temporarily set your error reporting level to E_ALL and see if you get any errors reported. – IncomePitbull Nov 11 '13 at 22:05

1 Answers1

0
$_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!
Community
  • 1
  • 1
speccode
  • 1,562
  • 9
  • 11
  • thank you. From the moment it goes to the mysql function (either one) the php script stops working. Everything that is before that msql_ msqli_ function appears, but not after. – Telmo Vaz Nov 11 '13 at 22:07
  • At beginning of your php file add this `ini_set('display_errors', 1);` and try again. Tell us if there is any error. Maybe you don't have enabled `mysqli` or `mysql` extension? – speccode Nov 11 '13 at 22:11
  • It appears this: " Fatal error: Call to undefined function mysqli_connect() in /home/username/www/test.php on line 6 " – Telmo Vaz Nov 11 '13 at 22:14