php n mysql .. query problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shubhangi
Forum Newbie
Posts: 4
Joined: Wed Jul 08, 2009 6:46 am

php n mysql .. query problem

Post by shubhangi »

I hav written following:

<?php
Require ("dbconnection.php");
$i = 1;
$group=$_POST["Group"];
$stream=$_POST["Stream"];
$certification=$_POST["Certification"];

$sql="SELECT GROUP_NAME
FROM emp_data
WHERE Group_id = '$group'
AND Stream_id = '$stream'
AND certification = '$certification'";
echo $sql;
if (!mysql_query(sql,$db))
{
die('Error Occured: ' . mysql_error());
}

$result = mysql_query( $sql );
$num_of_rows = mysql_num_rows($result);

?>

<form name="frm1" action= "Emp_Certi.php" >

<?php
echo "<table border='1'>";
echo "<tr><td><B>Groups</B></td></tr>";
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($tech_result))
{
echo "<tr>";
?>
<input type="radio" name="radiobutton[]" value="radiobutton">
<?php
echo "<td>" . $row['GROUP_NAME'] . "</td>"; echo "<br>";
echo "</tr>";
$i=$i+1;
}
}
else
echo "No groups found";
?>
<input type="submit" value="Submit" name="Submit" />
<?php
echo "</table>";
echo "</form>";
mysql_close($db);
?>

this is not wrking :(

the output as
SELECT GROUP_NAME FROM emp_data WHERE Group_id = 'tttttt' AND Stream_id = 'yyyyy' AND certification = 'xxxxx'Error Occured: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sql' at line 1

i m using php n mysql ... nt able to write proper sql wch can run in a php script..
please help
hw cld i chg it?
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: php n mysql .. query problem

Post by SvanteH »

shubhangi wrote:I hav written following:

Code: Select all

<?php
Require ("dbconnection.php");
$i = 1;
$group=$_POST["Group"];
$stream=$_POST["Stream"];
$certification=$_POST["Certification"];
 
$sql="SELECT GROUP_NAME
FROM emp_data
WHERE Group_id = '$group'
AND Stream_id = '$stream'
AND certification = '$certification'";
echo $sql; 
if (!mysql_query(sql,$db))
      {
        die('Error Occured: ' . mysql_error());
      }
 
$result = mysql_query( $sql );
$num_of_rows = mysql_num_rows($result);
 
?>
 
<form  name="frm1" action= "Emp_Certi.php" >
   
<?php
echo "<table border='1'>";
echo "<tr><td><B>Groups</B></td></tr>";
    if (mysql_num_rows($result) > 0) 
    {
        while($row = mysql_fetch_array($tech_result))
          {
              echo "<tr>";
              ?>
               <input type="radio"  name="radiobutton[]" value="radiobutton">
                           <?php
              echo "<td>" . $row['GROUP_NAME'] . "</td>"; echo "<br>";
              echo "</tr>";
              $i=$i+1;
          }
    }
    else
    echo "No groups found";
?>
<input type="submit" value="Submit" name="Submit" />
<?php
echo "</table>";
echo "</form>";
mysql_close($db);
?>
this is not wrking

the output as
SELECT GROUP_NAME FROM emp_data WHERE Group_id = 'tttttt' AND Stream_id = 'yyyyy' AND certification = 'xxxxx'Error Occured: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sql' at line 1

i m using php n mysql ... nt able to write proper sql wch can run in a php script..
please help
hw cld i chg it?
Please use the [ php ] tag.

Try this

Code: Select all

$sql = sprintf("SELECT group_name FROM emp_data WHERE group_id = %d AND stream_id = %d AND cerification = '%s'", $group, $stream, $certification);
also

Code: Select all

if (!mysql_query(sql,$db))
is wrong, change to

Code: Select all

if (!mysql_query($sql,$db))
Also may I suggest not querying twice but only once and store the result?
shubhangi
Forum Newbie
Posts: 4
Joined: Wed Jul 08, 2009 6:46 am

Re: php n mysql .. query problem

Post by shubhangi »

Thx for the solution. it worked just due to u.
Post Reply