-2

I get this error:

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\login\dbconnect.php:3 Stack trace: #0 C:\xampp\htdocs\login\index.php(3): include_once() #1 {main} thrown in C:\xampp\htdocs\login\dbconnect.php on line 3

This is how my Code looks like

    <?php
      error_reporting( E_ALL & ~E_DEPRECATED & ~E_NOTICE );
        if(!mysql_connect("localhost","root",""))
      {
             die('oops connection problem ! --> '.mysql_error());
      }
        if(!mysql_select_db("dbtest"))
      {
             die('oops database selection problem ! --> '.mysql_error());
      }

    ?>
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
VladJ
  • 103
  • 2
  • 9

2 Answers2

2

This is probably because you are using PHP7.0.?

The mysql_ API is no longer part of PHP7, it has been deprecated for a number of years and has been completely removed from PHP7.

To use this API you will have to switch back to PHP5.6 or earlier versions.

I assume you have WAMPServer 3 installed so all you need to do to switch to a version of PHP5.6 is to do this using the wampmanager menus

left click wampmanager icon -> PHP -> 5.6.19

You should then begin the process of amending all your code to use the PDO or mysqli_ API's

Ahh you are using XAMPP, in that case you will have to install an earlier version of XAMPP that has an older version of PHP, V5.6.? of PHP or earlier

RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
  • What can I do then? – VladJ Jul 13 '16 at 22:04
  • change to PDO or mysqli instead. – William Okano Jul 13 '16 at 22:04
  • Is it worth adding to your answer how to fix it _in case_ someone is already in a pre-7 version?, – FirstOne Jul 13 '16 at 22:07
  • @WilliamOkano How? – VladJ Jul 13 '16 at 22:08
  • Sadly, you will have to re-code every piece of code that uses those functions. I think this link can be helpful to you: [http://stackoverflow.com/questions/33517819/mysql-to-mysqli](http://stackoverflow.com/questions/33517819/mysql-to-mysqli) – William Okano Jul 13 '16 at 22:09
  • @RiggsFolly when some functions is deprecated php says that theme is deprecated, Call to undefined function -> this may be due to mysql.dll into ini file – Vanya Avchyan Jul 13 '16 at 22:09
  • @VanyaAvchyan It would be very unusual that any version of PHP that contains the `mysql_` api would not have it turned on! – RiggsFolly Jul 13 '16 at 22:10
  • @VanyaAvchyan Any way I can test that? Or solve it? – VladJ Jul 13 '16 at 22:10
  • @VanyaAvchyan the mysql_ API was deprecated until the php 5.6. In the lastest version (7.0) it was completely removed. Although it could be an older version which just don't have the extension enable, which I think it's very unlikely being an wamp server. – William Okano Jul 13 '16 at 22:11
  • How can I rewrite it using mysqli? – VladJ Jul 13 '16 at 22:13
  • That is a big job. Best to start now – RiggsFolly Jul 13 '16 at 22:14
  • @RiggsFolly How to start? I know nothing about mysqli – VladJ Jul 13 '16 at 22:16
  • @William Okano But how do you know what his version of mysql ?https://www.youtube.com/watch?v=e3y57I63gCw – Vanya Avchyan Jul 13 '16 at 22:16
  • The best place to start is [The MYSQLI Manual](http://php.net/manual/en/book.mysqli.php) But I would suggest [the PDO API is a better idea](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jul 13 '16 at 22:17
  • @VanyaAvchyan you're right. I don't know. But since there's no version on the xampp package, I just assumed that is the latest version. Also, xampp comes by default with mysql enabled, so, without more info, the best fit for this error would be PHP 7.0, which has not mysql_ functions enabled anymore :) – William Okano Jul 13 '16 at 22:24
  • There are older XAMPP versions available on the download page. But as I assume you are still learning, down waste time with the `mysql` api – RiggsFolly Jul 13 '16 at 22:29
  • @William Okano Yes, I agree that the 7 version maybe this error.But if the version is lower, it is not so.Yes, of course I agree – Vanya Avchyan Jul 13 '16 at 22:34
0
$conn = new mysqli('localhost','root',"",$dbname); //($dbname is optional)

from there you can query in the following manner:

$requestString = "SELECT X FROM $tablename WHERE $columnName='$z';

$result = mysqli_query($conn,$requestString);
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
Shizzle
  • 851
  • 1
  • 10
  • 25
  • That script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jul 13 '16 at 22:23