Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm not much familiar with PHP. So its really difficult to find the best way to do my work. However I did the task. But I couldn't design this table using css. Think there's something wrong in my code. Please help me.

I want to write the table tags in html, inside the abc.php file without designing it inside db_connect file using php tags.

The file(abc) which I have calling the function in db_connect file

PHP
<?php


include('a_header.php');
include_once('classes/class.database.php');
	$db = new db();
	?>

<div id="table1 >

    <?php
    echo "<table border='1' width='300px' >
        <tr>
            <th>Name</th>
            <th>Email</th>
        </tr>";
   
    ?>
    
</div>


        <?php
	$res2 = $db->get_patients();
        
	
include('a_footer.php');?>


This is the function I have written inside the db_connect file.
PHP
<?php
class db{
	private $con;
	function __construct(){
		$this->con = mysql_connect('localhost','root','');
		mysql_select_db('mask',$this->con);
	}

         function get_patients($name=NULL,$email=NULL,$username='gayani'){
            $query2 = "select distinct a.name, a.email from patient a,doctor_patient b, doctor c where c.username='".$username."' and c.username=b.doc_name and b.serial_no=a.serial_no" ;
                    . "  and name='".$name."';";

	    $res2 = mysql_query($query2,$this->con);
                
            while($row2 = mysql_fetch_array($res2)){
                    echo "<tr>";
                    echo "<td> <a href='v_patient.php'/>" . $row2['name'] . "</td>";
                    echo "<td>" . $row2['email'] . "</td>";
                    echo "</tr>";      
		}
                
            echo "</table>";
	}



I want to write the table tags in html, inside the abc.php file without designing it inside db_connect file using php tags. I tried it. But its not working. Help me guys.
Posted
Updated 21-May-14 15:32pm
v2
Comments
Peter Leow 21-May-14 21:56pm    
What errors have you encountered? already I have spotted some syntax errors?

1 solution

Try the following:
in the db_connect.php file
class db
{
     private $con;
     function __construct()
    {
        $this->con = mysql_connect('localhost','root','');
        //mysql_select_db('mask',$this->con);
    }

    public function getDbConnection(){
        return $this->con;
    }
}

in the abc.php file, syntax errors corrected in $query2 and href
<?php
// $res2 = $db->get_patients();

    require_once 'db_connect.php';
    $db = new db();
    $con = $db->getDbConnection();

    mysql_select_db('mask', $con );

    // How you get the value for $name, $username ??????

            $query2 = "select distinct a.name, a.email from patient a,doctor_patient b, doctor c where c.username='".$username."' and c.username=b.doc_name and b.serial_no=a.serial_no and name='".$name."'";
 
	    $res2 = mysql_query($query2,$con);
                
            while($row2 = mysql_fetch_array($res2)){
                    echo "<tr>";
                    echo "<td> <a href="v_patient.php">" . $row2['name'] . "</a></td>";
                    echo "<td>" . $row2['email'] . "</td>";
                    echo "</tr>";      
           }
                
            echo "";
	}

// other code
?>

However, you should upgrade your php to version 5 and above and use mysqli and prepared statement for your sql query: prepared-statements-in-php-and-mysqli/[^] to prevent sql injection.
 
Share this answer
 
v2
Comments
gayani dassa 21-May-14 23:01pm    
For the moment username is defined as "gayani". Inside the database there is a user called "gayani". After this step I will correct it to get data of logged user.

name is a variable of patient table and Im assigning the value of that variable to $name.

db connection is okay. I want to pass data from db_connect file to abc.php file with the output data from the above query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900