I have a code who should read all the rows from a mysql database table.
However, it doesn't show all the data from the table.
The $array should contains all the rows , but contains only the first.
I have three row in my module_cmds database
+--------+------+----+--------------------+
| module | hash | id | serverId |
+--------+------+----+--------------------+
| music | 1 | 1 | 231870365863903232 |
| rule34 | 1 | 2 | 231870365863903232 |
| logs | 1 | 3 | 231870365863903232 |
+--------+------+----+--------------------+
FYI, count($reponses) = 1
Try to change the ORDER BY
FYI, The $array, is use previously in the code, but I don't think it change something
$array = array();
$reponses = $bdd->query('SELECT * FROM module_cmds WHERE serverId=' . $donnees['server_id'] . ' ORDER BY module');
while ($donnee = $reponses->fetch()){
$array_param = array();
$reponses = $bdd->query('SELECT * FROM module_params WHERE idModule=' . $donnee['id']);
while ($donne = $reponses->fetch()){
$array_param_in = array( 'param' => $donne['paramName'],
'value' => $donne['value']);
array_push($array_param,$array_param_in);
}
$array_in = array( 'module' => $donnee['module'],
'hash' => $donnee['hash'],
'params' => $array_param);
array_push($array,$array_in);
}
For an ORDER BY module the answer is
"modules":[{"module":"logs","hash":"1","params":[]}]
For an ORDER BY id the answer is
"modules":[{"module":"music","hash":"1","params":[]}]
The answer should be
"modules":[{"module":"logs","hash":"1","params":[]},
{"module":"music","hash":"1","params":[]},
{"module":"rule34","hash":"1","params":[]},]```
I think the mistake come from the SQL, but
'SELECT * FROM module_cmds WHERE serverId = 231870365863903232 ORDER BY module'
work fine.
Where is my mistake ? Is it something that I don't know about array in Php ? Some typo ? Some line I forget ?
FYI, I have a similar code where I don't go into in another SQL Table, and it work perfectly there.
Try changing the $reponses in the second while loop.
Related
I am creating a multilingual website and I want to display a different data for each country.
So i already had created script which determines the language and origin of the user and I need to load data from 3 databases on the website in foreach function.
I had 3 database
listFrom, listFromCountryCode, listFromLang
listFrom:
id, prefix, status
listFromCountryCode:
id, idList, countryCode, status,
listFromTranslate:
id, idList, name, langCode, status
currently, to get this data I need to do a loop for listFromCountryCode and listFrom to get prefix. I came up with some optimizations such as use INNER JOIN in query. I cant connect these database in one piece because prefix for other countries is the same. I haven't done this code yet because at the beginning I wanted to ask more experienced developers for advice on how to optimize it. I know that the use of a loop for database queries is bad but I have no idea for another solution.
Update
Already created script and time-tested so when in table is more elements (100) site loaded in 3s. Normal script without that script loaded in 0.46s. Anyone got any idea how to optimize this script?
public function checkCountryCodeList()
{
$sql = "SELECT COUNT(*) FROM listFromCountryCode WHERE countryCode = :code AND status = :status";
$array = array(':code' => $this->countryCode, ':status' => $this->status);
$result = $this->db->selectCount($sql, $array);
if($result > 0){
return true;
}else{
return false;
}
}
public function getCountryCodeList()
{
$sql = "SELECT idList
FROM listFromCountryCode
WHERE countryCode = :code";
$array = array(':code' => $this->countryCode);
$result = $this->db->select($sql, $array);
return $result;
}
public function getList()
{
if(!$this->checkCountryCodeList()){
$this->countryCode = DEFAULT_COUNTRYCODE;
}
$countryCodeList = $this->getCountryCodeList();
$data = array();
foreach($countryCodeList as $row){
$sql = "SELECT listFrom.prefix, listFromTranslation.name
FROM listFrom, listFromTranslation
WHERE listFrom.id = listFromTranslation.idList
AND listFrom.id = :id
And listFromTranslation.langCode = :code";
$array = array(
':id' => $row['idList'],
':code' => $this->langCode
);
$result = $this->db->select($sql, $array);
$data[] = ['name' => $result[0]['name'], 'prefix' => $result[0]['prefix']];
}
return $data;
}
Update
Some example data
listFrom
| id | prefix |status |
|--- | ------ |-------|
| 1 | btc |1 |
| 2 | psc |1 |
listFromCountryCode
| id | idList |countryCode |status |
|--- | ------ |------------|------ |
| 1 | 1 |de |1 |
| 2 | 2 |de |1 |
listFromTranslate
| id | idList| name |langCode |status |
|--- | ------| ----------- |---------|------ |
| 1 | 1 | bitcoin |en |1 |
| 2 | 2 | paysafecard |en |1 |
How do I complete this code below? I am trying to select news from the database and the locations which can be 23,22,88,90 location codes. I know you can just do IN('23', '22', '88', '90') but the problem is, my locations are a string so it comes out like IN('23,22,88,90') I believe.
How do I expand on the string of locations and select all or any including the locations in the string? So in the database, newsLocations could be 22 23 22,90 23,80,90 90. If that makes sense? so if $locationstoGet has 22,88,90 only, it will get the newsLocation even if the result is just 88,90 without the 22.
$locationsToGet = '22,88';
$db->query("SELECT * FROM news WHERE newsLocation IN($locationstoGet)");
I hope I explained this alright.
I saw a response on another site here
So I will adapt the solution there to your scenario. Change locationsToGet into an array, and use the implode function to generate the right syntax for the IN Clause.
$locationsToGetArr = array('22','88');
$locations = "'".implode("','",$locationsToGetArr)."'"; //this should output '22','88'
$db->query("SELECT * FROM news WHERE newsLocation IN($locations)");
This solution is assuming your database structure is as such
+--------+--------------+
| news | newsLocation |
+--------+--------------+
| 1 | 88 |
| 1 | 22 |
| 2 | 22 |
| 2 | 88 |
+--------+--------------+
But if you are storing your data as the following instead
+--------+--------------+
| news | newsLocation |
+--------+--------------+
| 1 | 88,22 |
| 2 | 22,88 |
+--------+--------------+
You will not have much choice besides to select all from news table and have PHP filter the location. Not a very efficient method.
If your data is comma separated stored in databse column then you can use MYSQL FIND IN SET as per below example.
SELECT FIND_IN_SET('b','a,b,c,d');
OR you can try with regular expression in MYSQL but it will be too slow.
You can make an array of your locations and then populate your query string with the items from the array
$locations = '22,88';
$locationsToGetArray = explode(",", $locationToGet)
$query = "SELECT * FROM news WHERE newsLocation IN(";
for ($i = 0; $i < count($locationsToGetArray); $i++) {
$query .= $locationsToGetArray[$i];
if($i == (count($locationToGetArray) - 1)) $query.= ")";
else $query .= ",";
}
$db->query($query);
Hi I pulled a table from an external website using YQL. The table contains a list of times such as: But the number of columns can change to include more columns.
| Name | T1 | T2 | T3 |
|--------|--------|--------|--------|
| Name 1 | 23.234 | 45.234 | 16.456 |
| Name 2 | 23.389 | 44.322 | 15.222 |
| Name 5 | 22.890 | 44.221 | 15.345 |
What I'm trying to do is get the lowest value from each column. I've been able to get it by using this function.
<?php
$sectim = [];
for ($st=1; $st < count($allRows); $st++) {
$sectim[] = $phpObj->query->results->tbody->tr[1]->td[6]->content;
}
echo "<pre>"; print_r($sectim); echo "</pre>";
echo "<pre>"; print_r(min($sectim)); echo "</pre>";
?>
This gives me a list of all the times in that column and gives me the minimum. What I am struggling with is how to do it in a way that I can get the minimum from each table columns given the number of columns is not fixed and can either be 3 columns 4 or even 9 columns. I'm thinking of using either another for statements to loop through all the columns but I don't know whether that will work.
You can use this query
select min(T1) as column1,min(T2) as column2,min(T3) as column3 from tabel
This will give you minimum value of each column.
If you are looking For build dynamic query :
$array = array(
'column1' => 'T1',
'column2' => 'T2',
'column3' => 'T3'
);
$column_binder = array();
foreach ($array as $key=>$value){
$column_binder[] = " min($value) as $key ";
}
$query = "SELECT ".impode(",", $column_binder)." From tabel_name";
I am useing Yii framework to do a criteria selection for all rows that fit the criteria.
I am trying to take the ID of one table and search another tables codes that contain the prefix of the ID. (exp ID-code or 1-sdfa). Currently the code below is returning all of the rows as a result. Below are the details, any insight would help. Thank you.
[table 1]
tbl_School
---------------------------
| ID | Name |
---------------------------
| 1 | forist hills |
| 2 | Dhs |
---------------------------
[table 2]
tbl_ticket
------------------
| ID | code |
------------------
| 1 | 1-fd23s |
| 2 | 2-fdet2 |
| 3 | 1-4wscd |
| 4 | 2-oifjd |
| 5 | 1-zzds6 |
------------------
After runing the function on ID=1 I would like to see
------------------
| ID | code |
------------------
| 1 | 1-fd23s |
| 3 | 1-4wscd |
| 5 | 1-zzds6 |
------------------
Here is my code:
public static function get_tickets($ticket_ID){
$match = '';
$match = addcslashes($match, "$ticket_ID".'_%');
$q = new CDbCriteria( array(
'condition' => "code LIKE :match",
'params' => array(':match' => "$match%")
) );
$rows = Ticket::model()->findAll( $q );
return $rows;
}
PDO does escaping for you so you don't need to do the addcslashes yourself (I didn't even know that existed, always used addslashes :) )
Secondly, you end up selecting on [NUMBER]_%%, those are 3 wildcards.
As Ryan already changed in its comment, you might want to select on -% instead:
public static function get_tickets($ticket_ID){
$ticket_ID = intval($ticket_ID);
if (!$ticket_ID)
return null;
$q = new CDbCriteria( array(
'condition' => "code LIKE :match",
'params' => array(':match' => $ticket_ID . "-%")
) );
$rows = Ticket::model()->findAll( $q );
return $rows;
}
As you can see, I did do a numeric sanity check for the ticket number, just like being cautious.
Lastly: I hope you don't mind the suggestion, but isn't it simply possible adding the ticket number as a separate column? You are going to end up with perfectly avoidable performance problems if there are a lot of rows in this table. With a separate column that is an index you'd use a lot less cpu for the same result.
I have the following table structure:
+------------------------+
| id | name | category |
+------------------------+
| 1 | name_1 | cat_1 |
| 2 | name_2 | cat_2 |
| 3 | name_3 | cat_1 |
| . | . | . |
| . | . | . |
| n | name_n | cat_k |
+------------------------+
were "n" is the total rows of table and "k" is an arbitrary number. My question is, is there any way to make an SQL query that retrieve rows grouped by category? I mean it is possible to get something like this structure?
array(
"cat_1" => array(
"name_1", "1",
"name_3", "3",
),
"cat_2" => array(
"name_2", "2",
some rows ....
),
...
"cat_k" => array(
some rows....
),
)
If there is any way please give me some keywords, not entire solution please.
You can't really do this in a single query since mysql alone will not be able to yield multi-dimensional arrays, but it's almost trivial to do using PHP. Basically here is what you would do:
$cats = array();
while ($row = $result->fetch()) {
if (!isset($cats[$row->category])) {
$cats[$row->category] = array();
}
$cats[$row->category][] = $row->name;
}
The query itself will not give you that structure, but it is quite easy to read the result set into that sort of two dimensional array:
$query = "SELECT category, id, name FROM table ORDER BY category ASC, id ASC";
$result = /* use you DB query mechanism of choice here */;
$array = array();
while($row = /* use your DB row fetch mechanism of choice here */) {
$array[$row['category']][] = array($row['id'] => $row['name']);
}