-3

Here is the code that used but return the 500 error in php version 7 and I totally screwed what to do with and could not find any documentation to do so.

<?php
// Create a new MySQL database connection
if (!$con = mysql_connect('localhost', 'root', 'password')) {
die('An error occurred while connecting to the MySQL server!<br><br>' . mysql_error());
}

if (!mysql_select_db(sample)) {
die('An error occurred while connecting to the database!<br><br>' . mysql_error());
}

// Create an array of MySQL queries to run
$sql = array(
'DROP TABLE IF EXISTS content;',
'CREATE TABLE content SELECT * FROM sample1.content'
);

// Run the MySQL queries
if (sizeof($sql) > 0) {
foreach ($sql as $query) {
if (!mysql_query($query)) {
die('A MySQL error has occurred!<br><br>' . mysql_error());
}
}
}

mysql_close($con);

?>
  • 2
    Simple, you can't *make it work*. `mysql_*` functions are deprecated as of PHP 5.5 and are removed altogether in PHP 7. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO`](http://php.net/manual/en/book.pdo.php) instead. Also learn about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement). – Rajdeep Paul Sep 12 '16 at 17:11
  • 1
    `mysql_*` functions were deprecated practically an eternity ago... See: [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Alexander O'Mara Sep 12 '16 at 17:11

2 Answers2

4

You are using an deprecated implementation of mysql that have been removed in php7.

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_query() PDO::query()

please check http://php.net/manual/en/function.mysql-query.php

olibiaz
  • 2,551
  • 4
  • 29
  • 31
0

Here's a good tutorial about converting deprecated mysql_* PHP code to new mysqli_* code:

http://www.phpclasses.org/blog/package/9199/post/3-Smoothly-Migrate-your-PHP-Code-using-the-Old-MySQL-extension-to-MySQLi.html

In many cases, you simply need to change "mysql" to "mysqli" for each function call.

Remember to change them all!

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828