首页 > 解决方案 > 如何在 php7 中更改旧版本的 php 工作

问题描述

如何使这个旧版本的 php 在 php7 中工作?我有一个 mysql 数据库,用于发布我在 Construct 2 中创建的游戏的高分。但现在它已停止工作,因为我的主机更新为 php7(目前我可以在 php 7.1 - 7.3 之间进行选择)

我尝试了很长时间,搜索网络,使其再次工作,但一直无法解决。

我有 2 个 php 文件:getscores.php 和 savescores.php

当我尝试在 webbrowser ( Chrome ) 中查看 getscores.php 时,我收到一个错误:致命错误:未捕获的错误:调用未定义的函数 mysql_query() ...它指的是第 18 行。

对不起,我对 php 和 mysql-databases 几乎一无所知

非常感谢你,提前,如果有任何人可以提供帮助。:) ///灵魂机器!

获取分数.php

<?php
header('Access-Control-Allow-Origin: *');

$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="scores"; // Table name

// Connect to server and select database.
$link = mysqli_connect("$host", "$username", "$password", "$db_name");

// Retrieve data from database
$sql="SELECT * FROM scores ORDER BY score DESC LIMIT 10";   // The 'LIMIT                 10' part will only read 10 scores. Feel free to change this value
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysqli_fetch_array($result)){
echo $rows['name'] . "|" . $rows['score'] . "|";

// close while loop 
}

// close MySQL connection 
mysql_close();
?>

savescores.php

<?php

$db = "database";//Your database name
$dbu = "username";//Your database username
$dbp = "password";//Your database users' password
$host = "localhost";//MySQL server - usually localhost

$dblink = mysqli_connect($host,$dbu,$dbp,$db);

if(isset($_GET['name']) && isset($_GET['score'])){

 //Lightly sanitize the GET's to prevent SQL injections and possible XSS     attacks
 $name = strip_tags(mysql_real_escape_string($_GET['name']));
 $score = strip_tags(mysql_real_escape_string($_GET['score']));

 $sql = mysqli_query($dblink, "INSERT INTO `$db`.`scores` (`id`,`name`,`score`) VALUES ('','$name','$score');");
 if($sql){

      //The query returned true - now do whatever you like here.
      echo 'Your score was saved. Congrats!';

 }else{

      //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
      echo 'There was a problem saving your score. Please try again later.';

 }

}else{
 echo 'Your name or score wasnt passed in the request. Make sure you add ?    name=NAME_HERE&score=1337 to the tags.';
}

mysqli_close($dblink);//Close off the MySQL connection to save resources.
?>    

标签: mysqlphp-7.0

解决方案


分别用和替换mysql_query和。mysql_closemysqli_querymysqli_close

<?php
header('Access-Control-Allow-Origin: *');

$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="scores"; // Table name

// Connect to server and select database.
$link = mysqli_connect("$host", "$username", "$password", "$db_name");

// Retrieve data from database
$sql="SELECT * FROM scores ORDER BY score DESC LIMIT 10";   // The 'LIMIT                 10' part will only read 10 scores. Feel free to change this value
$result=mysqli_query($link, $sql);

// Start looping rows in mysql database.
while($rows=mysqli_fetch_array($result)){
echo $rows['name'] . "|" . $rows['score'] . "|";

// close while loop 
}

// close MySQL connection 
mysqli_close($link);
?>

这应该有效。


推荐阅读