简体   繁体   中英

how to pull data from different table and display it in different html table on a same page using php?

I have to display data from two different MySql tables in a same page in different HTML tables using PHP, I have two different tables as events and members, I have to pull data from these by their data.

<?php
 include_once "dbcon.php"; 
 // Check connection
 if(!$link)
 {
     die('not connected');
 }
 $link=  mysqli_query($link, "SELECT * FROM members WHERE status='1' AND dob BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");
 if(!$eventlink)
 {
     die('not connected');
 }

 $eventlink=  mysqli_query($eventlink, "SELECT * FROM events WHERE status='1' AND edate BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");
?> 

And in htmli will display the data using while loop, is this correct ?

But it was not working, it always displays "not connected", I don't know what I did wrong.

Try this below code, it was added the undefined variable $eventlink so that's why it shows you always not connected .

<?php
include_once "dbcon.php"; 
// Check connection
if(!$link)
  {
      die('not connected');
  }
  $link=  mysqli_query($link, "SELECT * FROM members WHERE status='1' AND dob BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");

  $eventlink=  mysqli_query($eventlink, "SELECT * FROM events WHERE status='1' AND edate BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");
?> 

Here it looks like the checked variables are set after they are checked.

To help you it would be needed to know how link and eventlink may look like after line 2. So make it a working example by giving the content of dbcon.php (empty/alternative access data) ans also the strukture of your tables (dump with just example data).

Or maybe start with a tutorial, eg from w3schools and learn step by step.

*edit:

the right chapter for you to start is: https://www.w3schools.com/php/func_mysqli_connect.asp

there is a function to check the connection:

// Check connection
if ( mysqli_connect_errno() ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

The rest was answered by others already.

Also consider to read documentation on the functions you want to use, eg http://php.net/manual/en/function.mysqli-connect.php . There you will find small examples and related functions.

This is working fine...

<?php
        include_once "dbcon.php"; 
        // Check connection

       $link=  mysqli_query($link, "SELECT * FROM members WHERE status='1' AND dob BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");
         if(!$link)
               {
                   die('not connected');
               }

        $eventlink=  mysqli_query($eventlink, "SELECT * FROM events WHERE status='1' AND edate BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");

    if(!$eventlink)
               {
                   die('not connected');
               }

        ?> 
    <?php
include_once "dbcon.php"; 
// Check connection
if(!$link)
  {
      die('not connected');
  }
  $link=  mysqli_query($link, "SELECT * FROM members WHERE status='1' AND dob BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");

  $eventlink=  mysqli_query($link, "SELECT * FROM events WHERE status='1' AND edate BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");
?>

i fixed the issue with the above code, i should have used the $link instead of $eventlink inside the mysqli_query function in the second query, this fixed my issue, thank you guys for trying to help .......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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