使用 PHP 从 SQL Server 中选择数据 [英] Selecting data from SQL Server Using PHP

查看:39
本文介绍了使用 PHP 从 SQL Server 中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP 从 PC 上的本地数据库中选择数据,但是当我运行 127.0.0.1/test.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.

错误:

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

这是我的 PHP 脚本:

Here is my PHP script:

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

我在网上环顾四周,但我无法弄清楚这里出了什么问题.我可以运行一个 hello world php 脚本并且它可以工作,所以我假设它与数据库的连接问题但是我在这里错过了什么?谢谢

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? Thanks

额外:

我有:

推荐答案

来自 文档

这个扩展在 PHP 5.5.0 中被弃用,在 PHP 中被移除7.0.0.相反,应使用 MySQLi 或 PDO_MySQL 扩展.

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.

您可能想要使用 Microsoft 为 SQL Server 提供的 PHP 驱动程序 而不是 PDO 中的 ODBC 驱动程序.

You'll probably want to use Microsoft's drivers for PHP for SQL Server rather than the ODBC driver in PDO.

这篇关于使用 PHP 从 SQL Server 中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆