Heim  >  Fragen und Antworten  >  Hauptteil

Wie kann ich eine Verbindung zu einer Tabelle herstellen und deren Inhalt anzeigen? (MySQL und PHP)

Ich habe zwei MySQL-Tabellen (Produkt und Kategorie). Ich habe einige Scheindaten in beiden Tabellen. Jetzt muss ich die Kategorie irgendwie mit dem Produkt verknüpfen. Beispiel: Ein Produkt mit der ID 1 sollte Folgendes zurückgeben:

| product name | category   |
| Monitor      | Technology |

Ich weiß, dass ich das schon einmal gemacht habe, aber heute kann ich anscheinend keine Lösung finden.

Bearbeiten Hier ist, was ich bisher habe. Die Verbindung funktioniert einwandfrei und ich kann die Daten in der Tabelle anzeigen.

<?php
// Include database connection
include("connection.php");

// Create variables for later use
$db = $conn;
$tableName = "Produkte";
$columns= ['id_product', 'name_product'];

// Create variable to use in index.php
$fetchData = fetch_data($db, $tableName, $columns);

// The function below feteches data from the tables specified and checks if the colums are emtpy by any chance.
function fetch_data($db, $tableName, $columns) {
    // Check db connection
    if (empty($db)) {
        $message= "Database connection error";
    }
    // Check if the columns variable is empty and not an array by any chance
    elseif (empty($columns) || !is_array($columns)) {
        $message="Product Name must be defined in an indexed array";
    }
    // Check if table name is empty
    elseif (empty($tableName)) {
        $message= "Table Name is empty";
    }
    // Else proceed as usual.
    else {
        $columnName = implode(", ", $columns);
        // The query needs to be repalced. Today my SQL stuff is leaving me a bit.
        $query = "SELECT p.".$columnName." AS product, c.name_category FROM $tableName p JOIN Kategorie c ON c.id_";
        $result = $db->query($query);
        if ($result== true) {
            if ($result->num_rows > 0) {
                $row= mysqli_fetch_all($result, MYSQLI_ASSOC);
                $message= $row;
            }
            else {
                $message= "No Data Found";
            }
        }
        // Throw error if error occures
        else{
            $message= mysqli_error($db);
        }
    }
    return $message;
}

Tabelle products 只有 2 列。 id 列和 product_name Spalten.

P粉738821035P粉738821035180 Tage vor371

Antworte allen(1)Ich werde antworten

  • P粉014293738

    P粉0142937382024-04-04 00:33:33

    基本技术有所不同:

    // create DBquery using JOIN statement 
    $query = "
    SELECT 
        p.name AS product, c.name AS category 
    FROM products p
    JOIN categories c ON c.id = p.category_id;";
    
    // get DB data using PDO
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    
    // show table header
    printf('| product name | category   |' . PHP_EOL);
    
    // loop for output result as table rows
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        printf('| %-12s | %10s |' . PHP_EOL, $row['product'], $row['category']);
    }

    在线尝试

    Antwort
    0
  • StornierenAntwort