MySQL - Use Variable column name [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have multiple tables all with different columns.
For example: Table1 has the columns (Username, Group, Car, pet). Table2 has the columns (Username, Extra_groups, Vehicle, animals)
I want to Return the username that has 'Red' anywhere in anywhere in the 2 tables minus the username field of course. Now becuase each table has different columns im describing the table to find what they are then doing the following query however it doesn't want to work.
SELECT Username FROM $Table WHERE $column LIKE '%$search%'
What is the best way to query the columns without knowing what they are going to be and omit one of them from the search.

"SELECT Username FROM " . $Table ." WHERE ".$column . " LIKE '% " . $search. "%'"

Related

How to get only a text corresponding to the id from MySQL and store it as a variable in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm a begginer to PHP and I want to know how can I fetch some text from the corresponding ID and store it as a variable in PHP.
The table is like
ID----NAME----ACCOUNT----PASSWORD
1----name1----accont1----password2
2----name2----accont2----password2
3----name3----accont3----password3
Now if I want to get the account2 as text and save it in an variable (say acc2) then what should I do. Assuming that I have connection information in connect.php.
Edit: I want to select the account2 using the ID like from ID 2 select account.
Thanks In Advance!!!
Assuming you use MySQL, the table is named users and you are using PDO, this would get what you need:
$stmt = $conn->query("SELECT * FROM users WHERE ID = 2");
$row = $stmt->fetch()
$account = $row['ACCOUNT']

how do can I add multiple rows after using Inner join on two tables [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have two tables in mysql db. The two tables are connected through a key :
five_day_general_information.days_dates_id = five_weekly_day_calander.id
SELECT
five_weekly_day_calander.Total_working_days
FROM five_day_general_information
INNER JOIN five_weekly_day_calander
ON five_day_general_information.days_dates_id = five_weekly_day_calander.id
WHERE five_day_general_information.Pro_id = 133;
enter image description here
AS YOU SEE IN THE PICTURE,
How can I add the 5 and 2 and return the results which is 7 so I can use that results in php
I think you just want aggregation:
SELECT SUM(fwdc.Total_working_days) as Total_working_days
FROM five_day_general_information fdgi JOIN
five_weekly_day_calander fwdc
ON fdgi.days_dates_id = fwd.id
WHERE fdgi.Pro_id = 133;
You can refer to the returned value as Total_working_days in the PHP result set.
Note that table aliases make the query easier to write and to read.

SQL query doesnt find exact name from database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
SELECT * FROM hge_funcionarios
JOIN hospitais
ON hge_funcionarios.hospital_id = hospitais.id_hospitais
JOIN funcoes
ON hge_funcionarios.funcao_id = funcoes.id_funcoes
WHERE nome LIKE '%$search%'
ORDER BY hospital_id DESC
When I try the exact name from the database doesnt show up any results.
If i search "Larissa" or "LARISSA", I get no results even in my database having "LARISSA CAMPOS".
If I try "lar" or anything like this I can find it, but when it gets too close to the name on database like "LARISS" I can't find it any more.
I tried collate and charset but no success.
EDIT: Its not a Query error with ambiguous column name in SQL because column names are distinct.
I'm writing this answer since it's not possible to show it in the comments. Feel free to disregard it.
The problem you are facing seems to be related to the injection of parameter values into your SQL query. The easy (dangerous) way is to simply concatenate strings, as in:
$stmt = $conn->prepare(
"select * from my_table where name = '" . $param1 . "'");
Even though it works for simple cases, your case is more complicated, and confusing. Most of the time you'll use Prepared Statements as in:
$stmt = $conn->prepare("select * from my_table where name = ?");
$stmt->bind_param("sss", $param1);
This way, the parameter will be injected the right way. In your case you'll need to prepend and append % to your parameter, since it'll be used for a LIKE operator.
WHERE nome LIKE '%$search%'
May be $ is the Reason.Try Like : WHERE nome LIKE '%search%'

Two selects to while function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have one table which need to find all date and put the name just once (this is workig = $sql), but i have problem to select the that name of data and count how much time is repeats in same table. So where is name Test(down) there needs to be count for how much time that name repeats in whole table.
If I put one more while function the table dies.
Im stacked. Im not very good with php and mysql. If someone can help.
$sql = "SELECT DISTINCT one,two FROM results";
$sql2 = "select test, count(*) as foo_count from results group by test;";
$result = mysqli_query($conn,$sql);
$result2 = mysqli_query($conn,$sql2);
<?php
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row["one"]."</td><td>".$row["two"]."</td><td>";}?> Test</td></tr>
I'm having a little bit of a hard time following your question, but I think you are looking for a single sql statement like this
$sql = "select test, count(one) as foo_count from results group by test";

Search query using php and mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Need help on how to filter search results in the search form using PHP and Mysql.
The form contains six fields Product is textbox, Category is drop down select item, Business in text box user enters and state, city are drop down select item and Landmark is textbox the user enters.
Product or Category or Business is mandatory. So the user may enter any one filed or may fill all the fields.
Now I need to get exact search result based on the input. Please Help me to solve this
Try this:
$query = "select * from table_name where 1 ";
if(!empty($_POST['field1']) ) {
$query .= " AND field1 like '".trim($_POST['field1'])."'";
}
if(!empty($_POST['field2'])) {
$query .= " AND field2 like '".trim($_POST['field2'])."'";
}
// and so on
$result = mysql_query($query);

Categories