Is using PHP to loop a mySQL query efficient? - php

I have a HTML table full of check-boxes on page 1 with the name attribute set like this:
input type="checkbox" name="A_1"
input type="checkbox" name="B_2"
On the following page, I loop through my $_POST array in PHP with explode to make an array like this:
Array ( [A_1] => on [A_2] => on [B_2] => on [B_5] => on [C_5] => on [submit] => Submit )
Into two arrays:
row_Array = Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 5 [4] => 5 )
column_Array = Array ( [0] => A [1] => A [2] => B [3] => B [4] => C )
This is my SQL query:
for($i=0; $i < count($column_array); $i++) {
$sql = "SELECT {$column_array[$i]} FROM table WHERE num IN ({$row_array[$i]})";
$result = mysqli_query($connection, $sql);
while ($data = mysqli_fetch_assoc($result)) {
$result_array[] = $data[$column_array[$i]];
}
}
Here is an example of my SQL table:
A B C
1 cat cot cet
2 rat rot ret
3 hat hot het
4 lat lot let
5 bat bot bet
The individual SQL queries from my loop look like this and return the correct values:
SELECT A FROM table WHERE num IN (1) returns cat
SELECT A FROM table WHERE num IN (2) returns rat
SELECT B FROM table WHERE num IN (2) returns rot
SELECT B FROM table WHERE num IN (5) returns bot
SELECT C FROM table WHERE num IN (5) returns bet
I am quite new to sql and php. This works but it seems like a terrible way to do this. Any advice or help would be greatly appreciated.

I believe it's a better practice to run a single query and loop through the data returned rather than looping and then running multiple queries.
However, depending on your data structure and application layout, UI, etc, that may not be possible.

I would aim for something like SELECT A,B,C,num FROM table WHERE num IN (1,2,5)
then maybe
foreach($result as $r){
$data[$r['num']] = $r;
}

Let's try just running one query and loop trough it. It's pretty much #Andy Gee 's answer expressed in code.
$cols = implode(',', array_unique($column_array));
$rows = implode(',', array_unique($row_array));
$sql = "SELECT {$cols} FROM table WHERE num IN ({$rows})";
$result = mysqli_query($connection, $sql);
while ($data = mysqli_fetch_assoc($result)) {
//Do your stuff here
}

Something like, it's realy bad idea but..:
$columns = implode(', ', $column_array);
$num_values = implode(', ', $row_array);
$sql = "SELECT {$columns}, num FROM table WHERE num IN ({$num_values})";
$result = mysqli_query($connection, $sql);
while ($data = mysqli_fetch_assoc($result)) {
$result_array[] = $data[$column_array[$i]];
}
Other way is to build one long Query with UNION
SELECT A as value, num FROM table WHERE num = 1
UNION
SELECT A as value, num FROM table WHERE num = 2
UNION
SELECT B as value, num FROM table WHERE num = 2
UNION
SELECT B as value, num FROM table WHERE num = 5
UNION
SELECT C as value, num FROM table WHERE num = 5
returns:
array(
array("num" => 1, "value" => 'cat'),
array("num" => 1, "value" => 'rat'),
...
);

Related

PHP SQL - Search for match with same players

Good mornin'.
I've been tryin to make "something" that will compare matchup percentage but i'm stuck'd at searching for matches with 2 same players.
I've found something about AND OR parenthesis so here is my sql query
$ccc = "SELECT * FROM zapasy WHERE (playerName01 = '".$player1."' OR playerName01 ='".$player2."') AND (playerName02 = '".$player2."' OR playerName02='".$player1."')";
$result3 = $db->query($ccc);
$ids = array();
while ($row55 = $result3->fetch_assoc())
{
$ids[] = $row55['winner'];
}
But , the problem is whenever i try to get data , it gives me more indexes than actualy expected.
This is the result of printing array values after fetching.
Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 )
As expected is only 2 3 4 as u can see in the DB sample here.
db sample
Anyone suggestions on what i'm doing wrong?
A simple way to write this in MySQL uses tuples:
where (?, ?) in ( (playername01, playername02), (playername02, playername01) )
The ? are for parameters. That should be the way that you pass values into queries.
You are doing wrong while using AND OR in where clause
$ccc = "SELECT * FROM zapasy WHERE (playerName01 = '".$player1."' AND playerName02 ='".$player2."') OR (playerName01 = '".$player2."' AND playerName02='".$player1."')";
Try this #Patrik Dano

MySql NOT IN many values from the mysql fetched result

I have 2 MySql queries which are interdependent.
My 'table1'
----------
id
----------
1
2
3
4
5
6
7
My First query
$sql1 = "SELECT * FROM table1 WHERE";// some condition which gives me id's 1,2,3
$res1=$obj->_executeQuery($sql1);
$res1=$obj->getAll($res1);
The result of this is giving me array
Array
(
[0] => Array
(
[id] => 1
..
..
)
[1] => Array
(
[id] => 2
..
..
)
[2] => Array
(
[id] => 3
..
..
)
)
I want to run another query on same 'table1', where not equal to list of ID's which i am getting from the first query.
My Second Query
$sql2 = "SELECT * FROM table1 WHERE id NOT IN (" . implode(',', $res1) . ")";
This is not showing me only one id i,e first. In above case i should get id's 4,5,6,7
Since implode will not give the desired value on multidimensional array so first you need to get the array of all id's to form one-dimensional array then use implode on the array of id's:
$id=array();
foreach ($res1 as $key=>$inner_array){
$id[]= $inner_array['id'];
}
you can use array_walk also here like this:
array_walk($res1,function($c) use (&$id) {$id[] = $c['id'];});
but i think the best one is array_map :
$id = array_map(function($i) {
return $i['id'];
}, $res1);
$sql2 = "SELECT * FROM table1 WHERE id NOT IN (" . implode(',', $id) . ")";
Note: when ever you are doing select please specify your column if you need few to select,unnecessarily selecting all will slow your sql processing.
suppose here:SELECT * FROM table1 WHERE, if you need only id then please select id only.
You have to change $res1, which is a two-dimensional array, into a one-dimensional array of IDs to be able to use implode:
$ids_from_first_query = array();
foreach($res1 as $result_row) {
$ids_from_first_query[] = $result_row['id'];
}
$ids_as_string = implode(',', $ids_from_first_query);
$sql2 = 'SELECT * FROM table1 WHERE id NOT IN(' . $ids_as_string . ')';
In the above code, $ids_as_string will look like this:
1,2,3
Thus it can be used in your MySQL query
Here you are getting two dimensional array so that's reason it is not working
while($each=mysql_fetch_array($res1))
{
$array[]=$each[id];
}
$imp=implode(",",$array);
$sql2 = "SELECT * FROM table1 WHERE id NOT IN (".$imp.")";
Try this it will works

Joining two mysql tables and populate an array in php

I have two tables
First table name is "Library"
ID Name
1 A
2 B
Second table name is "Books"
BID Book_Creater Book_Name
1 Variation1 Book1
1 Variation2 Book2
1 Variation3 Book3
2 Variation1 Book4
2 Variation2 Book5
The sql is
$sql = mysql_query("select library.ID,books.Book_Creater,books.Book_name from library,books where library.ID = books.BID;
Result is
ID Book_Creater Book_name
1 Variation1 Book1
1 Variation2 Book2
1 Variation3 Book3
2 Variation1 Book4
2 Variation2 Book5
Now I would like to create an array which should look like and this is where I am stuck.
array(
[1] => array(
[Variation1] => Book1
[Variation2] => Book2
[Variation3] => Book3
)
[2] => array(
[Variation1] => Book4
[Variation2] => Book5
)
)
Any suggestion will do great.
$res = array();
//for each result row
while($row = mysql_fetch_array($sql))
{
//add a row to the multi dimensional result array
//where the first key = id and the second key = Book_Creater
$res[$row['id']][$row['Book_Creater']] = $row['Book_name'];
}
print_r($res);
Simple way is to do a join as you currently do, and push them into an array as you loop around (possibly for neatness creating a new sub array explicitly when the ID as a key doesn't already exist).
Something like this:-
<?php
$store_array = array();
$sql = mysql_query("SELECT library.ID,books.Book_Creater,books.Book_name
FROM library
INNER JOIN books
ON library.ID = books.BID");
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
if (!array_key_exists($row['ID'], $store_array))
{
$store_array[$row['ID']] = array();
}
$store_array[$row['ID']][$row['Book_Creater']] = $row['Book_name']
}
print_r($store_array);
?>
You could also shuffle some effort onto the database and bring things back concatenated together. Then just split the results to put into the array:-
<?php
$store_array = array();
$sql = mysql_query("SELECT library.ID, GROUP_CONCAT(CONCAT_WS('#~#', books.Book_Creater,books.Book_name)) AS details
FROM library
INNER JOIN books
ON library.ID = books.BID
GROUP BY library.ID");
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$store_array[$row['ID']] = array();
$details = explode(',', $row['details']);
foreach($details as $detail)
{
$detail_line = explode('#~#', $row['detail']);
$store_array[$row['ID']][$detail_line[0]] = $detail_line[1];
}
}
print_r($store_array);
?>
Note that if you are doing this with real data you probably want to chose delimiters which are not going to appear in your data, and also that by default GROUP_CONCAT has a fairly limited max length.
Note also I have used mysql_* functions as that is what you seem to be using, but these are deprecated and probably shouldn't be used now.

The best way to select rows from several tables?

I am creating a php app and am now creating the DB interface code. In my APP their are three tables, user, network, user_network. They have the following columsn:
user<-------------------->user_network<-------------------->network
user_ID un_ID network_ID
user_Name un_Member network_Name
un_Network network_Description
I have created the following query which querys the user_network table and returns the ID's of all networks which the user is a member of:
$STH = $DBH->query("SELECT * FROM user_network WHERE nm_Member ='$userID'");
I then pull the network_ID's from this array using the following code:
$DB_NetworkID = array();
foreach($STH as $row)
{
$DB_NetworkID[$counter] = $row['nm_networkID'];
$counter++;
}
print_r($DB_NetworkID);
The array now holds of all the network IDs that the user is a member of, like so
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 ) //network IDs = 1, 3, 5, 7
I would now like to pull rows from the networks tables, how do i go about about SELECTing rows from the networks database WHERE the ID is contained in the array?
Thanks for any help.
You should use a JOIN instead and make it a single query:
SELECT
*
FROM
user
INNER JOIN
user_network
ON
user.user_ID = user_network.un_ID
INNER JOIN
network
ON
user_network.un_Network = network.network_ID
WHERE
user_ID = '$userID'
That query will give you all networks that a user is member of.
If your problem is the comparison against an array, then an easy workaround is using FIND_IN_SET() like this:
$list = implode(",", $DB_NetworkID); // turns it into "1,3,5,7"
... "SELECT * FROM foo WHERE FIND_IN_SET(network_ID, '$list')";
For better performance you should construct an .. IN (?,?,..) clause however.
Of course using JOIN as "halfdan" said, is the best way.
But I gonna answer your question:
If your array is like this:
$un = Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 ) //network IDs = 1, 3, 5, 7
You can use a Foreach like this:
Foreach($un as $key => $value) {
and your query will be like this:
SELECT * FROM network WHERE network_ID ='$value'

How to get the result of a query with values in a array?

This is my function where i fetch my results from database
function Prof_Viewer($MemberId)
{
$query = $this->db->query("SELECT distinct(t1.dProfileId) as prof
FROM tbl_profile_viewer as t1
JOIN tbl_login as t2
WHERE t1.dProfileViwerId='$MemberId'");
if($query->num_rows > 0)
{
foreach($query->result() as $row)
{
echo $row->prof;//i am receiving many values here
$query1 = $this->db->query("SELECT distinct(t3.dUser_name),t2.dPath,t3.dCreatedDate
FROM tbl_login as t3
JOIN tbl_profile_viewer as t1,
tbl_member_details as t2
WHERE t3.dMember_Id = '$row->prof'
AND t2.dMember_Id ='$row->prof'");
}
return $query1->result_array();
}
}
As commented above i receive many values while echo the variable $row->prof;
say i have values such as 1 2 and 3.......
Even if i have these three values my 'query1' takes only the last value .so i have only one result i want the query to be executed for 1 and 2 also
how to Achieve that
You can just use PHP's explode() to convert your string into an array. For example:
<?php
$str = 'one|two|three|four';
// positive limit
print_r(explode('|', $str));
// gives you an array:
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
?>
But I think you would be better off if you learned how to do JOIN's:
http://en.wikipedia.org/wiki/Join_(SQL)
I'm assuming you're using mysqli here.
Result_fetch_array() will do what you want
http://au2.php.net/manual/en/mysqli-result.fetch-array.php

Categories