-4
<?php

$getdatum = $_POST["notdienstdatum"];

mysql_connect("127.0.0.3","db","password");
mysql_select_db("db");

$dbselect = "SELECT apotheke FROM wp_notdienst WHERE datum = '$getdatum'";
$dbquery = mysql_query($dbselect);

$dbcount = mysql_num_rows($dbquery);

$dboutput = mysql_fetch_assoc($dbquery);
echo utf8_encode($dboutput["apotheke"]);

?>

How can I update the code so it's working with newer versions of PHP? I've already tried to make a new connection over

$conn = new mysqli($servername, $username, $password);

but I have problems to make mysql_query working..

Thanks for helping me out

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Johnny Kermode
  • 75
  • 2
  • 12
  • 2
    It's not just `mysql_connect` that is deprecated, also all other `mysql_*` functions. You should replace them all with `mysqli_*` functions (or PDO). – Bart Friederichs Jun 18 '18 at 13:44
  • Possible duplicate of [Fatal error: Call to undefined function mysql\_connect()](https://stackoverflow.com/questions/10615436/fatal-error-call-to-undefined-function-mysql-connect) – Rotimi Jun 18 '18 at 13:51
  • You are right, I've already tried that, but I get this error in return: mysqli_select_db() expects exactly 2 parameters, 1 given in xxxxx – Johnny Kermode Jun 18 '18 at 13:52
  • Possible duplicate of [Deprecated: mysql\_connect()](https://stackoverflow.com/questions/21797118/deprecated-mysql-connect) – undrftd Jun 18 '18 at 13:56

3 Answers3

2

You can connect like this using mysqli:

define('DB_SERVER', 'domain');
define('DB_USER', 'db_user');
define('DB_PASS', 'password');
define('DB_NAME', 'db_name');
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if($mysqli->connect_errno) {
  die('Database is down, try again in a minute.');  
}

The if statement displays a message if the connection fails.

Example SELECT statement:

$qry = "SELECT name FROM people WHERE id = ?";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('s', $id_to_retreive);
$stmt->execute();
$result = $stmt->get_result();
while($row = mysqli_fetch_assoc($result)) {
    $row['name'] // the retreived field
}

The advantage of prepared statements is that it's far more secure than having to remember to escape strings - see here

Matt
  • 1,518
  • 4
  • 16
  • 30
0

Read the MySQLi documentation and replace MySQL function with proper MySQLi functions.

For the code, you provided this is how it needs to be changed

$con = mysqli_connect("127.0.0.3","username","password", "db_name");

$getdatum = mysqli_real_escape_string($con, $_POST["notdienstdatum"]);

$dbselect = "SELECT apotheke FROM wp_notdienst WHERE datum = '$getdatum'";
$dbquery = mysqli_query($con, $dbselect);

$dbcount = mysqli_num_rows($dbquery);

if($dbcount > 0)
{
    while($row =  mysqli_fetch_assoc($dbquery))
    {
        echo utf8_encode($row["apotheke"]);
    }
}
else
{
echo "0 rows!";
}

But consider using PDO instead, it is more secure!

Ylber Veliu
  • 349
  • 2
  • 13
-1

Please look at the php mysql_connect documentation http://php.net/manual/en/function.mysql-connect.php

It has been deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.

Maybe your php version is higher.

So please use mysqli_connect

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

Aram Grigoryan
  • 740
  • 1
  • 6
  • 24