The following code returns an 500 error as the cod

2019-09-26 10:11发布

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);

?>

标签: php mysql
2条回答
Fickle 薄情
2楼-- · 2019-09-26 10:33

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

查看更多
The star\"
3楼-- · 2019-09-26 10:57

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!

查看更多
登录 后发表回答