Jump to content

Query Multi Variables To Mysql Using Php


fraz_pk

Recommended Posts

Hello friends,

 

I am working on php to improve my web development skills. I have created a web based university system using php and mysql. However, I got stuck in coding multi variables to query mysql using php. The multi variables are placed in mysql_query() with AND operator as shown below.

 

$query3= mysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent' AND Intake='$xmenu'");

 

I could be able to query single variable however, face problems with multiple variables. The codes below works but when I query $xboth the output displays three tables showing $query, $query2 and $query3.

 

I WANT TO DISPLAY ONLY $QUERY3 when passing $xboth.

 

 

 

<?php

include 'Lecture/connection.php';

 

$xstudent=$_POST['stuname'];

$xmenu=$_POST['drop'];

$xboth= $_POST['stuname'] AND $_POST['drop'];

 

 

$query= mysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent'");

$query2= mysql_query("SELECT * FROM Student WHERE Intake='$xmenu'");

$query3= mysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent' AND Intake='$xmenu'");

 

 

if($xstudent)

{

$fields_num = mysql_num_fields($query);

// Display table

echo "<table border='1'><tr>";

for($i=0; $i<$fields_num; $i++)

{

$field = mysql_fetch_field($query);

echo "<td>{$field->name}</td>";

}

echo "</tr>\n";

 

while($row = mysql_fetch_row($query))

{

echo "<tr >";

// $row is array... foreach( .. ) puts every element

// of $row to $cell variable

foreach($row as $cell)

echo "<td width= '30' >$cell</td>";

echo "</tr>\n";

}

 

}

 

 

if($xmenu)

{

$fields_num2 = mysql_num_fields($query2);

// Display table

echo "<table border='1'><tr>";

for($i=0; $i<$fields_num2; $i++)

{

$field2 = mysql_fetch_field($query2);

echo "<td>{$field2->name}</td>";

}

echo "</tr>\n";

 

while($row2 = mysql_fetch_row($query2))

{

echo "<tr >";

// $row is array... foreach( .. ) puts every element

// of $row to $cell variable

foreach($row2 as $cell2)

echo "<td width= '30' >$cell2</td>";

echo "</tr>\n";

}

}

 

if($xboth )

{

$fields_num3 = mysql_num_fields($query3);

// Display table

echo "<table border='1'><tr>";

for($i=0; $i<$fields_num3; $i++)

{

$field3 = mysql_fetch_field($query3);

echo "<td>{$field3->name}</td>";

}

echo "</tr>\n";

 

while($row3 = mysql_fetch_row($query3))

{

echo "<tr >";

// $row is array... foreach( .. ) puts every element

// of $row to $cell variable

foreach($row3 as $cell3)

echo "<td width= '30' >$cell3</td>";

echo "</tr>\n";

}

}

 

 

Please click below to check the screenshot of the output.

 

http://farazi14.webs.com/query.jpg

 

http://farazi14.webs.com/query2.jpg

 

http://farazi14.webs.com/query3.jpg

 

 

THANKS GUYS :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.