943,840 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1370
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 5th, 2008
0

query behaving funny

Expand Post »
If I run this query in the PHP Admin it displays 2 records

php Syntax (Toggle Plain Text)
  1. SELECT staff.emp_no, staff.fName, staff.lName, event.event_date, event.timeStart, event.timeEnd, event.emp_no, event.agenda
  2. FROM staff, event
  3. WHERE staff.emp_no = event.emp_no
  4. AND event.emp_no =201
  5. AND event.event_date = "27-01-2008"
  6. AND timeStart = "1020"

The above query gives
Results
emp_no fName lName event_date timeStart timeEnd emp_no agenda
201 John Atkins 27-01-2008 1020 1105 201 urgent meeting
201 John Atkins 27-01-2008 1020 1100 201 brief discussion

If I fill out a form and types in 27-01-2008 as the value for the variable $e_date, 201 for $check_array and 1020 for $s_time therefore making this new query

php Syntax (Toggle Plain Text)
  1. $sql = 'staff.emp_no, staff.fName, staff.lName, event.event_date, event.timeStart, event.timeEnd, event.emp_no, event.agenda FROM staff, event WHERE staff.emp_no = event.emp_no AND event.emp_no = "$check_array" AND event.event_date = "$e_date" AND timeStart = "$s_time"';

with this query from PHP prodices a ZERO record, can somebody tell me what is wrong? because even if I do a $count on mysql_fetch_array() I do not get a value greater than 0.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 5th, 2008
0

Re: query behaving funny

Hi.

The problem is that you are using single-quotes around your query.
Variables within single-quoted strings will not be evaluated like they are in double-quoted strings.

Consider this:
php Syntax (Toggle Plain Text)
  1. $var = 'hi';
  2.  
  3. echo "Var = $var"; // Var = hi
  4. echo 'Var = $var'; // Var = $var

Try reversing the quotes in your query, replacing double-quotes with single-quotes and vise versa.
Last edited by Atli; Nov 5th, 2008 at 8:06 pm. Reason: Spelling
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 5th, 2008
0

Re: query behaving funny

I just tried your correction but it did not work, therefore I have decided to display the whole query just in case you can tell me what I am doing wrong at this stage.

The program needs to check from the query the size of the fetch_query and then determine what to do but looks like the first part of the argument is the only one working ($count==0)

php Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Search - Online Diary System</title>
  4. </head>
  5. <body bgcolor="#FFFFFF" link="#0000CC" vlink="#0000CC">
  6.  
  7. <p>&nbsp;</p>
  8. <p align="center"><img src="images/logo.GIF" width="543" height="72"></p>
  9. <center>
  10. <table width="75%" border="0">
  11. <tr>
  12. <td> <form name="searchf1" method="post" action="meet_list.php">
  13. <div align="center">
  14. <input type="submit" name="search1" value="Back">
  15. </form></td>
  16. </tr>
  17. </table>
  18. </center>
  19. <?php
  20.  
  21. $link = mysql_connect('localhost','db', 'database') or die('Could not connect: ' . mysql_error());
  22. // echo 'Connected successfully';
  23. mysql_select_db("online_diary",$link) or die ('Could not connect ' .mysql_error());
  24.  
  25. // search db and put results into array
  26. FOREACH ($_SESSION['check_array'] as $check_array)
  27. {
  28.  
  29. $sql = "staff.emp_no, staff.fName, staff.lName, event.event_date, event.timeStart, event.timeEnd, event.emp_no, event.agenda FROM staff, event WHERE staff.emp_no = event.emp_no AND event.emp_no = '$check_array' AND event.event_date = '$e_date' AND timeStart = '$s_time'";
  30.  
  31. $result=mysql_fetch_array($sql);
  32.  
  33. // Mysql_num_row is counting table row
  34. $count=mysql_num_rows($result);
  35.  
  36. echo "$count";
  37. // check $count
  38. if ($count==0)
  39. { // no records match - go ahead and write record
  40. /**
  41. * $face2face = new meet;
  42. * $face2face->w_meet();
  43. */
  44. header('location:meet.php');
  45. exit();
  46.  
  47. }
  48. if ($count>0)
  49. {
  50. // print the list into a table
  51.  
  52.  
  53. ?>
  54. <body>
  55. <form name="check" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  56. <center>
  57. <?php
  58. echo "<table border='1'>
  59. <tr>
  60. <th> Colleague(s) </th>
  61. <th>Date (d-m-y)</th>
  62. <th>Meeting Time</th>
  63. <th>Agenda </th>
  64. <th>Status </th>";
  65.  
  66. // use a while on the array to print the contents of the array
  67. while ($e_row = mysql_fetch_array($result)){
  68.  
  69. // $_SESSION['e_search']
  70. $status = 'busy';
  71.  
  72. echo "<tr>";
  73. echo "<td>" . $e_row['fName'] . $e_row['lName'] . "</td>";
  74. echo "<td>" . $e_row['event_date'] . "</td>";
  75. echo "<td>" . $e_row['timeStart'] . "</td>";
  76. echo "<td>" . $e_row['Agenda'] . "</td>";
  77. echo "<td>" . $status ."</td>";
  78.  
  79. }
  80.  
  81. }
  82. echo "</table>";
  83. echo "<br />";
  84. echo "<br />";
  85. ?>
  86.  
  87. <table>
  88. <tr>
  89. <td><input type="submit" name="search_cancel" value="Abandon"></td>
  90. <td><input type="submit" name="search_next" value="Auto Fix" /> </td>
  91. </tr>
  92. </table>
  93. </form>
  94. </center>
  95. <?PHP
  96. }
  97. if(isset($_POST['search_next'])){
  98.  
  99. /**
  100. * ensure all the selected checkboxes pass their ids to a new array
  101. *
  102. */
  103.  
  104. //print_r($_POST['dname']);
  105. echo "<br />";
  106. echo "<br />";
  107. //foreach ($locations as $key => $value) {
  108. $_SESSION['check_array'] = $_POST['dname'];
  109. //print_r($_SESSION['check_array']);
  110.  
  111. // where to pass control to????
  112. header('location:meet_fix.php');
  113. echo "<br />";
  114. exit();
  115. }else if (isset ($_POST['search_cancel'])){
  116. // return to my_meeting.php
  117. header('location:monthly.php');
  118. echo "<br />";
  119. exit();
  120.  
  121. }
  122.  
  123.  
  124.  
  125. ?>
  126. </body>
  127. </html>
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 5th, 2008
0

Re: query behaving funny

I can't see where you set the $e_date or $s_time variables.
Did you perhaps forget to create them?
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 6th, 2008
0

Re: query behaving funny

Quote ...
php Syntax (Toggle Plain Text)
  1. $sql = 'staff.emp_no, staff.fName, staff.lName, event.event_date, event.timeStart, event.timeEnd, event.emp_no, event.agenda FROM staff, event WHERE staff.emp_no = event.emp_no AND event.emp_no = "$check_array" AND event.event_date = "$e_date" AND timeStart = "$s_time"';
Quotations inside quotations. That just returns the string of the name of the variable and not what the value of the variable is. Also in addition, you need to add the method you are using at the beginning. I assume you are using the Select method. So try the following:
php Syntax (Toggle Plain Text)
  1. $sql = 'SELECT staff.emp_no, staff.fName, staff.lName, event.event_date, event.timeStart, event.timeEnd, event.emp_no, event.agenda FROM `staff`, `event` WHERE staff.emp_no = event.emp_no AND event.emp_no = "'.$check_array.'" AND event.event_date = "'.$e_date.'" AND staff.timeStart = "'.$s_time.'"';
-or-
php Syntax (Toggle Plain Text)
  1. $sql = 'SELECT staff.emp_no, staff.fName, staff.lName, event.event_date, event.timeStart, event.timeEnd, event.emp_no, event.agenda FROM `staff`, `event` WHERE staff.emp_no = event.emp_no AND event.emp_no = "'.$check_array.'" AND event.event_date = "'.$e_date.'" AND event.timeStart = "'.$s_time.'"';
In the 2 above code boxes, the only difference is which table the column timeStart is associated with at the end of the code. Hope that helps.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Nov 6th, 2008
0

Re: query behaving funny

Now that I've taken a closer look at the code, there are a couple of errors in there.

First, like cwarn23 mentioned, you forgot the SELECT in your query.

Second, you are putting your query directly into the mysql_fetch_array function.
The mysql_fetch_array function is meant to process the return value from a mysql_query function and return a single row (See http://php.net/mysql_fetch_array).

A typical MySQL query should look like this:
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Assuming you already opened a connection before this...
  3.  
  4. $sql = "SELECT * FROM someTable";
  5. $result = mysql_query($sql) or die(mysql_error());
  6.  
  7. echo "<table>";
  8. while($row = mysql_fetch_row($result)) {
  9. echo "<tr>";
  10. foreach($row as $col) {
  11. echo "<td>$col</td>";
  12. }
  13. echo "</tr>";
  14. }
  15. echo "</table>";
  16. ?>
Which would fetch and display the entire table.

Note that the mysql_fetch_array , mysql_fetch_row and mysql_fetch_assoc functions are siblings. That is; they all fetch a single row from the resource returned by a mysql_query call.
The difference is that the mysql_fetch_assoc returns an associative array, using the column names as keys while the mysql_fetch_row function returns a zero-indexed array.
The mysql_fetch_array returns a combination of both, like if you were to merge the results of the other two.
Last edited by Atli; Nov 6th, 2008 at 4:48 am. Reason: Spelling
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 6th, 2008
0

Re: query behaving funny

I have updated the code as you all requested.

I am using the
FOREACH ($_SESSION['check_array'] as $check_array)
because $_SESSION holds an array of IDS which I am using as reference to search the table 'event'.

Is there another way I can query the database with WHILE and not use the FOREACH because, I expected the results to be in one table but it didn't - this is what i got


10
Colleague(s) Date (d-m-y) Meeting Time Agenda Status
JohnAtkins 27-01-2008 1020 urgent meeting busy
JohnAtkins 27-01-2008 1020 brief discussion busy
JohnAtkins 27-01-2008 1020 trade summit busy
JohnAtkins 27-01-2008 1020 trade summit busy
JohnAtkins 27-01-2008 1020 Parents Association busy
JohnAtkins 27-01-2008 1020 discussion on new slogan busy
JohnAtkins 27-01-2008 1020 new slogan busy
JohnAtkins 27-01-2008 1020 build a list of slogans busy
JohnAtkins 27-01-2008 1020 build list of slogans busy
JohnAtkins 27-01-2008 1020 anything goes busy



10MikeFloyd27-01-20081020urgent meetingbusyMikeFloyd27-01-20081020brief discussionbusyMikeFloyd27-01-20081020trade summit busyMikeFloyd27-01-20081020trade summit busyMikeFloyd27-01-20081020Parents AssociationbusyMikeFloyd27-01-20081020discussion on new sloganbusyMikeFloyd27-01-20081020new sloganbusyMikeFloyd27-01-20081020build a list of slogansbusyMikeFloyd27-01-20081020build list of slogansbusyMikeFloyd27-01-20081020anything goesbusy

As you can see it was 2 seperate tables. I have included the updated query below.

What I want done is to check if the event date matches with another event in the database then I will have to automatically find another time within the event date that all the IDs referenced in FOREACH are free before writing it as a record
php Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3.  
  4. $met = mysql_connect("","","");
  5. if (!$met)
  6. {
  7. die('Could not connect: ' . mysql_error());
  8. }
  9. mysql_select_db("face2face", $met);
  10.  
  11. $e_date = $_SESSION['meet_date'];
  12. $s_time = $_SESSION['stime'];
  13.  
  14.  
  15. echo $e_date;
  16. echo $s_time;
  17. echo "<br />";
  18.  
  19. ?>
  20. <html>
  21. <head>
  22. <title>Search - Online Diary System</title>
  23. </head>
  24. <body bgcolor="#FFFFFF" link="#0000CC" vlink="#0000CC">
  25.  
  26. <p>&nbsp;</p>
  27. <p align="center"><img src="images/logo.GIF" width="543" height="72"></p>
  28. <center>
  29. <table width="75%" border="0">
  30. <tr>
  31. <td> <form name="searchf1" method="post" action="meet_list.php">
  32. <div align="center">
  33. <input type="submit" name="search1" value="Back">
  34. </form></td>
  35. </tr>
  36. </table>
  37. </center>
  38.  
  39. <body>
  40. <form name="check" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  41. <center>
  42. <?php
  43. echo "<table border='1'>
  44. <tr>
  45. <th> Colleague(s) </th>
  46. <th>Date (d-m-y)</th>
  47. <th>Meeting Time</th>
  48. <th>Agenda </th>
  49. <th>Status </th>";
  50.  
  51.  
  52.  
  53. // search db and put results into array
  54. FOREACH ($_SESSION['check_array'] as $check_array)
  55. {
  56.  
  57. $sql = 'SELECT staff.emp_no, staff.fName, staff.lName, event.event_date, event.timeStart, event.timeEnd, event.emp_no, event.agenda FROM `staff`, `event` WHERE staff.emp_no = event.emp_no AND event.emp_no = "'.$check_array.'" AND event.event_date = "'.$e_date.'" AND event.timeStart = "'.$s_time.'"';
  58.  
  59. $result = mysql_query($sql) or die(mysql_error());
  60.  
  61. // Mysql_num_row is counting table row
  62. $count=mysql_num_rows($result);
  63.  
  64. //
  65. echo "$count";
  66. // check $count
  67. if ($count==0)
  68. { // no records match - go ahead and write record
  69.  
  70. header('location:meet.php');
  71. //exit();
  72.  
  73. }
  74. elseif (!$count=0)
  75. {
  76. while ($e_row = mysql_fetch_array($result))
  77. {
  78. $status = 'busy';
  79. // print the list into a table
  80. echo "<tr>";
  81. echo "<td>" . $e_row['fName'] . $e_row['lName'] . "</td>";
  82. echo "<td>" . $e_row['event_date'] . "</td>";
  83. echo "<td>" . $e_row['timeStart'] . "</td>";
  84. echo "<td>" . $e_row['agenda'] . "</td>";
  85. echo "<td>" . $status ."</td>";
  86.  
  87.  
  88. }
  89.  
  90. }
  91.  
  92.  
  93. echo "</table>";
  94. echo "<br />";
  95. echo "<br />";
  96. ?>
  97.  
  98. <table>
  99. <tr>
  100. <td><input type="submit" name="search_cancel" value="Abandon"></td>
  101. <td><input type="submit" name="search_next" value="Auto Fix" /> </td>
  102. </tr>
  103. </table>
  104. </form>
  105. </center>
  106. <?PHP
  107. }
  108. if(isset($_POST['search_next'])){
  109.  
  110.  
  111.  
  112. //print_r($_POST['dname']);
  113. echo "<br />";
  114. echo "<br />";
  115. //foreach ($locations as $key => $value) {
  116. //$_SESSION['check_array'] = $_POST['dname'];
  117. //print_r($_SESSION['check_array']);
  118.  
  119. // where to pass control to????
  120. header('location:meet_fix.php');
  121. echo "<br />";
  122. exit();
  123. }else if (isset ($_POST['search_cancel'])){
  124. // return to my_meeting.php
  125. header('location:monthly.php');
  126. echo "<br />";
  127. exit();
  128.  
  129. }
  130.  
  131.  
  132.  
  133. ?>
  134. </body>
  135. </html>
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 6th, 2008
0

Re: query behaving funny

Hi Folks, I have sorted the FOREACH and the printing into a single table - it was due putting '}' at the wrong place.

The corrected part is:

php Syntax (Toggle Plain Text)
  1.  
  2. }
  3.  
  4. }
  5.  
  6. }
  7. echo "</table>";
  8. echo "<br />";
  9. echo "<br />";
  10. ?>
  11. <table>

What I want to do next is from the 'event' table during the printing of the details into the table, put the timeEnd value into an array for each event.emp_no as the key so that I can search the array for the highest value of the timeEnd and then add 100 to that value before performing another search until I can find 1 free time that all the staff are Free and can attend the meeting bearing in mind the search cannot go beyond 1700 (24 hour format)
Last edited by jackakos; Nov 6th, 2008 at 12:35 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 6th, 2008
0

Re: query behaving funny

From my last post, I have done some work but the following function is given errors because I may be doing something wrong with the array.

I have populated an array by using
php Syntax (Toggle Plain Text)
  1. // $_SESSION array to accept 'event' emp_no & timeEnd
  2. $endt=array($e_row['emp_no'] => $e_row['timeEnd']);

where $endt was declared as global.


php Syntax (Toggle Plain Text)
  1. function max_time()
  2. {
  3. // find out the highest figure in the array for the timeEnd, then add 100 for the next hour
  4.  
  5. FOREACH($endt as $key => $val)
  6. {
  7. if ($val == max($endt))
  8. {
  9. $_SESSION['my_key'] = $key;
  10. }
  11. }
  12.  
  13. add_time();
  14. }

can someone correct with the proper function to perform on the Array $endt

The error messages are:
1. Warning: Invalid argument supplied for foreach()

2. Fatal error: Call to undefined function add_time()

add_time() is a declared function in the same code.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 7th, 2008
0

Re: query behaving funny

The Warning is shown because the $endt variable you are using isn't an array.
This is probably because you didn't import it into the current scope, so as it is, the variable is undefined.
Consider this:
php Syntax (Toggle Plain Text)
  1. <?php
  2. // This defnies a varaible in the global scope
  3. $arr = array(0 => "value");
  4.  
  5. function doError()
  6. {
  7. // At this point, there is no variable named
  8. // $arr in the local scope. The $arr variable
  9. // defined in the global scope is not visible here.
  10.  
  11. // Which means that this foreach loop is using a
  12. // undefined variable, which generates a warning.
  13. foreach($arr as $_key => $_value)
  14. {
  15. // etc...
  16. }
  17. }
  18.  
  19. function doCorrect()
  20. {
  21. // This imports the $arr variable into
  22. // the current scope.
  23. global $arr;
  24.  
  25. // And now we are working with the global variable.
  26. foreach($arr as $_key => $_value)
  27. {
  28. // etc...
  29. }
  30. }
  31. ?>

Secondly, the fatal error there is being shown because you are calling a function that does not exist. If it did, this error wouldn't happen.
Make sure the spelling is correct and that all includes are in order.

If you can't find the problem, post the function here.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: sending the a combo box to another form
Next Thread in PHP Forum Timeline: how to share primary key?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC