This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I tried inserting this into my code to generate a dropdown menu through an sql table but it responds with nothing in the dropdown. When I execute the sql query it doesn't show any error as well. Please be kind enough to fix my error. Thank you
<?php
$db = mysqli_connect('localhost', 'root', '', 'registration');
$sql = "Select (unitid) from unit where (unitname)=
('$unitname')";
mysqli_query($db, $sql);
echo "<select name='unitid'>";
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" .$row['unitid']."'> ".$row['unitname'] . "</option>";
}
echo "</select>";
?>
Try saving the result of mysqli_query:
$result = mysqli_query($db, $sql);
And then using it in the while condition:
while ($row = mysqli_fetch_array($result)) {
The select should be also "Select unitid, unitname ..." to return also the unitname used in the options:
$sql = "SELECT unitid, unitname FROM unit WHERE unitname = '$unitname'";
And you should use prepared statements if you want to prevent it from SQL injection attacks.
If you want all the units to be shown on the combo change the select to:
$sql = "SELECT unitid, unitname FROM unit";
So, the code should be now:
<?php
$db = mysqli_connect('localhost', 'root', '', 'registration');
$sql = "Select unitid, unitname from unit";
$result = mysqli_query($db, $sql);
echo "<select name='unitid'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" .$row['unitid']."'> ".$row['unitname'] . "</option>";
}
echo "</select>";
?>
<?php
$db = mysqli_connect('localhost', 'root', '', 'registration');
$sql = "Select unitid, unitname from unit where unitname=\"".$unitname."\"";
$rows = array();
$return = mysqli_query($db, $sql);
while($row = mysqli_fetch_array($return, MYSQLI_ASSOC))
$rows [] = $row;
echo "<select name='unitid'>";
for($i=0; $i<count($rows); $i++)
echo "<option value='".$rows['unitid']."'> ".$rows['unitname']."</option>";
echo "</select>";
?>
you didn't fetch the array correctly, so your query basically returned an empty array. You also wrote a wrong query. This solution should work fine
Related
How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
I am trying to connect to Oracle database from the following PHP script to populate a drop down list but the script doesn't work.
Can anyone see an issue? Many thanks!
$conn = oci_connect('username', 'password', 'host');
$stid = oci_parse($conn, 'select product_id, product_name from product order by product_id');
oci_execute($stid);
$query = "select product_id, product_name from product order by product_id";
$res = mysql_query($stid);
echo "<select name = 'Product'>";
while (($row = mysql_fetch_row($res)) != null)
{
echo "<option value = '{$row['product_id']}'";
if ($selected_product_id == $row['product_id'])
echo "selected = 'selected'";
echo ">{$row['product_name']}</option>";
}
echo "";
Why are you using mysql_* to interrogate an oracle database?? I think the right function to use is oci_execute
$res = mysql_query($stid);
The above line in your code is used to query a MySQL database,not Oracle.
I wonder if anyone can help.
I am trying to echo some SQL data into a <select> form with each <option> as a different row from the table.
For example if my table has two columns 'username' and 'category' and there are two rows for the same username, with the data:
"username: test, category: test1."
& second row as:
"username: test, category: test2."
How could I echo 'test1' and 'test2' as two options in a select form?
The table name is 'testtable' so my current $sql query is ("SELECT 'category' FROM 'testtable' WHERE 'username = \'$user\'")
$user is currently set to the $_SESSION['username']
Code examples would be really helpful and much appreciated. I just cant seem to get them to echo in examples I have found on forums.
try this
<select name="your_select_name" id="your_select_id">
<option value="">Select</option>
<?php
$username = $_SESSION['username'];
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
while($row = mysql_fetch_assoc($res))
{
echo '<option value="'.$row['category'].'">'.$row['category'].'</option>';
}
?>
</select>
UPDATE 2:
For distinct category use this
$res = mysql_query("SELECT DISTINCT(`category`) as category FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
OR
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' GROUP BY category ") or die(mysql_error());
Try this:
<select>
<?php while($row = mysql_fetch_array($result)
{?>
<option><?php echo $row['category'];?></option>
<?php }?>
</select>
You got from your query all rows:
<?php
$query = "SELECT category FROM testtable WHERE username = '" . $_SESSION['username'] ."'";
$result = mysql_query($query);
$options = array();
while($row = mysql_fetch_array($result))
{
$options[] = "<option>" . $row['category'] . "</option>";
}
?>
<select>
<?php echo implode("",$options); ?>
</select>
Your select query has an error: unpaired single quote before username, and single quotes surrounding category - is that possible that was the reason you couldn't do it?
<?php
$result = mysql_query($query);
echo "<select>";
while($row = mysql_fetch_array($result))
{
echo "<option>".$row['category']."</option>";
}
echo "</select>";
?>
I want to search question from mysql using ComboBox. If I select chapter number one in ComboBox, I want to display that question which only chapter one have.
In this suppose my chapter 1 contains 2 questions, chapter 2 contains some questions and so on. When I select chapter number 1 then it doesn't display question that chapter 1 have. It will only print the last question from the last chapter. How can I solve this problem?
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q)){
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
?>
Your mistake is that you are selecting questions from last iteration of query where you select chapters.
...
while($info=mysql_fetch_array($q)){
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
...
will always select last chapter. But you have another problem. Assuming you want to show questions when user select some value from dropdown, you have to send that selection using POST/GET/AJAX back to PHP and generate results based on that selection. Something like this:
if(!isset($_POST['fname']))
{
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q)){
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
}
else
{
$sql1 = "select question from math where chapter = " . $_POST['fname'];
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
}
In your question query change the while loop according to below code:
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
$d1 = array();
while($info=mysql_fetch_array($q)){
$d1[] = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter IN ('".implode("','",$d1)."')";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
?>
make the $d1 variable as an array and change the select query of question.
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q))
{
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res))
{
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
}
?>
$sql1 = "select question from math where chapter=".$d1;
Is this Query and display info all correct? Syntax-wise.
<?php
mysql_connect("HOST", "USERNAME", "PASSWORD") or die (mysql_error ());
mysql_select_db("DATABASENAME?!?!") or die(mysql_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysql_query($strSQL) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysql_close()
?>
Use statement as
<?php
$conn=mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error ());
mysqli_select_db("DATABASENAME",$conn) or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysqli_close()
?>
Also I advice you to use Mysqli or Pdo instead of Mysql driver as it is deprecated.
There is no issue in your question or code. Syntax wise it is fine. But it is advised not to use mysql_* functions because it is deprecated.
The general way of using the code for your condition would be:
<?php
mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error());
mysqli_select_db("DATABASENAME") or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
foreach ($row as $column => $value)
echo $column . " = " . $value . "<br />";
echo "<br/>";
}
mysqli_close();
?>
So, this iterates and suppose say you have four columns, and two rows, it gives an output like:
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value