473,609 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mltple table query Error: Query was empty

10 New Member
Hi,

I'm working on a select statement to retrieve data, which I know(data exists) is in the MySQL DB.

Here is the part I'm working on:

Expand|Select|Wrap|Line Numbers
  1. function funGetRecipeIngredientsAsTable() {
  2.  
  3. $recipeID = $_SESSION['sesRecipeID'];
  4.  
  5. $sqlString = "SELECT u.unitName, m.measureAbbrev, i.ingredientName
  6. FROM Recipe r, RecipeIngredients ri, Units u, Measurements m, i.Ingredients
  7. WHERE r.recipeID = ri.recipeID
  8. AND ri.unitID = u.unitID
  9. AND ri.measureID = Measurements.measureID
  10. AND ri.ingredientID = i.ingredientID
  11. AND ri.recipeID = ".$recipeID.";";
  12.  
  13. $ingredientDataResult = mysql_query($sqlString);
  14.  
  15. echo "sql string is: ".$ingredientDataResult ."<br/>";
  16. echo "recipe ID is: " . $recipeID ."<br/>";
  17.  
  18. if (!mysql_query($ingredientDataResult)) {
  19.   die('Error: ' . mysql_error());
  20.   }


(have some other code here, but that's not the issue)
I have a connection to the DB because all my other functions are working

When I run the function I get this:

sql string is:
recipe ID is:
Error: Query was empty

It's acting as if it doesn't know what my $recipeID is, and it also isn't telling me what the value of $sqlString is either. I have set recipeID session variable on another page, and on this page I'm retrieving it, yet it's empty.

I've played with the last where statement so many times, maybe I've ended up with something totally wrong. When I test it in MySQL, I replace all the variable stuff with a number and it retrieves all the data I need to see.

Hoping someone can help me.
Apr 17 '10 #1
11 3244
Atli
5,058 Recognized Expert Expert
Hey.

Are you sure you have started the session on that page (with session_start)?
In any case, you should verify the ID before trying to put it into the query. You can use the isset function on the $_SESSION['sesRecipeID'] element to do that.

and it also isn't telling me what the value of $sqlString is either
You are printing the wrong variable. Your echo is printing the result of the mysql_query call, rather than the query string, which will never print anything other than FALSE or a resource ID.
Apr 17 '10 #2
hgreenesmith
10 New Member
Thank you for responding! Yes, I have the session_start at the top of my page.

I just did an isset and it returned my else statement, so that's one issue I will have to check.

As far as printing the wrong variable... What else can I print? I want to see the results, not the actual string. If I put the $sqlString in there all I'm going to get is the select statement.

Am I not thinking clearly? From my code, can you tell me what to print out? I'm confused.

Thank you again for your help! : )
Apr 17 '10 #3
Atli
5,058 Recognized Expert Expert
Ahh I see.

You need to use the mysql_fetch_arr ay, or one of it's siblings, to fetch the data from the result set. The mysql_query function only returns a resource ID; a pointer to a location where the actual data MySQL returned is stored. To fetch it, you need the mysql_fetch functions.

For example, to simply dump the data from a query into a table:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. mysql_connect(...);
  3. mysql_select_db(...);
  4.  
  5. $sql = "SELECT `col1`, `col2` FROM `myTable`";
  6. $result = mysql_query($sql) or die(mysql_error());
  7.  
  8. echo '<table>';
  9. while($row = mysql_fetch_assoc($result)) 
  10. {
  11.     echo '<tr>';
  12.     echo "<td>{$row['col1']}</td>";
  13.     echo "<td>{$row['col2']}</td>";
  14.     echo '</tr>';
  15. }
  16. echo '</table>';
  17.  
  18. mysql_free_result($result);
  19. mysql_close($result);
  20. ?>
Apr 17 '10 #4
hgreenesmith
10 New Member
actually that is very similar to what I already have. Maybe I have something out of order... after my if/die statement I wrote this, but since it wasn't even pulling anything, I assumed it was higher up in my select statement.

after my select statement my intent is to set the variables and then dump them into the table which already exists. I'm calling the function at the bottom of that table just before the closing tag to make the table grow as ingredients get entered and saved in the DB. That's why you don't see table tags in this code.

Expand|Select|Wrap|Line Numbers
  1. //SET SESSION VARIABLES
  2. $_SESSION['sesUnitName'] = $unitName;
  3. $_SESSION['sesMeasureName'] = $measureAbbrev;
  4. $_SESSION['sesIngredientName'] = $IngredientName;
  5. //$_SESSION['recipeID'] = $recipeID;
  6.  
  7.  
  8. $ingredientDataResult = mysql_query($sqlString);
  9.  
  10. echo "sql string is: ".$sqlString ."<br/>";
  11.  
  12. //echo "recipe ID is: " . $recipeID ."<br/>";
  13.  
  14. if (!mysql_query($ingredientDataResult))
  15.   {
  16.   die('Error: ' . mysql_error());
  17.   }
  18.  
  19. echo "<tr>Ingredients Added</tr>
  20. <tr>
  21. <th colspan='2'></th>
  22. <th>Unit Name</th>
  23. <th>Measurement</th>
  24. <th>Ingredient</th>
  25. </tr>";
  26.  
  27. while($row = mysql_fetch_array($ingredientDataResult))
  28.   {
  29.   echo "<tr>";
  30.   echo "<td  colspan='5'>You have added: </td>";
  31.   echo "<td>" . $row['sesUnitName'] . "</td>";
  32.   echo "<td>" . $row['sesMeasureName'] . "</td>";
  33.   echo "<td>" . $row['sesIngredientName'] . "</td>";
  34.   echo "</tr>";
  35.   }
So for my original thought in this reply; do I have this out of order? By the way, I've tried both the fetch_array and the fetch_assoc and neither did any good.
Apr 17 '10 #5
Atli
5,058 Recognized Expert Expert
The only problem in there (assuming the $sqlString variable is defined earlier) is the IF clause on lines #14-17. You are doing a mysql_query call on the return value of a previous mysql_query call, which will always fail.

If your intent is to verify that the first call returned a valid result, then you can just remove the second call and just test the variable itself.
Expand|Select|Wrap|Line Numbers
  1. if (!$ingredientDataResult) {
  2.     die("Error: " . mysql_error());
  3. }
A mysql_query call always returns either FALSE or a resource ID. A resource ID is evaluated as TRUE, so this test will return FALSE (and thus execute the die command) only if the query failed.
Apr 17 '10 #6
hgreenesmith
10 New Member
Ok. I've just redone the or die method and now I finally have a clue. This is my new error.
mysql Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7

It seems my syntax is either wrong or it's misleading because it doesn't know what else to say. So it's one of two things or both.
1-get the session variable working
2-syntax is wrong on the last line of the select statement.

I have a really odd question.

My recordID is set up as a $recipeID = "{$_GET["recipeID"]}";

If I've set it up as this, can I turn around and set it as a session variable on another page? The reason I'm asking is because on another page it tells me what the recordID is, but on this php page where I'm trying to retrieve it as a session variable and it won't.
Apr 17 '10 #7
Atli
5,058 Recognized Expert Expert
Try putting this in a new PHP document in the same directory your other PHP files are, and check to see if it prints you session variable.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. header('content-type: text/plain');
  4. print_r($_SESSION);
  5. ?>
Note, this works best in one of the modern browsers. (Firefox/Chrome/Opera... basically everything except IE.)

If that doesn't print your sesRecipeID variable, can you post the code that sets the variable?
Apr 17 '10 #8
hgreenesmith
10 New Member
I am using the latest v of firefox at the moment and after trying your example I got three errors. could not send session cookies(which I don't have any) could not send session cache limiter. Both of these are referencing line 1
line 2 was Cannot modify header information - headers already sent

Expand|Select|Wrap|Line Numbers
  1. //Set the variable
  2. $_SESSION['sesRecipeID'] = $recipeID;
  3.  
  4. //retrieve session variables for loginID, recipeID, recipeTitle
  5. $recipeID = $_SESSION['sesRecipeID'];
  6. $recipeTitle = $_SESSION['sesRecipeTitle'];
  7.  
  8. //echo '<br>Record inserted, ID:  ' .$recipeID;
  9. // Redirect back to the recipe page
  10.  header("Location: addRecipe.php?recipeID=$recipeID");
This code is on a separate php for processing just the id and the title, it then redirects back to the page where data gets entered.

Expand|Select|Wrap|Line Numbers
  1. $recipeID = "{$_GET["recipeID"]}";
  2. $recipeTitle = "{$_GET["recipeTitle"]}";
  3.  
  4. //generated code from start logic to connect to db
  5. $link = mysql_connect('hgreenesmith1.startlogicmysql.com', 'hgreenesmith1', 'heather1!'); 
  6. if (!$link) { 
  7.     die('Could not connect: ' . mysql_error()); 
  8. mysql_select_db(recipes);
  9. $sqlString = "SELECT recipeTitle, categoryID, temp, bakeTime 
  10.                             FROM Recipe
  11.                             WHERE Recipe.recipeID = $recipeID";
  12.  
  13. if(!$_GET["recipeID"] == ""){
  14. $recipeTitleQuery = @mysql_query($sqlString);
  15. $rows = mysql_fetch_array($recipeTitleQuery);
  16. }
  17.     if($rows >= 0){
  18.         $recipeTitle = $rows[0];
  19.         $categoryID = $rows[1];
  20.     }
  21.     else {
  22.         $recipeTitle = "txtRecipeTitle";
  23.         $categoryID = "selCategories";    
  24.     }
  25.  
  26.  
These two groups of code work just fine.

I really appreciate all your help. Thank you!
Apr 17 '10 #9
Atli
5,058 Recognized Expert Expert
I am using the latest v of firefox at the moment and after trying your example I got three errors. could not send session cookies(which I don't have any) could not send session cache limiter. Both of these are referencing line 1
line 2 was Cannot modify header information - headers already sent
That means there was something before the code I posted. It has to be the only thing in the output. Even a white-space before the <?php tag will result in this error.
Apr 17 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
7453
by: lawrence | last post by:
If I do something like : $result = mysql_query($query); $dbArray = dbResultIntoKeyArray($result); and this is the function: function dbResultIntoKeyArray($result) {
2
2916
by: Jim Hatfield | last post by:
I'm using: "select min(objid) from table_name where objid > $objid" to select the next row in a sequence where there may be gaps. It works fine unless the where clause results in an empty set. From the command line, a value is *always* returned, which is NULL when the set of rows selected by the where clause is empty. I wasn't sure what I expected mysql_query to return. I didn't expect a FALSE result, since the query is syntactically...
4
5313
by: Eli Sidwell | last post by:
Trying to return a Recordset to an ASP and the Recordset is empty. The StorredProc works in the query analyzer and it even works from a quick VB app that I wrote to test it. The storedproc that I am using is fairly complex (creates some temporary tables and populates them with 'Insert Into Select ...', but the during testing the only Select statements that return visible rows is the final one that returns the finished table with an...
0
504
by: Phil Powell | last post by:
Based upon an article I read in http://archives.neohapsis.com/archives/mysql/2004-q1/0337.html I am trying to retrieve an "empty row" of fields from a table that may be empty. If I have this query: This will return 1 single row whereby I have the table columns within
3
4867
by: nephish | last post by:
Hey there, i have a simple database query that returns as a tuple the number of rows that the query selected. kinda like this >>> cursor.execute('select value from table where autoinc > 234') >>> x = cursor.fetchall() >>> print x >>> 21L
7
19947
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into those varchar columns. Bill Leung leungb@aptea.com
2
3974
by: mark | last post by:
I have come across a problem that has me baffled. I am using PHP with MySQL 4.1.14. Whenever I execute an update statement against a specific table, some of the varchar fields do not get set to the new values, but get set to empty strings instead. I have simplified the code down to updating a single column in the table, but the same results happen when updating just the single column.
2
2348
by: hackmagic | last post by:
Hi, i have a form that normally has a Recordset containing only one record bound to it. i have replaced the navigation buttons with my own and the 'New Record' button assigns an empty Recordset to the form and then uses <recordset>.AddNew to create the new record. the query that i use to obtain the empty recordset (so i bind the controls as normal) is: "SELECT TFiles.Id AS FileId, TFiles.* FROM TFiles WHERE TFiles.Id=0" this query is...
2
1360
by: Ievel | last post by:
"Ricky" <iqboss@hotmail.comwrote in message news:a9c648ec.0307111115.14fe5ef0@posting.google.com... I haven't been able to test the following and you probably can find more performant statements, but it think it does the job: UPDATE T t1 SET en2_id = DECODE(SELECT 1 FROM T t2 WHERE t2.en1 = t1.en2
8
3893
by: thatcollegeguy | last post by:
http://smarterfootball.com/exits/theHTML.html I am not sure what is wrong w/ this code. The main issue is that the table that is in the initial html will empty its td but the table that I load using php and jquery through a mysql database will not empty the td elements. There is a table underneath the button in the initial html. if you click on a box in the table, it will empty. the same does not happen for the table that is generated by...
0
8121
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8062
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8519
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6987
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5506
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1378
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.