gpt4 book ai didi

php postgres pdo 获取和选择查询

转载 作者:行者123 更新时间:2023-11-29 13:30:04 25 4
gpt4 key购买 nike

我是 pdo 和 php 的新手,所以请多多包涵。

我想要的是与数据库建立安全连接并获取数据。但是正如我检查的那样,甚至没有数据库连接。当我检查元素时,我发现了这个:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

我检查过它,我想我知道它是什么意思,但我无法修复它

这里是我的代码,

global $dbname = $_GET['dbname'];
global $table = $_GET['table'];
global $yerId= $_GET['gid'];

try {
$db = new PDO("pgsql:dbname=$dbname;host=localhost", "postgres", "postgres" );
$sql = $db->query ("SELECT km, turu, hat_kesimi, ili, ilcesi, mahadi FROM $table WHERE gid = $yerId ");
while($result = $sql-> fetch(PDO_FETCH_ASSOC) ) {
printf ('<table><thead><tr><th colspan="4" align="left" >TCDD 3.BOLGE MUDURLUGU <img src="tcdd.png" align="right" width="92px" /></th></tr><tr><th colspan="4">Hemzemin Gecitler ve Ozellikeri</th></tr></thead><tbody><tr><th>Kilometre</th><td colspan="3">%s</td></tr><tr><th>Turu</th><td colspan="3">%s</td></tr><tr><th>Hat Kesimi</th><td colspan="3">%s</td></tr><tr><th>Sehir</th><td colspan="3">%s</td></tr><tr><th>Ilce</th><td colspan="3">%s</td></tr><tr><th>Mahalle</th><td colspan="3">%s</td></tr><tr><th colspan="4" > copyright © all rights reserved by Piri Reis Bilisim </th></tr></tbody></table>', $result["km"],$result["turu"], $result["hat_kesimi"], $result["ili"], $result["ilcesi"],$result["mahadi"]);
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

最佳答案

当我通过 PHP -l 运行您的代码时,我收到以下错误消息:

PHP Parse error: syntax error, unexpected '=', expecting ',' or ';' in test.php on line 2 Errors parsing test.php

把这里的global关键字去掉就行了,错了:

$dbname = $_GET['dbname'];
$table = $_GET['table'];
$yerId= $_GET['gid'];

关于php postgres pdo 获取和选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25973647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com