Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本php问题-连接到数据库_Php_Mysql_Database Connection - Fatal编程技术网

基本php问题-连接到数据库

基本php问题-连接到数据库,php,mysql,database-connection,Php,Mysql,Database Connection,我刚开始研究php,连接到远程数据库并返回数据。我一直在遵循一个教程,但是当我应用相同的代码时(就像在教程中一样),我得到了 服务器错误 网站在检索时遇到错误。它可能因维护而停机或配置不正确。 以下是一些建议: 稍后重新加载此网页 这是脚本-数据库连接很好,我相信是检索数据导致了错误,但不知道如何修复它 <?php // Database credentials $host = "localhost"; $db = "json"; $uid = "json

我刚开始研究php,连接到远程数据库并返回数据。我一直在遵循一个教程,但是当我应用相同的代码时(就像在教程中一样),我得到了

服务器错误

网站在检索时遇到错误。它可能因维护而停机或配置不正确。 以下是一些建议: 稍后重新加载此网页

这是脚本-数据库连接很好,我相信是检索数据导致了错误,但不知道如何修复它

  <?php

   // Database credentials
   $host = "localhost"; 
   $db = "json"; 
   $uid = "json"; 
   $pwd = "json1";

    // Connect to the database server   
    $link = mysql_connect($host, $uid, $pwd) or die("Could not connect");

   //select the json database
   mysql_select_db($db) or die("Could not select database");


// Create an array to hold our results
   $arr = array();
   //Execute the query
   $rs = mysql_query("SELECT id,userid,firstname,lastname,email FROM users");

   // Add the rows to the array 
   while($obj = mysql_fetch_object($rs)) {
   $arr[] = $obj;

?>

在脚本末尾,您有以下内容:

// Add the rows to the array 
   while($obj = mysql_fetch_object($rs)) {
   $arr[] = $obj;

?>

您在
行中打开
{
;但从未关闭它。

在脚本末尾,您有以下内容:

// Add the rows to the array 
   while($obj = mysql_fetch_object($rs)) {
   $arr[] = $obj;

?>

您在<<代码> < < /C> >行时打开<代码> {<代码>;但永远不要关闭它。

您可以考虑使用MySqLyFixChyAsSoCo()代替MySqLFixChixObjor()。一个命名数组通常比一个更随机的对象更容易处理。

添加了mysql错误的显示

$link = mysql_connect($host , $uid, $pwd);
if (!$link) {
    echo "Error connection to database. MySQL said: ";
    echo mysql_error();
    exit;
}
if (!mysql_select_db($db)) {
        echo "Couldn't select database.";
}

$rs = mysql_query("SELECT id,userid,firstname,lastname,email FROM users WHERE 1=1");

$users = array();
if ($rs && mysql_num_rows($rs) > 0) {
    while($userRow =  mysql_fetch_assoc($rs)){
        $users[] = $userRow; 

        // Or do what you want with the user data
         $id = $userRow['id'];
         $userId = $userRow['userid'];
         $firstname= $userRow['firstname'];
         $lastname= $userRow['lastname'];
         $email= $userRow['email'];

    }
}

可以考虑使用MySqLyFuffChyAsSoCo()代替MySqLyFutChyObjor()。命名数组通常比一个更随机的对象更容易处理。

添加了mysql错误的显示

$link = mysql_connect($host , $uid, $pwd);
if (!$link) {
    echo "Error connection to database. MySQL said: ";
    echo mysql_error();
    exit;
}
if (!mysql_select_db($db)) {
        echo "Couldn't select database.";
}

$rs = mysql_query("SELECT id,userid,firstname,lastname,email FROM users WHERE 1=1");

$users = array();
if ($rs && mysql_num_rows($rs) > 0) {
    while($userRow =  mysql_fetch_assoc($rs)){
        $users[] = $userRow; 

        // Or do what you want with the user data
         $id = $userRow['id'];
         $userId = $userRow['userid'];
         $firstname= $userRow['firstname'];
         $lastname= $userRow['lastname'];
         $email= $userRow['email'];

    }
}

是的,对不起,我也发现了!谢谢:D(我会在时间限制到期后将你标记为)是的,对不起,我也发现了!谢谢:D(我会在时间限制到期后将你标记为)我猜你的服务器配置为不显示代码错误。尝试添加
错误报告(E_ALL ^E_STRICT);ini_set('display_errors','on'))
在脚本的开头。您可能会得到一个
致命错误…
,因为代码末尾的while循环缺少
}
。添加
}
以关闭while循环。我认为您的PHP配置不正确。我总是使用回显错误/警告/通知的配置。我猜您的服务器配置为不显示代码错误。尝试添加
错误报告(E\u ALL ^E\u STRICT);ini_设置(“显示错误”、“打开”)在脚本的开头。您可能会得到一个
致命错误…
,因为代码末尾的while循环缺少
}
。添加
}
以关闭while循环。我认为您的PHP配置不正确。我总是使用回显错误/警告/通知的配置。