简体   繁体   English

PHP:将 MySQL 转换为 Postgresql

[英]PHP: Translating MySQL to Postgresql

I need to translate the following MySQL sentences in PHP to Postgresql我需要将 PHP 中的以下 MySQL 语句翻译成 Postgresql

Config.php File config.php 文件

MySQL MySQL

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);

I tried: (Postgresql)我试过:(Postgresql)

$db = pg_connect($db_host, $db_username, $db_password, $db_name);

MySQL: MySQL:

$get_total_rows = 0;
$results = $mysqli->query("SELECT COUNT(*) FROM menu");

if($results){
$get_total_rows = $results->fetch_row(); 
}
//break total records into pages
$total_groups= ceil($get_total_rows[0]/$items_per_group);

I tried: (Postgresql) I don't know if I need really to change anything, I just change the name of my variable since it is different in my config.php file我试过:(Postgresql)我不知道我是否真的需要改变任何东西,我只是改变了我的变量的名称,因为它在我的 config.php 文件中是不同的

$get_total_rows = 0;
$results = $db->query("SELECT COUNT(*) FROM menu");

if($results){
$get_total_rows = $results->fetch_row(); 
}
//break total records into pages
$total_groups= ceil($get_total_rows[0]/$items_per_group);

MySQL MySQL

if($_POST)
{
//sanitize post value
$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

//throw HTTP error if group number is not valid
if(!is_numeric($group_number)){
    header('HTTP/1.1 500 Invalid number!');
    exit();
}

//get current starting point of records
$position = ($group_number * $items_per_group);


//Limit our results within a specified range. 
$results = $mysqli->prepare("SELECT id, name, message FROM paginate ORDER BY id ASC LIMIT $position, $items_per_group");
$results->execute(); //Execute prepared Query
$results->bind_result($id, $name, $message); //bind variables to prepared statement


echo '<ul class="page_result">';
while($results->fetch()){ //fetch values
    echo '<li id="item_'.$id.'"><span class="page_name">'.$id.') '.$name.'</span><span class="page_message">'.$message.'</span></li>';  
}
echo '</ul>';

$mysqli->close();
}

Have not idea how to put this into Postgresql不知道如何将其放入 Postgresql

Thank you so much非常感谢

MySQL and Postgres both use a SQL dialect, so many of your MySQL queries will work, though Postgres is a bit more strict and consistent than MySQL. MySQL 和 Postgres 都使用 SQL 方言,因此您的许多 MySQL 查询都可以工作,尽管 Postgres 比 MySQL 更严格和一致。 There's no automatic way to do this conversion, however.但是,没有自动进行这种转换的方法。 In fact, looking through your code it appears these queries should work in Postgres as well.事实上,查看您的代码似乎这些查询也应该在 Postgres 中工作。 Long term you might want to consider using an ORM to make these sorts of migrations easier.从长远来看,您可能需要考虑使用 ORM 来简化这些类型的迁移。

I'd recommend implementing unit tests for all of your queries work before making the change.我建议在进行更改之前为所有查询工作实施单元测试。 This way you can ensure correct results when you switch to Postgres.这样您就可以确保在切换到 Postgres 时得到正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM