简体   繁体   English

使用PHP从SQL Server选择数据

[英]Selecting data from SQL Server Using PHP

I am trying to select data from a local database on my PC using PHP but i am getting this error when i run 127.0.0.1/test.php which is what the file is called. 我正在尝试使用PHP从PC上的本地数据库中选择数据,但是运行127.0.0.1/test.php时出现此错误,这就是所谓的文件。

error: 错误:

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in 
C:\xampp\htdocs\test.php:11 Stack trace: #0 {main} thrown in 
C:\xampp\htdocs\test.php on line 11

Here is my PHP script: 这是我的PHP脚本:

  <?php
    $servername = "Windows local servername";
    $username = "Windows username";
    $password = "windows password";
$dbname = "dbname";

// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn){
    die('error connecting to database');
}else{
    mysql_select_db(dbname, $conn);
}

$sql = "SELECT UserId, UserEmail, UserPassword FROM User";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["UserId"]. " - UserEmail: " . $row["UserEmail"]. " " . $row["UserPassword"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

I have looked around online but i cant figure out what is wrong here. 我已经在网上四处张望,但无法弄清楚这里出了什么问题。 I can run a hello world php script and it works so i am assuming its a connection issue with the database but what have i missed here? 我可以运行一个hello world php脚本,它可以工作,所以我假设它与数据库存在连接问题,但是我在这里错过了什么? Thanks 谢谢

EXTRA: 额外:

i have: 我有:

在此处输入图片说明

Try to run 尝试跑步

phpinfo() 

And look for the mysql extensions, in ubuntu you can install them by typing 并查找mysql扩展,在ubuntu中,您可以通过键入以下内容进行安装

sudo apt-get install php-pdo-mysql

Anyway the instruction you're trying to use was deprecated in HP 5.5.0 and deleted in PHP 7.0.0 so may be this is why its missing... depending on you PHP version, you could try with the mysqli extension instead like: 无论如何,您要使用的指令在HP 5.5.0中已弃用,而在PHP 7.0.0中已删除,因此这可能就是它丢失的原因...根据您的PHP版本,您可以尝试使用mysqli扩展名,例如:

$mysqli = mysqli_connect("example.com", "user", "password", "database");
$res = mysqli_query($mysqli, "SELECT 'Please, do not use ' AS _msg FROM DUAL");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];

You could get more info on the actual way of performing this with PHP7 at http://php.net/manual/en/mysqli.quickstart.dual-interface.php for instance 例如,您可以在http://php.net/manual/en/mysqli.quickstart.dual-interface.php上获得有关使用PHP7执行此操作的实际方法的更多信息。

From the docs 来自文档

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 此扩展在PHP 5.5.0中已弃用,在PHP 7.0.0中已删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。

You'll probably want to use Microsoft's drivers for PHP for SQL Server rather than the ODBC driver in PDO. 您可能要使用Microsoft的PHP for SQL Server驱动程序,而不是PDO中的ODBC驱动程序。

In place of $conn = mysql_connect($servername, $username, $password, $dbname); 代替$ conn = mysql_connect($ servername,$ username,$ password,$ dbname); use $connectioninfo=array("Database"=>"dbname", "UID"=>"userid", "PWD"=>"password", ); 使用$ connectioninfo = array(“ Database” =>“ dbname”,“ UID” =>“ userid”,“ PWD” =>“ password”,);

$con = sqlsrv_connect($server,$connectioninfo); $ con = sqlsrv_connect($ server,$ connectioninfo);

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

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