Selecting the values of columns in a sql subquery - mysql

i'm wanting to display the actual header_titles (a row in each of my tables) from all the tables in my database
My current query
select column_name, table_name from information_schema.columns
where column_name in (
select column_name from information_schema.tables
where table_schema='site'
) and
column_name='header_title';
At the moment the query returns 'header_titles' 12 times (the amount of tables in my db with the field, and obviously the table names. I'm not sure how to get the values of each of the header_titles

A very trivial example
UPDATE - column values are now being printed on the screen
$query = "SELECT table_name FROM information_schema.tables AS tbl
WHERE EXISTS(SELECT 1 FROM information_schema.columns AS col
WHERE tbl.table_name = col.table_name AND column_name = 'header_title')";
$result = mysqli_query($conn, $query) or trigger_error(mysqli_error($conn), E_USER_ERROR);
while($row = mysqli_fetch_row($result))
{
$q = 'SELECT header_title FROM '.$row[0];
$r = mysqli_query($conn, $q) or trigger_error(mysqli_error($conn), E_USER_ERROR);
$index = 0;
while($data = mysqli_fetch_row($r))
{
$index++;
echo "Table = ".$row[0].", row = ".$index.", HEADER_TITLE = ".$data[0].PHP_EOL;
}
}

Related

Update same tables from from select value from same table mysql

i am trying to update a table column which same column from same table select.
Here is the code (updated)
public function UpdateStockIn($id, $subUnitValue) {
$query = "UPDATE BRAND_LIST SET CURRENT_STOCK_BOTTLE = (SELECT CURRENT_STOCK_BOTTLE FROM BRAND_LIST WHERE ID = ?) + '.$subUnitValue.' WHERE ID = ? ";
$success = 0;
try {
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $id);
$stmt->execute();
$success = 1;
} catch (PDOException $ex) {
echo $ex->getMessage();
}
return $success;
}
it Show error like this
You can't specify target table 'BRAND_LIST' for update in FROM clause
Try run these 2 sqls, The first one will store a value into mysql local variable then use into 2nd sql.
SELECT #old_subUnitValue := GROUP_CONCAT(table1.CURRENT_STOCK_BOTTLE) FROM BRAND_LIST AS table1 WHERE table1.ID=2;
UPDATE BRAND_LIST AS table2 SET table2.CURRENT_STOCK_BOTTLE = #old_subUnitValue + '.$subUnitValue.' WHERE table2.ID=2;
Use the below query
$query = "UPDATE BRAND_LIST SET CURRENT_STOCK_BOTTLE = CURRENT_STOCK_BOTTLE + ".$subUnitValue." WHERE ID = ?";

Could not insert to database after fetching the column names from the query

I am trying to make the clone of selected table row. The columns are stored in mapping table and are dynamic. I get the column_name from the first query which is to be used in second query to insert the selected result.
In this case I am getting error as Unknown column '$matches'.
db = new PDO("...");
$statement = $db->query("select column_name from mapping_table");
$list = $statement->fetchAll(PDO::FETCH_COLUMN);
$matches = implode('`,`', $list);
$db1 = new PDO("...");
$db1->query("insert into tbl_user(`$matches`) (SELECT `$matches` FROM tbl_user WHERE id= :id)");
$db1->bindParam(':id', $id);
$result= $db1->execute();

How to fetch the column names for the query

In this project, I have a dynamic columns which I stored in mapping table. Now usnig following query, I can fetch all the column names I need.
$db = new PDO("...");
$statement = $db->prepare("select column_name from mapping_table");
$list = $statement->fetch();
$matches = implode(',', $list);
Now from this result, I need to make a query. My question is how can I pass this $matches to the column_name of second query.
$db1 = new PDO("...");
$qry1= $db1->prepare("select {$matches} from table1");
$result= $qry1->fetch();
Try something like this:
db = new PDO("...");
$statement = $db->query("select column_name from mapping_table");
$list = $statement->fetchAll(PDO::FETCH_COLUMN);
$matches = implode('`,`', $list);
And then:
$db1 = new PDO("...");
$qry1= $db1->query("select `$matches` from table1");
$result= $qry1->fetch();

What is faster way to retrieve column constraints in tables

In order to validate the integrity of data entered into forms by users, one test I must perform is to ensure that the data does not exceed the length of the table column that will hold it. Rather than hard-coding in the maximum length values for the various columns in my tables, I thought it would be more efficient to write a routine that would gather all the relevant constraint information from my tables (here, just the data_type and character_maximum_length) and put it into an associative array in which I could simply retrieve the values with easily writable and readable:
$table_info_array[$table_name][$column_name]['character_maximum_length'];
I found a solution, and it works rapidly on my localhost, but the construction of the $table_info_array is unacceptably slow on my live server. Here is what I coded:
/* Skipped here: set up your PDO $connection first, then: */
// Define your database:
$database_name = "my_database";
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '" . $database_name ."'";
$stmt = $connection->prepare($sql);
$result = $stmt->execute();
$counter = -1;
$table_array = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
foreach ($row as &$value) {
$counter++;
$table_array[$value] = $counter;
}
}
$table_info_array = array();
foreach ($table_array as $table_name => $table_value) {
// Here I'm selecting the data_type and character_maximum_length by column_name, but other information could easily be collected:
$sql = "SELECT column_name, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = '" . $table_name . "'";
$stmt = $connection->prepare($sql);
$result = $stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$table_info_array[$table_name][$row['column_name']]['data_type'] = $row['data_type'];
$table_info_array[$table_name][$row['column_name']]['character_maximum_length'] = $row['character_maximum_length'];
}
}
This will show everything; notice how concise the results are:
print_r($table_info_array);
The syntax for retrieving table column constraints is therefore:
$table_info_array[$table_name][$column_name]['data_type'];
$table_info_array[$table_name][$column_name]['character_maximum_length'];
For example, now I can retrieve the maximum character length on the 'title' column in my 'artwork' table, like this:
$table_info_array['artwork']['title']['character_maximum_length'];
The construction of the $table_info_array works at lightning speed on my localhost, but on my live server it is unreasonably slow. Is there a faster way to do this?

number of tables in database always returns 1

everybody is suggesting to use SELECT COUNT(*) FROM information_schema.tables
if I do the following, I always get '1' returned:
$res=mysql_query("SELECT COUNT(*)
FROM information_schema.tables WHERE table_schema = 'usr_web275'");
$number_of_tables_in_database=mysql_num_fields($res);
echo $number_of_tables_in_database;
Do the following:
$res=mysql_query("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'usr_web275'");
//Check if any row is returned or not.
if (!$res) {
echo "No data found";
}
else
{
$row = mysql_fetch_assoc($res); //Returns the row of the result
$number_of_tables_in_database = $row["count(*)"];
echo $number_of_tables_in_database;
}