473,378 Members | 1,319 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,378 software developers and data experts.

mltple table query Error: Query was empty

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

✓ answered by Atli

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?

11 3220
Atli
5,058 Expert 4TB
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
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 Expert 4TB
Ahh I see.

You need to use the mysql_fetch_array, 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
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 Expert 4TB
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
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 Expert 4TB
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
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 Expert 4TB
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
So sorry about that. I was so tired, I never noticed that the beginning of the tag had a space in front of it.

Here is what the test page says.
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3. )
I've never used that code before, so I don't know exactly what it's doing, but I'm guessing that IF there is anything in a session, my loginID should have at least been there...

I went back real quick and created three variables and then I set them. When I viewed this page again, I got this:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [sesLoginName] => Heather
  4.     [sesRecipeID] => 1
  5.     [sesRecipeTitle] => Potato Soup
  6. )
Is this how it should look?
I then went to another page to see if I could retrieve these and when I echo them, they are all empty. I'm not able to retrieve their values for some reason. I will continue googling. I'm sure I'm missing some little key element I've forgotten to do.
Apr 17 '10 #11
Since I now know the issue is not mysql, and actually a php challenge, this subject can be closed/resolved. I am working on making a class that will house the session variables and I will make that page required on all the other pages. Once I get that done, if I run into any problems I might post another thread under the php heading.

Thank you again for all your help!
Apr 17 '10 #12

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

Similar topics

3
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
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. ...
4
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...
0
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...
3
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')...
7
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...
2
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...
2
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...
2
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...
8
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.