473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Is this Possible - PHP/MySQL

Well, after half pulling my hair out messing with this, I am thinking it is
not possible. I did research and found references to "sub-queries" not
possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I am
breaking that rule or not.

Psuedo code of what I am trying to do...

<?php

$sql = "select * from table1";
$db_result = mysql_query($sql);

while ($row = mysql_fetch_array($db_result)) {
$table_key = $row("table_key");
$sql2 = "select * from table2 where tabel_key='$table_key'";
$db_result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($db_result2);

if record exists in table2
do something with table 2;
else
do something with table 1;
}
?>

The second query seems to always return an empty array. Is this nested
query allowed in PHP/MySQL?

Thanks in advance for any help,

PTM
Jul 17 '05 #1
5 4602
I noticed that Message-ID: <bg**************@bignews5.bellsouth.net>
from Phillip T. Murphy contained the following:
The second query seems to always return an empty array.


tabel_key?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
.oO(Phillip T. Murphy)
Well, after half pulling my hair out messing with this, I am thinking it is
not possible. I did research and found references to "sub-queries" not
possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I am
breaking that rule or not.
A subquery (or subselect) is a SELECT statement nested inside another:

SELECT something FROM somewhere WHERE foo = (SELECT somethingelse ...)

Subqueries are supported as of MySQL 4.1.
The second query seems to always return an empty array. Is this nested
query allowed in PHP/MySQL?


No problem with that.

But you have no error checking in your code, you won't notice when a
query fails. Check if mysql_query() returns a resource ID, if not have a
look at mysql_error() to see if MySQL encountered a problem.

http://www.php.net/mysql_query
http://www.php.net/mysql_error

Micha
Jul 17 '05 #3

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:cp********************************@4ax.com...
.oO(Phillip T. Murphy)
Well, after half pulling my hair out messing with this, I am thinking it
is
not possible. I did research and found references to "sub-queries" not
possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I
am
breaking that rule or not.


A subquery (or subselect) is a SELECT statement nested inside another:

SELECT something FROM somewhere WHERE foo = (SELECT somethingelse ...)

Subqueries are supported as of MySQL 4.1.
The second query seems to always return an empty array. Is this nested
query allowed in PHP/MySQL?


No problem with that.

But you have no error checking in your code, you won't notice when a
query fails. Check if mysql_query() returns a resource ID, if not have a
look at mysql_error() to see if MySQL encountered a problem.

http://www.php.net/mysql_query
http://www.php.net/mysql_error

Micha


Thanks for the quick reply. in the interest of space, I cut that out. I
have a function defined that replaces the mysql_query() call and does error
checking.

At least I know what I am trying to do is possible now. I am starting over.
I would like to be able to just use a LEFT JOIN, but the second table needs
a where clause on a field that does not exist in the first table, so too
many records are elimiated.

I will keep trying. Thanks for the input.
Jul 17 '05 #4
"Phillip T. Murphy" <pS**********@bellsouth.net> wrote in message
news:bg**************@bignews5.bellsouth.net...
Well, after half pulling my hair out messing with this, I am thinking it is not possible. I did research and found references to "sub-queries" not
possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I am breaking that rule or not.

Psuedo code of what I am trying to do...

<?php

$sql = "select * from table1";
$db_result = mysql_query($sql);

while ($row = mysql_fetch_array($db_result)) {
$table_key = $row("table_key");

[...]

Not sure if/what this will fix, but in that last line ($table_key =
$row("table_key");), the parantheses in $row("table_key") should be changed
to brackets: $row["table_key"]

- JP
Jul 17 '05 #5

"kingofkolt" <je**********@comcast.net> wrote in message
news:B4ckd.21457$5K2.20313@attbi_s03...
"Phillip T. Murphy" <pS**********@bellsouth.net> wrote in message
news:bg**************@bignews5.bellsouth.net...
Well, after half pulling my hair out messing with this, I am thinking it

is
not possible. I did research and found references to "sub-queries" not
possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I

am
breaking that rule or not.

Psuedo code of what I am trying to do...

<?php

$sql = "select * from table1";
$db_result = mysql_query($sql);

while ($row = mysql_fetch_array($db_result)) {
$table_key = $row("table_key");

[...]

Not sure if/what this will fix, but in that last line ($table_key =
$row("table_key");), the parantheses in $row("table_key") should be
changed
to brackets: $row["table_key"]

- JP


Thanks for the responses everyone. Thanks to suggestions here and assuring
me that it is possible to do nested queries, I got it to work.

Come to find out, it had to do with the way I was checking if the second
query returned anything or not. When it didn't return anything, I was
checking one of the array variables to see if it was NULL or Blank. That
did not work. I backed up and now check what the mysql_query returns (FALSE
or an Array).

All is now well. As always, coming to a group like this to get answers fast
solved my problem.
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pugi! | last post by:
Hi, I would like to upload office-document (doc, xls, ...) using a form to a website (apache, php, mysql) in a specific directory and if possible insert it into a table (MySQL-db). Is this...
4
by: nandyal | last post by:
All, The following script works as intended in MS-Windows and in other Linux boxes, except on the one that runs php 4.3.10. Sorry for the long post. 1) # uname -a Linux bubba 2.4.20 #5 SMP...
0
by: Dan Anderson | last post by:
Can anyone point me to a reference on how to insert images into a column in a mySQL database -- or is that not possible? Thanks in advance, Dan Anderson -- MySQL General Mailing List
2
by: mos | last post by:
I want to put a MySQL 4.1 database on a Win2k laptop but the problem is it contains confidential client information. It has to be Window because applications accessing the database are written in...
1
by: leegold2 | last post by:
In MYSQL is it possible to shift and splice colums? say I have a table w/fields: name | tele_num | age and I get all the addresses later - address initally stored in a txt type file with 1 rec...
2
by: AMD | last post by:
Hi, I would like to have MySQL use a mapped network drive. I'd like to do this in case there is a failure of the mysql machine, I can just replace it with a new machine pointing to the same...
3
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works...
14
by: luvtopost | last post by:
I am trying to learn PHP and was reading the book by Laura Welling and Luke Thompson. Obviously, PHP is already installed on a server when hosted by an ISP already. Is there a way to just download...
1
by: klaydze | last post by:
hi guys, is it possible that i can retrieve records from different database like SQL Server 2000 and MySQL? example, assuming i have some table in SQL Server 2000 and i also have in MySQL. the table...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.