Getting Data From Multiple MySQL Tables Using PHP and mysqli

Lazenbyt-mherPresident

I am trying to draw data from multiple tables that have been indexed to relate to one another. I ran this query in MySQLWorkbench, and it ran successfully. However when I tried to run a PHP test, nothing showed up, not even for the first field. Here is my code:

<?php
$db = new mysqli('host', 'user', 'password', 'database');

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$query = "
    SELECT 
        `Contact`.`firstName`,
        `Contact`.`lastName`,
        `ssn`.`ssn`,
        `Contact`.`country`,
        `Allergies`.`allergy`,
        `Allergies`.`allergyType`,
        `Allergies_Contact`.`allergyNotes`,
        `CurrentPrescriptions`.`prescriptionName`,
        `CurrentPrescriptions`.`prescribedDate`,
        `BloodType`.`bloodType`
    FROM
        `database`.`Contact`,
        `database`.`Allergies_Contact`,
        `database`.`Allergies`,
        `database`.`ssn`,
        `database`.`CurrentPrescriptions`,
        `database`.`BloodType`
    WHERE
        `Contact`.`contactKey` = `Allergies_Contact`.`contactKey`
            AND `Allergies`.`allergiesKey` = `Allergies_Contact`.`allergiesKey`
            AND `ssn`.`contactKey` = `Contact`.`contactKey`
            AND `CurrentPrescriptions`.`contactKey` = `Contact`.`contactKey`
            AND `BloodType`.`contactKey` = `Contact`.`contactKey`;
";

$result = $db->query($query) or die($db->error.__LINE__);
if ($result = mysqli_query($db, $query)) {
    while ($row = mysqli_fetch_row($result)) {
        print(row[0]);
    }
    mysqli_free_result($result);
}
mysqli_close($db);
?>

Please tell me what I am doing wrong here, because from what I can see its formatted correctly.

Sal00m

Several things:

1.- You have two query sentences, change:

$result = $db->query($query) or die($db->error.__LINE__);
if ($result = mysqli_query($db, $query)) {

With this

$result = $db->query($query) or die($db->error.__LINE__);
if ($result !== false) {

2.- Yo made a mistake when trying to print the variable, change:

while ($row = mysqli_fetch_row($result)) {
    print(row[0]);
}

With this

while ($row = mysqli_fetch_row($result)) {
    print($row[0]); // You missed a $
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting data from multiple tables using a join

From Dev

Getting Data From Multiple Rows in PHP & MySQL

From Dev

display data from multiple tables using php

From Dev

getting data from multiple tables in single query -mysql

From Dev

Getting specific results from multiple tables in mysql

From Dev

How to retrieve data from multiple tables using a PHP form?

From Dev

How to retrieve data from multiple tables using a PHP form?

From Dev

how to get data from multiple tables in sql using php

From Dev

Echo data from two different mysql tables using php

From Dev

MYSQL getting data from three tables

From Dev

PHP mysqli search multiple tables

From Dev

Chart.js - Getting data from database using mysql and php

From Dev

How efficient to retrieve multiple values from multiple tables from mysql database using php

From Dev

PHP - Live Search using multiple MySQL tables

From Dev

MySQL query from multiple tables with an array (PHP)

From Dev

PHP MySQL newest records from multiple tables

From Dev

MySQL query from multiple tables with an array (PHP)

From Dev

Selecting nested data from multiple MYSQL tables

From Dev

MySQL: Select Data from Multiple Tables

From Dev

query Fetch data from multiple tables MySQL

From Dev

joining data from multiple mysql tables

From Dev

PHP SELECT data from multiple tables

From Dev

MySQL - Delete from multiple tables using a UNION?

From Dev

Returning multiple results from multiple joins across several tables using mysql/php

From Dev

Returning multiple results from multiple joins across several tables using mysql/php

From Dev

Display data from two tables - PHP mySQL

From Dev

multiple products filter from mysql data using php

From Dev

Insert data from multiple array form inputs using php and mysql

From Dev

storing data from multiple select form to MYSQL using PHP

Related Related

  1. 1

    Getting data from multiple tables using a join

  2. 2

    Getting Data From Multiple Rows in PHP & MySQL

  3. 3

    display data from multiple tables using php

  4. 4

    getting data from multiple tables in single query -mysql

  5. 5

    Getting specific results from multiple tables in mysql

  6. 6

    How to retrieve data from multiple tables using a PHP form?

  7. 7

    How to retrieve data from multiple tables using a PHP form?

  8. 8

    how to get data from multiple tables in sql using php

  9. 9

    Echo data from two different mysql tables using php

  10. 10

    MYSQL getting data from three tables

  11. 11

    PHP mysqli search multiple tables

  12. 12

    Chart.js - Getting data from database using mysql and php

  13. 13

    How efficient to retrieve multiple values from multiple tables from mysql database using php

  14. 14

    PHP - Live Search using multiple MySQL tables

  15. 15

    MySQL query from multiple tables with an array (PHP)

  16. 16

    PHP MySQL newest records from multiple tables

  17. 17

    MySQL query from multiple tables with an array (PHP)

  18. 18

    Selecting nested data from multiple MYSQL tables

  19. 19

    MySQL: Select Data from Multiple Tables

  20. 20

    query Fetch data from multiple tables MySQL

  21. 21

    joining data from multiple mysql tables

  22. 22

    PHP SELECT data from multiple tables

  23. 23

    MySQL - Delete from multiple tables using a UNION?

  24. 24

    Returning multiple results from multiple joins across several tables using mysql/php

  25. 25

    Returning multiple results from multiple joins across several tables using mysql/php

  26. 26

    Display data from two tables - PHP mySQL

  27. 27

    multiple products filter from mysql data using php

  28. 28

    Insert data from multiple array form inputs using php and mysql

  29. 29

    storing data from multiple select form to MYSQL using PHP

HotTag

Archive