| | |
query behaving funny
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
If I run this query in the PHP Admin it displays 2 records
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
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.
php Syntax (Toggle Plain Text)
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 =201 AND event.event_date = "27-01-2008" 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)
$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.
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Keep PEACE alive!
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:
Try reversing the quotes in your query, replacing double-quotes with single-quotes and vise versa.
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)
$var = 'hi'; echo "Var = $var"; // Var = hi 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
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)
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)
<html> <head> <title>Search - Online Diary System</title> </head> <body bgcolor="#FFFFFF" link="#0000CC" vlink="#0000CC"> <p> </p> <p align="center"><img src="images/logo.GIF" width="543" height="72"></p> <center> <table width="75%" border="0"> <tr> <td> <form name="searchf1" method="post" action="meet_list.php"> <div align="center"> <input type="submit" name="search1" value="Back"> </form></td> </tr> </table> </center> <?php $link = mysql_connect('localhost','db', 'database') or die('Could not connect: ' . mysql_error()); // echo 'Connected successfully'; mysql_select_db("online_diary",$link) or die ('Could not connect ' .mysql_error()); // search db and put results into array FOREACH ($_SESSION['check_array'] as $check_array) { $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'"; $result=mysql_fetch_array($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); echo "$count"; // check $count if ($count==0) { // no records match - go ahead and write record /** * $face2face = new meet; * $face2face->w_meet(); */ header('location:meet.php'); exit(); } if ($count>0) { // print the list into a table ?> <body> <form name="check" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <center> <?php echo "<table border='1'> <tr> <th> Colleague(s) </th> <th>Date (d-m-y)</th> <th>Meeting Time</th> <th>Agenda </th> <th>Status </th>"; // use a while on the array to print the contents of the array while ($e_row = mysql_fetch_array($result)){ // $_SESSION['e_search'] $status = 'busy'; echo "<tr>"; echo "<td>" . $e_row['fName'] . $e_row['lName'] . "</td>"; echo "<td>" . $e_row['event_date'] . "</td>"; echo "<td>" . $e_row['timeStart'] . "</td>"; echo "<td>" . $e_row['Agenda'] . "</td>"; echo "<td>" . $status ."</td>"; } } echo "</table>"; echo "<br />"; echo "<br />"; ?> <table> <tr> <td><input type="submit" name="search_cancel" value="Abandon"></td> <td><input type="submit" name="search_next" value="Auto Fix" /> </td> </tr> </table> </form> </center> <?PHP } if(isset($_POST['search_next'])){ /** * ensure all the selected checkboxes pass their ids to a new array * */ //print_r($_POST['dname']); echo "<br />"; echo "<br />"; //foreach ($locations as $key => $value) { $_SESSION['check_array'] = $_POST['dname']; //print_r($_SESSION['check_array']); // where to pass control to???? header('location:meet_fix.php'); echo "<br />"; exit(); }else if (isset ($_POST['search_cancel'])){ // return to my_meeting.php header('location:monthly.php'); echo "<br />"; exit(); } ?> </body> </html>
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Keep PEACE alive!
•
•
•
•
php Syntax (Toggle Plain Text)
$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"';
php Syntax (Toggle Plain Text)
$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.'"';
php Syntax (Toggle Plain Text)
$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.'"';
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
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
The
A typical MySQL query should look like this:
Which would fetch and display the entire table.
Note that the
The difference is that the
The
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)
<?php // Assuming you already opened a connection before this... $sql = "SELECT * FROM someTable"; $result = mysql_query($sql) or die(mysql_error()); echo "<table>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $col) { echo "<td>$col</td>"; } echo "</tr>"; } echo "</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
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
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)
<?php session_start(); $met = mysql_connect("","",""); if (!$met) { die('Could not connect: ' . mysql_error()); } mysql_select_db("face2face", $met); $e_date = $_SESSION['meet_date']; $s_time = $_SESSION['stime']; echo $e_date; echo $s_time; echo "<br />"; ?> <html> <head> <title>Search - Online Diary System</title> </head> <body bgcolor="#FFFFFF" link="#0000CC" vlink="#0000CC"> <p> </p> <p align="center"><img src="images/logo.GIF" width="543" height="72"></p> <center> <table width="75%" border="0"> <tr> <td> <form name="searchf1" method="post" action="meet_list.php"> <div align="center"> <input type="submit" name="search1" value="Back"> </form></td> </tr> </table> </center> <body> <form name="check" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <center> <?php echo "<table border='1'> <tr> <th> Colleague(s) </th> <th>Date (d-m-y)</th> <th>Meeting Time</th> <th>Agenda </th> <th>Status </th>"; // search db and put results into array FOREACH ($_SESSION['check_array'] as $check_array) { $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.'"'; $result = mysql_query($sql) or die(mysql_error()); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // echo "$count"; // check $count if ($count==0) { // no records match - go ahead and write record header('location:meet.php'); //exit(); } elseif (!$count=0) { while ($e_row = mysql_fetch_array($result)) { $status = 'busy'; // print the list into a table echo "<tr>"; echo "<td>" . $e_row['fName'] . $e_row['lName'] . "</td>"; echo "<td>" . $e_row['event_date'] . "</td>"; echo "<td>" . $e_row['timeStart'] . "</td>"; echo "<td>" . $e_row['agenda'] . "</td>"; echo "<td>" . $status ."</td>"; } } echo "</table>"; echo "<br />"; echo "<br />"; ?> <table> <tr> <td><input type="submit" name="search_cancel" value="Abandon"></td> <td><input type="submit" name="search_next" value="Auto Fix" /> </td> </tr> </table> </form> </center> <?PHP } if(isset($_POST['search_next'])){ //print_r($_POST['dname']); echo "<br />"; echo "<br />"; //foreach ($locations as $key => $value) { //$_SESSION['check_array'] = $_POST['dname']; //print_r($_SESSION['check_array']); // where to pass control to???? header('location:meet_fix.php'); echo "<br />"; exit(); }else if (isset ($_POST['search_cancel'])){ // return to my_meeting.php header('location:monthly.php'); echo "<br />"; exit(); } ?> </body> </html>
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Keep PEACE alive!
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:
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)
The corrected part is:
php Syntax (Toggle Plain Text)
} } } echo "</table>"; echo "<br />"; echo "<br />"; ?> <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.
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Keep PEACE alive!
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
where $endt was declared as global.
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.
I have populated an array by using
php Syntax (Toggle Plain Text)
// $_SESSION array to accept 'event' emp_no & timeEnd $endt=array($e_row['emp_no'] => $e_row['timeEnd']);
where $endt was declared as global.
php Syntax (Toggle Plain Text)
function max_time() { // find out the highest figure in the array for the timeEnd, then add 100 for the next hour FOREACH($endt as $key => $val) { if ($val == max($endt)) { $_SESSION['my_key'] = $key; } } add_time(); }
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.
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Keep PEACE alive!
The Warning is shown because the
This is probably because you didn't import it into the current scope, so as it is, the variable is undefined.
Consider this:
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.
$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)
<?php // This defnies a varaible in the global scope $arr = array(0 => "value"); function doError() { // At this point, there is no variable named // $arr in the local scope. The $arr variable // defined in the global scope is not visible here. // Which means that this foreach loop is using a // undefined variable, which generates a warning. foreach($arr as $_key => $_value) { // etc... } } function doCorrect() { // This imports the $arr variable into // the current scope. global $arr; // And now we are working with the global variable. foreach($arr as $_key => $_value) { // etc... } } ?>
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.
![]() |
Other Threads in the PHP Forum
- Previous Thread: sending the a combo box to another form
- Next Thread: how to share primary key?
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube






