怎样用PHP连接到 STEEMSQL 数据库steemCreated with Sketch.

in #cn7 years ago (edited)

dev.php.png

读过justyy的R Tutorial - Connecting to STEEMSQL - R 教程之 怎么样连接到 STEEMSQL 数据库了解到steem的数据库用的MS SQLServer可以用ODBC来连接。哈哈,有福了。感谢@justyy的帖子。

抽空试了一下用PHP+ODBC连接并且查询steemsql,可以工作,记录一下。

1)安装php-odbc,freetds, unixODBC,我用的是fedora,直接yum install php-odbc freetds unixODBC即可,其他发行版的linux类似
2)配置freetds,添加下面内容:
[steemDB]
host = sql.steemsql.com
port = 1433
tds version = 8.0
2.1)配置odbcinst.ini,添加下面内容
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/libtdsodbc.so
Setup = /usr/lib/libtdsS.so
UsageCount = 1
2.2)配置odbc.ini,添加下面内容:
[steemDB]
Description = MSSQL Server
Driver = freetds
Database = DBSteem
ServerName = steemDB
TDS_Version = 8.0
保存所有的配置文件,然后开始码代码,比如文件名叫queryInfo.php。

<?php
$data_source='steemDB';
$user='steemit';
$password='steemit';

// Connect to the data source and get a handle for that connection.
$conn=odbc_connect($data_source,$user,$password);
if (!$conn){
    if (phpversion() < '4.0'){
      exit("Connection Failed: . $php_errormsg" );
    }
    else{
      exit("Connection Failed:" . odbc_errormsg() );
    }
}

// This query generates a result set with one record in it.
$sql="select voting_power from Accounts where name='justyy'";

// Execute the statement.
$rs=odbc_exec($conn,$sql);

// Fetch and display the result set value.
if (!$rs){
    exit("Error in SQL");
}
while (odbc_fetch_row($rs)){
    $col1=odbc_result($rs, "voting_power");
    echo "$col1\n";
}

// Disconnect the database from the database handle.
odbc_close($conn);
?>

在linux命令行里面执行php queryInfo.php,会看到用justyy查询到的结果是5496(这个值是会变化的),除以100,得到的voting_power应该是54.96,把代码中的justyy换成alanzheng得到的是8657 :-)

使用PHP的朋友可以试一下。

Sort:  

Congratulations it's beautiful @alanzheng . Must feel great - that is a lot of work!

赶紧去试一试先

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 61937.20
ETH 3004.64
USDT 1.00
SBD 2.49