简体   繁体   English

使用PHP进行MySQL数据库操作

[英]MySQL database manipulation with PHP

Spent the last few days researching this, but I think I'm just not getting it... I can usually do what I need to with PHP but am new to MySQL 花了近几天的时间对此进行研究,但我认为我没有得到...我通常可以使用PHP来完成我需要做的工作,但对MySQL来说却是新手

I set up a MySQL database to hold some photos. 我设置了一个MySQL数据库来保存一些照片。 The photo's are in separate galleries (gallery is denoted by a gallery field). 照片位于单独的画廊中(画廊由画廊字段表示)。 The photo's are also indexed by an id number. 照片还通过ID号索引。

When displaying the photos (it all works perfectly up to now...), I would like to be able to jump to the next or previous photo in the gallery, but can't just us the next or previous id, as it could be from a different group (found that out the hard way ;) 在显示照片时(到目前为止,所有功能都可以完美使用...),我希望能够跳至图库中的下一张或上一张照片,但不能只使用下一张或上一张ID,因为它可以来自不同的小组(发现困难的方式;)

from my research, I feel like I need to use this: 从我的研究中,我觉得我需要使用这个:

$sql = "SELECT id FROM gphoto WHERE gallery='$g' ORDER BY id DESC";
$query = mysqli_query($db_conx, $sql);

But $query doesn't seem to give me what I expect. 但是$ query似乎没有给我我期望的东西。 It feels like $query it should contain an array if all id's which contain gphoto, and I should be able to just find my current id number, then jump one up or down, but when I try to read $query I get: 感觉好像$ query如果所有id都包含gphoto的话,它应该包含一个数组,我应该能够找到我当前的id号,然后向上或向下跳转一个,但是当我尝试读取$ query时,我得到了:

Cannot use object of type mysqli_result as array 无法将类型为mysqli_result的对象用作数组

I'm obviously misunderstanding something Some people have suggested: $result = mysqli_fetch_assoc($query); 我显然误解了一些人的建议:$ result = mysqli_fetch_assoc($ query);

I had tried this after reading the online manual extensively, but for some reason it only lists one item... in this case the last record. 在广泛阅读在线手册后,我尝试过此操作,但由于某种原因,它仅列出了一项...在这种情况下,它是最后一条记录。

If I run it as ASC, it lists the first. 如果我将其作为ASC运行,它将列出第一个。 Should it be listing all records like I expect, or is it a different command? 它应该像我期望的那样列出所有记录,还是不同的命令?

C) C)

+1 for Fabio's response. 为法比奥的回应+1。

A good advice is to use PDO interface for accessing databases in PHP. 一个好的建议是使用PDO接口访问PHP中的数据库。 It's a meaningful interface to construct, execute and fetch the results of your queries without the knowledge of the used db driver. 这是一个有意义的接口,可在不了解所用数据库驱动程序的情况下构造,执行和获取查询结果。

http://php.net/manual/en/book.pdo.php http://php.net/manual/en/book.pdo.php

You need to fetch data from database after executing your query to handle mysqli object you have just created 执行查询以处理刚创建的mysqli对象后,需要从数据库中获取数据

$query = mysqli_query($db_conx, $sql);
$result = mysqli_fetch_assoc($query);

Now you have the array you were looking for in your variable $result 现在您有了要在变量$result中查找的数组

if ($result = mysqli_query($db_conx, $query)) {
    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        //mysqli_fetch_assoc fetches data by row
    }
}

You are only getting 1 row, thats how mysqli_fetch_assoc works. 您只得到1行,这就是mysqli_fetch_assoc的工作方式。 If you want the entire result to be an array of the rows, use mysqli_fetch_all 如果您希望整个结果是行的数组,请使用mysqli_fetch_all

I would recommend using PDO however like @ceadreak suggested. 我建议使用PDO,但建议使用@ceadreak。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM