PHP错误连接到数据库

时间:2015-12-23 04:41:16

标签: php mysql database

背景资料: 我正在尝试创建一个TODO App

<?php 
require_once 'app/init.php';

$itemsQuery = $db->prepare("
SELECT id, name, done
FROM items
WHERE user = :user
");
$itemsQuery->execute([
'user' => $_SESSION['user_id']
]);

$items = $itemsQuery->rowCount() ? $itemsQuery : [];
foreach ($items as $item) {
echo $item['name'], '<br>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Darshil Patel</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head> 
<body>
    <div class="list">
        <h1 class="header">To do.</h1>

    <ul class="items">
        <li>
            <span class="item">Pick Up Shopping</span>
            <a href="#" class="done-button">Mark as done</a>
        </li>
    </ul>

    <form class="item-add" action="add.php" method="post">
        <input type="text" name="name" placeholder="Add a New Item"         class="input" autocomplete="off" required>
        <input type="submit" value="Add" class="submit">
    </form>
</div>

INIT.PHP:

<? php

session_start();

$_SESSION['user_id'] =  1;

$db = new PDO('msql_dbname= u352634928_list;host=mysql.hostinger.co.uk', 'mysql.hostinger.co.uk', 'password');

if(!isset($_SESSION['user_id'])) {
die('You are not signed in');
}

我不确定为什么会收到此错误:

  

致命错误:未捕获的异常'PDOException',消息'无效   数据源名称'在/home/u352634928/public_html/app/init.php:7堆栈   追踪:#0 /home/u352634928/public_html/app/init.php(7):   PDO-&gt; __ construct('msql_dbname = u35 ...','u352634928_list','password')

     

1 /home/u352634928/public_html/index.php(2):require_once('/ home / u35263492 ...')#2 {main}抛出

     第7行的 /home/u352634928/public_html/app/init.php

4 个答案:

答案 0 :(得分:0)

简单的拼写错误:

更改

locationMarker.appearAnimation = kGMSMarkerAnimationPop
locationMarker.icon = UIImage(named: "testImage") as UIImage?

<? php 

基本上,所有PHP语句都以半冒号<?php 结束。

在您的情况下,您的PHP以;开头,您在<?之前添加了空格。

因此,php是一个声明,并未以php结束。

这就是解析错误背后的原因。

答案 1 :(得分:0)

您在exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 0.1f); <?之间留了一个空格。所以在第一次做php

答案 2 :(得分:0)

fData <- reactive({ data <- hustonCrime if (input$offenseFilter != "All"){ data <- subset(hustonCrime, offense %in% input$offenseFilter) } if (input$monthFilter != "All"){ data <- subset(hustonCrime, month %in% input$monthFilter) } }) 更改为<? php,删除其间的空格。

require_once

pdo

答案 3 :(得分:0)

这是连接Mysql数据库的正确语法:

$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);

给你

$db = new PDO("mysql:host=mysql.hostinger.co.uk;dbname= u352634928_list", 'mysql.hostinger.co.uk', 'password');