I want to select and display the Column Name of my table. I used SHOW SQL Command to display the data to my select box. The problem is I want to omit Two(2) columns, ID and Image column to be exact. I have no problem with displaying all the column names. Please refer to my code below for more details.
PHP Code:
<?php
require "connect.php";
$sql = "SHOW columns FROM tblLocation";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($result)) {
echo "<option value='".$row['Field']."'>".ucfirst($row['Field'])."</option>";
}
?>
use an if statement:
if ($row['Field'] != 'ID' && $row['Field'] != 'Image') {
echo "<option value='".$row['Field']."'>".ucfirst($row['Field'])."</option>";
}
Related
I created table in php and want to show only some of column names in this table. I used SHOW Statement, but it shows all columns. This is my code:
$result = mysqli_query($con, "SHOW COLUMNS FROM habits ");
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' .$row['Field'].'</td>
</tr>';
}
}
How can show only some of them ? Does anybody know the solution?
By the way, SELECT Statement doesn't work for this code. It shows this:
Notice: Undefined index: Field
in:\xampp\htdocs\diagnoVisProject\HabitsPharmacotherapies.php on line 70
If you know the names of the columns you'd want to show you could add an array with those names. Then before you print a row you'd verify if the name of the column is in the array. If it isn't you don't print the row.
Something like this:
$result = mysqli_query($con, "SHOW COLUMNS FROM habits ");
$allowedColums = ["column1", "column2", "etc."];
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
if (in_array($row['Field'], $allowedColums)) {
echo '<tr>
<td scope="row">' . $row['Field'] . '</td>
</tr>';
}
}
}
You can use
SELECT column_name FROM information_schema.tables WHERE table_name = 'habits' AND (column_name='your column 1' OR column_name='your column 2')
But I'm not sure what would youdo with it if you already know the column name
I have two table one is storing basic info with image type is blob and
the other table is for attendance table , Now my problem is when i select all
the table with basic info with image and insert it to another table the image
column is only blank the rest has a value , Hope you can help fix it thanks.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
} else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image) VALUES ('$rfid','$id','$name0','$course0','$image')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
Instead of executing two separate queries I will suggest you to have a single query which will select from one table and insert into another table.
Example query:
INSERT INTO table2 (st_id,uid,changed,status,assign_status)
SELECT st_id,from_uid,now(),'Pending','Assigned'
FROM table1
NOTE: It's not good to save images in database. We usually keep them in folders and save the path of it in database.
The simplest way to solve your error is by doing like this
$image=mysqli_real_escape_string($mysqli,$row['image'];
$mysqli is connection variable.
and then in your query use $image to insert image hope it will helps.
I created table in php and want to show only some of column names in this table. I used SHOW Statement, but it shows all columns. This is my code:
$result = mysqli_query($con, "SHOW COLUMNS FROM habits ");
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' .$row['Field'].'</td>
</tr>';
}
}
How can show only some of them ? Does anybody know the solution?
By the way, SELECT Statement doesn't work for this code. It shows this:
Notice: Undefined index: Field
in:\xampp\htdocs\diagnoVisProject\HabitsPharmacotherapies.php on line 70
If you know the names of the columns you'd want to show you could add an array with those names. Then before you print a row you'd verify if the name of the column is in the array. If it isn't you don't print the row.
Something like this:
$result = mysqli_query($con, "SHOW COLUMNS FROM habits ");
$allowedColums = ["column1", "column2", "etc."];
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
if (in_array($row['Field'], $allowedColums)) {
echo '<tr>
<td scope="row">' . $row['Field'] . '</td>
</tr>';
}
}
}
You can use
SELECT column_name FROM information_schema.tables WHERE table_name = 'habits' AND (column_name='your column 1' OR column_name='your column 2')
But I'm not sure what would youdo with it if you already know the column name
If i use the following line in PHP the MySQL query works perfectly fine:
$query = "
SELECT *
FROM customers_1
JOIN customers_2
ON customers_1.id = customers_2.cus_id;
";
but when i try to just get two single fields like this:
$query = "
SELECT customers_1.product,customers_1.id
FROM customers_1
JOIN customers_2
ON customers_1.id = customers_2.cus_id;
";
it doesnt work at all, means i do not get any output. When i copy and paste the second query and put it into PHPMyAdmin it works perfectly fine.
What i do afterwards is this:
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
if($row['id']=="3")
{
foreach($row as $field)
{
echo $field;
}
}
}
mysql_close($dbhandle);
Because $row['id'] is nothing in your code because you just fetch one column from your query SELECT customers_1.product.. and that column is product
And no need foreach loop . You just get your value in single while loop
Updated Answer after question updated
while ($row = mysql_fetch_array($result)) {
if (!empty($row['id'])) {
echo $row['id'];
}
}
Note:- Mysql is deprecated instead use mysqli or PDO
You are comparing id in your code while your query is retrieving customers_1.product.
I want to use the following code to populate a drop-down list with all of the customer types:
<select name="type" id="type" class="neutral">
<?php // SQL QUERY TO RETRIEVE EVERY TYPE OF CUSTOMER
$sql = "SELECT CUST_TYPE FROM `CUSTOMER` GROUP BY `CUST_TYPE`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){echo '<option value="'.$row.'">'.$row.'</option>';}
?>
The query works in phpMyAdmin; it gives the correct output (corporate, other, school, sports) but in the webpage it displays a drop-down list with 4 options, all containing the word "array." Please help!
Try
while($row = mysql_fetch_assoc($result)){
echo '<option value="'.$row['CUST_TYPE'].'">'.$row['CUST_TYPE'].'</option>';
}