PHP的MySQL获取的结果,显示 [英] PHP MySQL fetch result and display

查看:87
本文介绍了PHP的MySQL获取的结果,显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在试图在这里多学习一些PHP和MySQL,和我在理解如何工作的问题。我不知道其他的语言,所以我希望我能够理解PHP更好,如果我能得到一些帮助,这个例子:

Trying to learn some more PHP and MySQL here now, and I'm having problems understanding how this works. I do know other languages, so I'm hoping I will understand PHP much better if I just can get some help with this example:

我有一个表称为文件。这包含3行(3档),在那里我定义FILEID,thumbURL,fullRUL,和类似的东西。

I have a table called files. This contains 3 rows (3 files), where I've defined fileID, thumbURL, fullRUL, and stuff like that.

在我的网页我想取这整个的项目/文件列表,以及循环来显示他们都做了。

on my webpage I want to fetch this whole list of items/files, and do a for loop to display them all.

这是我的PHP code,这当然会重演3倍,但它显示了相同的信息,因为它只使用了相同的行:

This is my PHP code, which of course repeats itself 3 times but it displays the same information, because it only uses the same row:

    <div id="contentWrapper">
    <?php
    for($i = 0; $i < $numItems; $i++){
        echo "<div id='contentItem'>";
        echo "<a href='".$row['fullURL']."' title='".$row['description']."'><img src='". $row['thumbURL']."' width='200px' height='150px' /></a>";
        echo "</div>";
    }
    ?>
</div>

这是code,我从数据库中获取数据:

This is the code where I get the data from the database:

    <?php
$db = mysql_connect("localhost", "xxxxx", "xxxxx");
mysql_select_db("login",$db) or die("Kan ikke koble til databasen");

$filesSQL = mysql_query("SELECT * FROM files");
$numItems = mysql_num_rows($filesSQL);

$sql="SELECT * FROM `files` WHERE fileID=1";
$result=mysql_query($sql);

if (false === $result) {
echo mysql_error();
}

$row=mysql_fetch_array($result, MYSQL_BOTH);
?>

这里目前例子并只取firsst行并存储在一个数组,所以这就是为什么我得到所有3项相同的值。

The current example here does only fetch the firsst row and stores that in an array, so thats why I get the same values on all 3 items.

不过,我已经使用$ filesSQL [$ i] ['thumbURL']和其他类似methodes试过,但我不能得到它的工作。

But I've tried using $filesSQL[$i]['thumbURL'] and other similar methodes, but I cant get it to work.

我想这是一个非常基本的问题,所以我希望的答案,这将帮助我了解如何与数据库,数组和显示它的工作。

I guess this is a very basic question, so I'm hoping for an answer that will help me understand how to work with databases, arrays and displaying it.

我可以很容易做到这一点的ActionScript3或其他语言,但不是在PHP! = S

I can easily do this in Actionscript3 or other languages, but not in php! =S

谢谢! = D

推荐答案

,请尝试使用:

while($row = mysql_fetch_assoc($result)) {
       echo "<div id='contentItem'>";
       echo "<a href='".$row['fullURL']."' title='".$row['description']."'><img src='". $row['thumbURL']."' width='200px' height='150px' /></a>";
       echo "</div>";
}

在这种情况下, mysql_fetch_assoc 基本上是做同样的事情, mysql_fetch_array($结果,MYSQL_BOTH)无数字键。这是给你,你用真有。

In this case, mysql_fetch_assoc is doing basically the same thing as mysql_fetch_array($result, MYSQL_BOTH) without the numeric keys. It's up to you which you use there really.

对于未来的数据库交互,不过,我建议你阅读到任何的MySQLi PDO 的mysql 现在preFIX并利用它的功能是去precated。

For future database interactions, though, I'd recommend reading into either MySQLi or PDO as the mysql prefix and functions using it are now deprecated.

编辑:

由于所要求的少数用户,使用更新的MySQLi函数,而不是一个例子:

As requested by a few users, an example using the updated MySQLi functions instead:

<?php
$db = new mysqli('localhost', 'user', 'pass', 'db');
if($db->connect_error) {
    die("Connection error: ".$db->connect_error);
}
$filesSQL = $db->query("SELECT * FROM files");
$numItems = $db->num_rows; //this can also be done procedurally as mysqli_num_rows($filesSQL)
$sql = $db->query("SELECT * FROM files WHERE fileID = 1");
if(!$sql) {
    echo $db->error;
}
//personally, if I wanted an SQL error after a query I would use:
$sql = $db->query('query') or die($db->error);

//then to follow, your while loop becomes
while($row = mysqli_fetch_assoc($sql)) { //can also be done with object as $row = $sql->fetch_assoc
    //do your loop
}

可悲的是,我不知道PHP模板在评论你的常识的建议,这样的例子对于那些欢迎!

Sadly, I'm not aware of PHP templates as suggested by Your Common Sense in the comments, so examples for that are welcome!

这篇关于PHP的MySQL获取的结果,显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆