0

I keep getting this error, and i do not know much about PHP, but I would like to get started with it and with MySQL, but this error stops me from continueing with my learning!

Info
Server Program: Niginx
Installed Extras: PHP [5.6.4] | MySQL

Files:

<?php include "includes/config.php"; ?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" style+"text/css" href="css/main.css" />
<script type="text/javascript" language="javascript" src="javascript/main.js"></script>

</head>

<body>

</body>
</html>

config.php:

<?php

//connect to database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("chat") or die(mysql_error());

echo "Working";
?>
Rizier123
  • 57,440
  • 16
  • 89
  • 140
  • 3
    Suggests that the old deprecated MySQL extension hasn't been enabled on your server.... which is good.... you should be using MySQLi or PDO anyway – Mark Baker Feb 12 '15 at 14:29
  • Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Feb 12 '15 at 14:30
  • And in the greater scheme of things, you shouldn't be connecting to mysql as the root user, with no password. That's a massive security problem on two levels. – Marc B Feb 12 '15 at 14:30
  • @MarkBaker What shall i do then? I need help as it is a project, do you think I should use somethings else? – Lightning XD Feb 12 '15 at 14:31
  • And when you use MySQLi or PDO, you'll need to `include` the library; your root problem now is that you're not including the **DEPRECATED** (sorry to shout :) `MySQL` library. – Ed Gibbs Feb 12 '15 at 14:32
  • @MarcB Yeah, but that doesn't matter as i am only hosting it as LocalHost so no one else is going to see it until i put it on my website... COuld you help me? – Lightning XD Feb 12 '15 at 14:33
  • What shall i do then? how do i enable the MySQL deprecated thingy... @MarcB – Lightning XD Feb 12 '15 at 14:35
  • You don't enable it, you start by using either [MySQLi](http://www.php.net/manual/en/book.mysqli.php) or [PDO](http://www.php.net/manual/en/book.pdo.php) instead.... what's the point of learning the MySQL extension (that is being killed off with good reason) when it will be officially dead before the end of this year? – Mark Baker Feb 12 '15 at 14:41
  • @LightningXD `mysqli` works in practically exactly the same way as `mysql`, just use `mysqli` instead. – Spencer Wieczorek Feb 12 '15 at 14:49
  • Why would people want to change it to MySQLi? Might as well keep it the same to make it easier! I know, i have to change it in my PHP code, i have done it and now it works thanks to everyones help! – Lightning XD Feb 12 '15 at 15:00
  • possible duplicate of [Undefined function mysql\_connect() error](http://stackoverflow.com/questions/4770046/undefined-function-mysql-connect-error) – user2428118 Feb 12 '15 at 15:08
  • You mean why was MySQL dropped? `It was originally introduced in PHP v2.0 for MySQL v3.23, and no new features have been added since 2006. Coupled with the lack of new features are difficulties in maintaining such old code amidst complex security vulnerabilities.` Because the old MySQL extension was so difficult to update (patching up badly written code is hard, so "keeping it the same" isn't easier), the MySQLi extension was written as an completely new alternative in a way that supported the new features of later MySQL versions, and that was easy to maintain and update on an ongoing basis. – Mark Baker Feb 12 '15 at 16:18

1 Answers1

1

I assume you are following an old tutorial on connecting to MySQL databases.

The library for commands beginning with mysql_ is deprecated. This that you shouldn't be using when writing code in php, as it will be "turned off" in the future. Take a look at the mysql_connect() command's documentation for more information.

Thankfully, there's a number of alternative ways for you to connect to a database, which aren't going to be turned off any time soon! The one I'd recommend in your situation for simplicity is the mysqli_ library. Looks pretty similar, right? You use it in a fairly similar way too, check the documentation for mysqli_connect().

Converting your mysql_ commands to mysqli_ is not hard, take a look at this answer for some more help.

Community
  • 1
  • 1
Hecksa
  • 2,512
  • 21
  • 33