How do I capture search index in MySQL using PHP? - php

I do have table table1 with two columns say name and details when I search by details there were multiple name listing. I want to retrieve that name list and display to a webpage using PHP. how can I do that?

the most basic page.php:
<?php
$sql = mysql_query('SELECT name FROM table1 WHERE details = "something"');
$result = array();
while($row = mysql_fetch_assoc($sql)){
array_push($result,$row['name']);
echo $row['name'].'<br />';
}
print_r($result);

Related

how can I select and display all the rows from the same user, underneath each other with php

Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo $data['workout'];
}
Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.
As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo "<button>$data['workout']</button></br>";
}
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' Group By username";

searching a table in a database with result from another table

hi i am trying to write a php to grab a specific category of post in a wordpress database, this is what i got so far:
$q = "select * from table1 where column like 'condition'";
$r = mysql_query($q);
$id = array();
if($r){
while ($row = mysql_fetch_array($r)) {
$link = $row["object_id"];
$id[] = array(
"postid"=>$link,
);
}
}
else{
echo mysql_error();
}
now i am trying to use the result i got from pervious code to search another table to get something else. i am new to php so i am hoping to get some help
thanks in advance

Search for form element in database multiple times

I am trying to learn php and came across a task which i have need help with.
Background information - I have a postgresql database which has
multiple tables.
What I need - When I enter anything in the form on my HTML page, i
need to extract all information from all the tables that contain
information related to what I have entered.
Example - Suppose I enter food poisoning in the form. I need to access
all the tables and extract the different information related to food
poisoning.
My code: (the connection part is not being posted as it works fine)
<?php
$result = pg_prepare($dbh, "Query1", 'SELECT * FROM Project.bacteria WHERE disease = $1');
// if (pg_numrows($result) == 0) {
// $result = pg_prepare($dbh, "Query1", 'SELECT * FROM Project.virus WHERE disease = $1');
// }
//$sql = "SELECT * FROM Project.bacteria WHERE disease=";
//$result = pg_query($dbh, $sql);
$result = pg_execute($dbh, "Query1", array($disease));
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
//$rows = pg_fetch_all($result)
/*// iterate over result set
// print each row*/
while ($row = pg_fetch_array($result)) {
echo $row[0]." ".$row[1]. "<br />";
}
?>
For the above code, when I enter food poisoning it searches just one table i.e bacteria and returns the related information ( here as a test i have taken just information at row position 1 and row position 2.)
Since there are multiple tables, like a table drugs that stores information of drugs used to cure food poisoning, i would want to extract that information from the respective table.
Any help would be appreciated.
try this one
SELECT * FROM bacteria WHERE disease = ''
UNION ALL
SELECT * FROM drugs where desease = ''
but i think the best way is to normalize you tables. :)

Populate drop-down list with MySQL query in PHP

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>';
}

Printing a single field below

Suppose I do a select like this:
select company, location, acnum, bills from table
Just how do i output data using php so that the company and location remain at the top and every bill is printed and the acnum is displayed at the bottom. I'm using mysql_fetch_assoc and have gone mad trying. I can go tru every record but cant get one at the top and bottom.
I'm trying to get data out like this:
Company name here Location here
Bill
Bill
Bill
Bill
Acnum goes here.
P.S I'm new to PHP. I used to program in ASP before.
If you have the company ID you can do something like this:
$sql = 'SELECT company, location, acnum
FROM Companies
WHERE id = '.$id
$result = mysql_query($sql);
$company = mysql_fetch_assoc($result)) {
mysql_free_result($result);
$sql = 'SELECT bills
FROM Companies
WHERE id = '.$id
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$bills[] = $row;
}
mysql_free_result($result);
echo $company['company'] . $company['location'];
foreach($bills as $bill) {
echo $bill['bills'];
}
echo $company['acnum'];

Categories