0

I am currently attempting to migrate my website from MVC asp.net to PHP after experiencing numerous issues with my host provider. The initial changes have proved easy with PHP being quite simple to get to grips with. However, I have hit a brick wall for the last few pages that require interaction with an SQL database.

I have worked through a number of different tutorials and helps and each one leads to the sample problem.

function db_connect()
{
    $conn = mysql_connect ('localhost', 'root', '') or die ('I cannot connect to the database because: ' . mysql_error());

    mysql_select_db ('Database');

    return $conn;
}

Which returns the error "Call to undefined function mysql_connect()" on the first line.

I have checked the php.ini file and this seems correct

; extension_dir = "./"
; On windows:
extension_dir = "C:\Program Files\Jcx.Software\VS.Php\2013\php 5.4\ext"

extension=php_mysql.dll

And the dll is in the corresponding directory.

I think I am probably missing something really obvious, but would appreciate any help someone can give me.

feeela
  • 29,399
  • 7
  • 59
  • 71
Chris Harland
  • 456
  • 3
  • 16
  • see what you have `phpinfo();` – ɹɐqʞɐ zoɹǝɟ May 13 '14 at 15:05
  • 7
    If you are just starting to port that code you really should avoid using the old `mysql_*` functions as they are deprecated. – feeela May 13 '14 at 15:05
  • 3
    Please don't use the deprecated mysql extension, it's been discouraged for years now. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool May 13 '14 at 15:05
  • 4
    Most tutorials try to teach you very very old practice, even deprecated or removed stuff like `mysql_` functions. Google for `mysql php pdo` and you will find better stuff. – Daniel W. May 13 '14 at 15:06
  • 2
    Try using PDO or mysqli* functions instead, just as a note. – Brian Warshaw May 13 '14 at 15:06
  • 2
    "after experiencing numerous issues with my host provider" And using PHP under Windows is a realistic hosting assumption? I guess you should start using a UNIX based system to develop such things… – feeela May 13 '14 at 15:07
  • 1
    As you can tell from the above comments, you should **not** use mysql_ functions, as the **[manual clearly indicates](http://us1.php.net/function.mysql-connect)**. Use either **[mysqli](http://www.php.net/manual/en/book.mysqli.php)** or **[PDO](http://www.php.net/manual/en/book.pdo.php)** methods for your database connection. And make sure you know what **[SQL Injection](http://www.php.net/manual/en/security.database.sql-injection.php)** is and how to avoid it happening. – vogomatix May 13 '14 at 15:13
  • 1
    If you're migrating from MVC to PHP, you really should be using a [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) like [Laravel](http://laravel.com/). Writing PHP from the ground up is going to take forever and, as you've demonstrated here, you're going to do it completely wrong unless you spend some time reading a guide like [PHP the Right Way](http://www.phptherightway.com/). – tadman May 13 '14 at 15:21
  • Thanks guys. That's great. Tried PDO and it's worked straight away. That'll teach me for looking at tutorials. lol – Chris Harland May 14 '14 at 08:12

1 Answers1

0

use mysqli. By the way, you should create another user with set privileges and never have users log in as the root user

function db_connect()
{

    $conn = new mysqli("localhost", "root", $password, $database);

    if ($conn->connect_errno){
       //Error connecting
    }
}   
ksealey
  • 1,698
  • 1
  • 17
  • 16
  • PDO is significantly better than `mysqli` when it comes to binding parameters, the named placeholder feature alone is irreplaceable. If you must use `mysqli`, at least use the object-oriented interface. – tadman May 13 '14 at 15:22