我的数据库中有以下列:http://i.stack.imgur.com/oMYJC.png
我如何才能让它读取该列并将example.com写入我的站点?:
echo "Your website: " . $website;请注意,我是php的初学者。
发布于 2015-05-11 22:15:19
由于您是PHP新手,因此首先要通过PHP了解数据库连接。请查看以下内容:http://php.net/manual/en/mysqli.quickstart.connections.php
成功连接数据库后,尝试如何通过PHP查询数据库:http://php.net/manual/en/mysqli.quickstart.statements.php
示例代码:
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$mysqli->real_query("SELECT website FROM my_table");
$res = $mysqli->use_result();
while ($row = $res->fetch_assoc()) {
echo " Website = " . $row['website'] . "\n";
}发布于 2015-05-11 22:19:40
下面是一个例子:
$query = "SELECT website FROM TableName WHERE YourCondition";
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
echo "Your website: ". $row[0];
}
$result->free();
}点击这里查看mysqli::multi_query:
http://php.net/manual/en/mysqli.multi-query.php
并在此处阅读mysqli的完整文档:
发布于 2015-05-11 22:25:58
首先,您需要建立到MySQL数据库的连接。为此,我建议使用PDO。有关这方面的更多信息,请查看http://php.net/manual/en/book.pdo.php。
要建立与PDO的连接,请尝试以下操作:
$pdo = new PDO('mysql:host=YOUR_MYSQL_SERVER;dbname=YOUR_DB_NAME',$username,$password);然后编写一个查询,从您的数据库中获取数据。在您的示例中:
$sql = 'SELECT website FROM your_table_name';然后执行查询:
$query = $pdo->prepare($sql);
$query->execute();
$website_list = $query->fetchAll(PDO::FETCH_ASSOC);$website_list现在包含保存在该列中的所有网站的关联数组。
然后将它们打印到您的网站上:
foreach($website_list as $site){
print 'Your website: '.$site['website'].'<br/>';
}https://stackoverflow.com/questions/30169834
复制相似问题