Jump to content

Can php do two execute (queries) in same time?


sigmahokies
Go to solution Solved by Barand,

Recommended Posts

Hi everyone

 

I'm getting somewhere in PHP, but I am still have long way to go.

I want to ask you...is there possible to have two execute the query from database? For example -

 

$one = "select FirstName, Lastname, Local FROM Members WHERE Local = 'Yes'"; (Local member)

$two = "select FirstName, Lastname, Local FROM Members WHERE Local = 'No'"; (Non-Local member)

 

if ($one && $two) {

(mysqli_num_rows($one) && mysqli_num_rows($two))

 

That is my script, but is it possible to have split in two col for "Yes" and "No".

Please let me know if it is possible or not...

 

if not, it means, I have to do completely seperate script between Yes and No (true/false).

 

Thank you,

 

Gary

Edited by sigmahokies
Link to comment
Share on other sites

There is a multi query option but I have never found the need to use it.

 

In you example I cannot see a need either. If you want to separate the local and non-local you can ORDER BY local.

 

If, as it appears, you are only interested in how many of each

SELECT local, COUNT(*) as total
FROM members
GROUP BY local;
Link to comment
Share on other sites

Hi Gizmola,

 

I am trying to do is separate the yes and no from one columns that go to two columns. For example, I will like to have list of members are local, and list of member are non-local. I can put local and non-local on top of table, then list goes on under local, and list goes on under non-local that i created the one columns in database, i put some yes and no in one column under local on table in database. Get it?

 

Please let me know if you need to understand what I said. Really, English is my second language.

 

Thanks,

Gary 

Link to comment
Share on other sites

Order? It will go one columns, but i will like to have two columns on website that split from one column on the table. I created one column under "local" on the table, But I inserted a cell "yes" and "no" on one columns, so i will like to have split one to two columns on website that show separate the yes and no, one column show "yes" (true), the next columns show "no" (false). if you saying ORDER can split the yes and no as true and false into two columns? I will give it a try.

 

P.S. I tried to insert two image to make clear example what I want to create, but seem this post doesn't have icon to insert an image...

Link to comment
Share on other sites

  • Solution

try something like this

<?php
include('db_inc.php'); // defines HOST etc
$db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);

$sql = "SELECT firstname
        , lastname
        , local
        FROM member
        ORDER BY firstname, lastname";
$local = '';
$nonlocal = '';
$res = $db->query($sql);
while (list($fn, $ln, $loc) = $res->fetch_row()) {
    if ($loc=='Y') {
        $local .= "$fn $ln<br>";
    }
    else {
        $nonlocal .= "$fn $ln<br>";
    }
}
?>
<html>
<head>
</head>
<body>
<table border='1'>
<tr><th>Local</th><th>Non-Local</th></tr>
<tr style='vertical-align: top;'>
    <td><?=$local?></td>
    <td><?=$nonlocal?></td>
</tr>
</table>
</body>
</html>
  • Like 1
Link to comment
Share on other sites

Hi Barand and Gizomla,

 

I'm not sure I follow you, what i did do scipt in PHP in HTML:

 

<!doctype html>
<html>
<head>
<title>Local and Non-local Members</title>
<link href="rcd.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="1" width="25%">
<tr><td><b><i>Local</i></b></td><td nowrap><b><i>Non-local</i></b></td></tr>
<tr>
<?php
 
$local = "SELECT * FROM Members WHERE Local = 'Yes'";
$nonlocal = "SELECT * FROM Members WHERE Local = 'No'";
$local2 = mysqli_query($Garydb, $local);
$nonlocal2 = mysqli_query($Garydb, $nonlocal);
 
if (mysqli_num_rows($local2)) {
while ($rows = mysqli_fetch_assoc($local2)) {
echo "<tr><td nowrap>".$rows['FirstName']." ".$rows['LastName']."</td>";
 
if (mysqli_num_rows($nonlocal2)) {
while ($row2 = mysqli_fetch_assoc($nonlocal2)) {
echo "<td nowrap>".$row2['FirstName']." ".$row2['LastName']."</td></tr>";
}
}
}
}
?>
</tr>
</table>
</body>
</html>
 
This script just end up incorrect separate between local and nonlocal. I hope you can help...
Link to comment
Share on other sites

I tried following you before, and just looked at this last post and still don't understand.

 

How about giving us a real life example such as:

 

Example 1:

 

John Non1

John Non2

John Non3

...

...

...

jane local1

Jane local2

Jane local3

 

 

and show us how you want to see that output.  What you gave us now gives you this:

 

john non1  jane local1 jane local2 jane local3

john non2

john non3

 

My original interpretation of what you wanted was something like my Example 1:

 

Non Locals:

john non1

john non2

john non3

 

Locals:

jane loc1

jane loc2

jane loc3

 

That is not what you want?

Link to comment
Share on other sites

I wouldn't call Barand's code OOP.  It's the same code I use and I don't use OOP very often at all.

 

So - it doesn't work.  Care to enlighten us as to what error you are getting or what makes you feel it is not working?  Maybe post your code so we can see what you are running.  Be sure you have php error checking turned on too!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.