gpt4 book ai didi

php - 使用 PHP 从数据库中获取数据

转载 作者:行者123 更新时间:2023-11-28 23:17:41 24 4
gpt4 key购买 nike

我有以下 PHP 代码,但它不起作用:

<?php
$pid = "test123";
$conn = mysql_connect('localhost', 'user', 'password');
mysql_select_db('database');
$result = mysql_query('SELECT name FROM my_table WHERE
channel=$pid', $conn);
$content = mysql_result($result, 0);
echo $content;
?>

我只是得到一个空白页...

有人可以帮帮我吗?!谢谢!

最佳答案

这就是我在 mysql 和 PDO 中的做法。

数据库

/*In the deprecated mysql extension*/
$pid = "test123";
/*connecting*/
$conn = mysql_connect('localhost', 'user', 'password');
mysql_select_db('database');
/*If this comes from outside you need to escape it*/
$pid = mysql_real_escape_string($pid, $conn);

/*Putting backtick around columns that are reserved words*/
$result = mysql_query("SELECT `name` FROM my_table WHERE
`channel`= '$pid'", $conn);
$content = mysql_result($result, 0);
echo $content;

PDO

/*In PDO*/
$pid = "test123";
$pdo = new PDO("mysql:host=localhost;dbname=database", 'user', 'password');
/*Putting backtick around columns that are reserved words*/
$query = "SELECT `name` FROM my_table WHERE
`channel`= :pid";
$pdo_stmt = $pdo->prepare($query);
/*Prepared statements escape it for you*/
$pdo_stmt->bindValue(":pid", $pid);
$pdo_stmt->execute();

echo $pdo_stmt->fetchColumn();

关于php - 使用 PHP 从数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142134/

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