Kohana v3 database, how to get table structure? - php

How can I using KohanaPHP framework and database module get mysql table structure?
I've tried this:
$query = DB::query(NULL, 'DESCRIBE table_name');
$result = $query->execute();
But it only returns number of columns in table, and foreach loop failed.
Is there any other way to get table structure or how can I update code above to works properly?

Try this:
$query = DB::query(NULL, 'SHOW FULL COLUMNS FROM table_name');
$result = $query->execute();
EDIT
You need to specify the type of query of DB::query() will just return the number of affected rows.
$query = DB::query(Database::SELECT, 'SHOW FULL COLUMNS FROM table_name');
$result = $query->execute();
This will give you the result you expect.

Related

Select Data From Multiple MySQL Tables

Im searching for a function in MySQL in order to Select rows from multiple tables that have a similar name. For example: Proyect_1, Proyect_2, Proyect_3
All of the tables have the same column names, the only difference between the tables is the table name. It starts with the prefix 'proyect'. The issue is that the program doesn´t know how many 'proyect' tables there are, so i can´t make a list of them and select data like always
I need something like this:
SELECT mydata FROM TABLES LIKE 'Proyect_%';
Any ideas? Thanks!
To get all tables with a common prefix
SHOW TABLES LIKE 'Proyect_%';
This will return rows of tables that matched the prefix. Example:
Proyect_1
Proyect_2
Proyect_3
In PHP you can create a UNION query that will pick up the tables returned by the above query,
$sql = "SHOW TABLES LIKE 'Proyect_%'";
$result = $conn->query($sql);
$dataQuery = array();
$query = "";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_array(MYSQLI_NUM)) {
$dataQuery[] = "SELECT * FROM {$row[0]}";
}
$query = implode(' UNION ', $dataQuery);
}
echo $query;
if you want to search for all tables with name like Proyect then you can get from MySQL information schema.
SELECT * FROM information_schema.tables
From here you can find table by table name

display the last id from a colomn - ZendFramwork

I want to display the last ID from a column called id_client, I'm using ZendFramework.
This is how i write my query in php:
$select = $this->getDbTable()->select('ID_CLIENT');
$select->order('ID_CLIENT DESC');
$result = $this->getDbTable()->fetchAll($select);
$result = $result[0];
var_dump($result);
The problem is that it returns a select * and I do not understand why knowing that I specify that I had to select only id_client :
Anyone as an idea?
I think this is ZF 1.x. Column selection is done in "from" method. Your query should be something like this:
// $db should be a Zend_Db instance
$select = $db->select()
->from(
array('alias' => 'table_name'),
array('ID_CLIENT')
);
$select->order('ID_CLIENT DESC');
$result = $this->getDbTable()->fetchAll($select);
$result = $result[0];
var_dump($result);
Good ol' Zend Framework 1.
If I understand it right, you are trying to retrieve only the id_client of the last row of that table.
If so, you have to sort the results by id_client in descending order and then retrieve the first line with the method current(). Don't forget that the result is an object (Zend_Db_Table_Row) and it has to be accessed properly.
$select = $this->getDbTable();
$select->from('TABLE_NAME'); // Not really necessary
$select->columns('ID_CLIENT');
$select->order('ID_CLIENT DESC');
$result = $this->getDbTable()->fetchAll($select)->current();
$lastIdClient = $result->id_client;

PHP and OCI8 running subquery on a dataset

I'm not sure if I am asking this the correct way, but I want to run a query on an Oracle database, fetch the result, and then run additional queries on that result dataset. Is that possible? I am trying to avoid another call to the database.
$query = "SELECT * FROM MY.TABLE ";
$stid = oci_parse($connection, $query);
oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS_OCI_RETURN_LOBS)) {
//my code for the full dataset
}
Then I would like to do something like
PSUEDO-CODE
$newDataset = runThisQuery("SELECT * FROM [oci dataset from above(what is that syntax?)] WHERE my_value = 1");
while($newRow = loop through $newDataset){
//my code for the subquery
}
Any suggestions?
To further describe my problem: I am getting a table of fields, and from that table I would like to extract the unique values of certain fields into their own php arrays.

Using mysql_num_rows with multiple tables?

Here's the code I've started with:
$result = mysql_query("SELECT * FROM apis_hashes_a", $link);
$count = mysql_num_rows($result);
What I need to do is to get the total sum of all rows for all tables that start with apis_hashes_.
There are tons of tables and new ones are being added all the time, but all of these tables start with apis_hashes_ at the beginning. Is this something that would be possible to do or do I have to list every table individually in the PHP code?
JUST use SUM() IN SQL.Use the code below
<?php
$query = "SELECT SUM(TABLE_ROWS) as score
FROM INFORMATION_SCHEMA.TABLES
WHERE SCHEMA = '{your_db_name}'";
$result = mysql_query($query, $link);
while($row=mysql_fetch_array($result)){
$total_rows = $row['score'];
}
echo $total_rows;
?>
Hope this helps you
The sql:
show tables like 'apis_hashes_%'
will return the list of all your tables, then you need loop thru all the names of the tables with the mysql sum() function.

CodeIgniter multiple database query

I tried to run query on multiple database with following sql query:
// Collect clients db name
$dbs = array();
foreach($cafes as $cafe)
{
$dbs[] = $cafe->db_name; // client_7
}
// Run multi database query
foreach($dbs as $db)
{
$this->db->select("$db.orders.*,(select count($db.order_items.id) from $db.order_items where $db.order_items.siparis_id = orders.id) as urun_sayisi, (select sum($db.order_items.tutari) from $db.order_items where $db.order_items.siparis_id = orders.id) as price");
}
$this->db->get()->result();
In above example, i have cafe clients and each client have own db named ex: client_7. I am trying to do list all client orders. I got following error with above query:
Unknown table 'client_7' in field list
How can list all rows on orders table from multiple database?
Please mention database name ahead your table name..
for example
SELECT userid FROM db1.user;
SELECT userid FROM db2.user;
Try this:
<?php
$secound_db= $this->load->database('database_two', TRUE);
$query = $secound_db->get('person');
var_dump($query);
Source

Categories