I have never used array_combine before and I am getting a "Boolean instead of resource" error in the query. If I change the query to read ... WHERE cal_id = " . $quidx . "; the Boolean error goes away but I get unexpected T_STRING errors later in the script for reasons I can't figure, and the outcome of the query is nothing.
Main question: Is this the proper usage for array_combine? If so, what am I missing in the rest of this script that causes no information after the query? $quid1 is an array of id numbers and $tm is an array of Unix timestamps. Both arrays check out to have the same number of rows consistently. After array_combine, $cls1 returns a valid array of both previous arrays but they don't seem to work in the query.
Thanks for helping. I'm still learning.
I have edited the script to include the new query statement. Problems now are with the foreach statements where the error reads invalid argument.
$cls1 = array_combine($quid1, $tm);
$quidx = array_values($quid1);
$quclx = array_values($tm);
//// note to self.. start final query for email write with new id data, likely redundant.
$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";
$wemail = mysql_query($qumail);
while ($row = mysql_fetch_array($wemail, MYSQL_NUM)) {
$quname2 = $row[1];
$qtime = $row[2]
$qudesc2 = $row[3];
Here's the complete script section where this is applicable. I'm still pecking through it so it's not all fixed yet:
$cls1 = array_combine($quid1, $tm);
$quidx = array_values($quid1);
$quclx = array_values($tm);
//// note to self.. start final query for email write with new id data, likely redundant.
$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";
$wemail = mysql_query($qumail);
while ($row = mysql_fetch_array($wemail, MYSQL_NUM)) {
$quname2 = $row[1];
$qtime = $row[2]
$qudesc2 = $row[3];
}
foreach ($qtime as $key=>$btUx) {
if (strlen($btUx) < 6){
$btUx = '0' . $btUx;
date_default_timezone_set('UTC');
$unixEpoch = strtotime($btUx);
date_default_timezone_set('America/Denver');
$formtime = date("H:i", $unixEpoch);
}
}
foreach ($tm as $key=>$tf) {
$idnotime = 0;
$idnow = (strlen($tf) > 2);
switch($tf) {
case $idnow:
$repmlnow = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[1], $formtime, $row[3]);
break;
case $idnotime:
$repmlnotm = sprintf("Event: %s \nDesc: %s \n\n", $row[1], $row[3]);
break;
}
}
/////===================== send mail...
This is my last edit unless there are other comments. I have changed the original script to the following and everything seems to work properly in this case thanks to the contributor below...
$cls1 = array_combine($quid1, $tm);
$quidx = array_values($quid1);
$quclx = array_values($tm);
$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";
$wemail = mysql_query($qumail);
while ($row = mysql_fetch_array($wemail, MYSQL_NUM)) {
$quname2 = $row[1];
$qtime = $row[2];
$qudesc2 = $row[3];
}
if(strlen($qtime) < 6){
$btUx = '0' . $qtime;
date_default_timezone_set('UTC');
$unixEpoch = strtotime($btUx);
date_default_timezone_set('America/Denver');
$formtime = date("H:i", $unixEpoch);
}elseif(strlen($qtime) > 5){
date_default_timezone_set('UTC');
$unixEpoch = strtotime($value);
date_default_timezone_set('America/Denver');
$formtime = date("H:i", $unixEpoch);
}
foreach ($quclx as $key=>$tf) {
$idnotime = 0;
$idnow = (strlen($tf) > 2);
switch($tf) {
case $idnow:
$repmlnow = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $quname2, $formtime, $qudesc2);
break;
case $idnotime:
$repmlnotm = sprintf("Event: %s \nDesc: %s \n\n", $quname2, $qudesc2);
break;
}
}
/////===================== send mail...
$quidx is an array with numerical index as key and $quid1 array values as it value. try change query become:
$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";
Related
I am having a huge issue looping through results, These two queries work hand in hand to check if a restaurant is open today. My problem is i have restaurants, id 1-5(more in the future). But the loop seems to only get restaurant id 5. I have read many posts on here and it seems like i am doing the right thing. But i cannot seem to loop to get the other restaurant id's.
I am blocked now, newbie who is very open to any suggestions or advise.
$sel = "SELECT Rest_Details.Resturant_ID,Delivery_Pcode.Pcode,Delivery_Pcode.Restaurant_ID
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE'$searchP'";
$res = $dbc->query($sel);
if (!$res) {
echo "invalid query '" . mysqli_error($dbc) . "\n";
}
$i=1;
while ($row_res = $res->fetch_array()) {
$rest_ = $row_res['Resturant_ID'];
$i++;
}
date_default_timezone_set("Europe/London");
$daynum = jddayofweek(unixtojd());
$query = "SELECT *
FROM Opening_hrs WHERE
Restaurant_ID = $rest_
AND Day_of_week = $daynum";
$run_qu = $dbc->query($query);
if ($run_qu->num_rows > 0) {
while ($row_qu = $run_qu->fetch_assoc()) {
$message = "open" . $row_qu["Open_time"] . "</br>";
}
} else {
$message = $message . "close" . $row_qu["Closing_time"] . "</br>";
}
You could either output whatever you want to within your loop or build-up an output string because the value of $rest_ will always be the last value in the loop and i don't think that's what you want... Again you are doing the same with $message. And I am willing to bet that this is what you want to do:
<?php
date_default_timezone_set("Europe/London");
$sel = "SELECT Rest_Details.Resturant_ID,Delivery_Pcode.Pcode,Delivery_Pcode.Restaurant_ID
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE'$searchP'";
$res = $dbc->query($sel);
if (!$res) {
echo "invalid query '" . mysqli_error($dbc) . "\n";
}
$i=1;
while ($row_res = $res->fetch_array()) {
$rest_ = $row_res['Resturant_ID'];
$i++; // <== YOU DON'T NEED THIS VARIABLE....
// GET THE DATES WITHIN THE LOOP...
$daynum = jddayofweek(unixtojd());
$query = "SELECT *
FROM Opening_hrs WHERE
Restaurant_ID = $rest_
AND Day_of_week = $daynum";
$run_qu = $dbc->query($query);
if ($run_qu->num_rows > 0) {
while ($row_qu = $run_qu->fetch_assoc()) {
$message = "open" . $row_qu["Open_time"] . "</br>";
}
} else {
$message = $message . "close" . $row_qu["Closing_time"] . "</br>";
}
}
I think this is what you are trying to do.
// $searchP should be checked to prevent SQL injection.
$sel = "SELECT Rest_Details.Resturant_ID, Delivery_Pcode.Pcode,
Delivery_Pcode.Restaurant_ID
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_IDW
WHERE Delivery_Pcode.Pcode LIKE '$searchP'";
$res = $dbc->query($sel);
if (!$res) {
echo "invalid query '" . mysqli_error($dbc) . "\n";
}
// set these once as they don't change
date_default_timezone_set("Europe/London");
$daynum = jddayofweek(unixtojd());
// $i=1; - not required, never used
// loop over the original results
while ($row_res = $res->fetch_array()) {
$rest_ = $row_res['Resturant_ID'];
//$i++; not used
// check for a match
$query = "SELECT * FROM Opening_hrs
WHERE Restaurant_ID = $rest_
AND Day_of_week = $daynum";
$run_qu = $dbc->query($query);
if ($run_qu->num_rows > 0) {
// at least one match
while ($row_qu = $run_qu->fetch_assoc()) {
$message = "open" . $row_qu["Open_time"] . "<br />";
$message .= "close" . $row_qu["Closing_time"] . "<br />";
}
} else {
// no matches
$message = "No results for <i>$daynum</i>.";
}
}
It should be possible to get the details in a single query, but I would need to see your SQL tables for that (and you did not ask for that too :]).
Also, it is <br> or <br />, not </br>.
Please, can anyone tell me what I'm doing wrong here?
$unir = "";
$equxflo = $DB_con->prepare("SELECT idequipo, CONCAT(equipo.nombre,' ',equipo.modelo,' ',equipo.marca,' ',equipo.serieplaca) AS flota FROM equipo WHERE idusuario = '$idu' ORDER BY idequipo DESC");
$equxflo->execute();
while($row = $equxflo->fetch(PDO::FETCH_ASSOC)){
print_r($row); //show me all the array
$ht = $row['flota'];
$ie = $row['idequipo'];
$unir = "["."'"."$ht"."'".","."$ie"."]".",";
}
print_r($unir); //show me only la last data
The problem is because of this line:
$unir = "["."'"."$ht"."'".","."$ie"."]".",";
^ missing concatenation operator
for example I like the show in $unir this ['a',3],['a',2],['a',1], but only show ['a',1] ...
Based on your requirement, do this:
$unir = "";
$equxflo = $DB_con->prepare("SELECT idequipo, CONCAT(equipo.nombre,' ',equipo.modelo,' ',equipo.marca,' ',equipo.serieplaca) AS flota FROM equipo WHERE idusuario = :idu ORDER BY idequipo DESC");
$equxflo->bindParam(":idu", $idu);
if ($equxflo->execute()) {
while ($row = $equxflo->fetch(PDO::FETCH_ASSOC)){
$ht = $row['flota'];
$ie = $row['idequipo'];
$unir .= "['" . $ht . "'," . $ie . "],";
}
print_r($unir);
}
Better Solution:
A better solution would be to declare $unir as an array and put data in a temporary array and push it to $unir array in each iteration, like this:
$unir = array();
$equxflo = $DB_con->prepare("SELECT idequipo, CONCAT(equipo.nombre,' ',equipo.modelo,' ',equipo.marca,' ',equipo.serieplaca) AS flota FROM equipo WHERE idusuario = :idu ORDER BY idequipo DESC");
$equxflo->bindParam(':idu', $idu);
if ($equxflo->execute()) {
while ($row = $equxflo->fetch(PDO::FETCH_ASSOC)) {
$tmp_array = array($row['flota'], $row['idequipo']);
$unir[] = $tmp_array;
}
print_r($unir);
}
I would recode it in this manner, notice the bindParam method. Also you are overwriting the result set per every iteration. I added a variable $final_result[] which stores the results:
$equxflo = $DB_con->prepare("SELECT idequipo,
CONCAT(
equipo.nombre,' ',
equipo.modelo,' ',
equipo.marca,' ',
equipo.serieplaca) AS flota
FROM equipo
WHERE idusuario = :idu
ORDER BY idequipo DESC
");
$equxflo->bindParam(":idu", $idu);
if ($equxflo->execute()) {
$final_result = array();
while ($row = $equxflo->fetch(PDO::FETCH_ASSOC)) {
print_r($row); //show me all the array
$ht = $row['flota'];
$ie = $row['idequipo'];
// I DONT UNDERSTAND WHAT YOU ARE DOING WITH $unir
// IS IT JSON? IS IT AN ARRAY? ...EXPLAIN
$unir = "[" . "'" . "$ht" . "'" . "," . "$ie" . "]" . ",";
$final_result[] = $row; // <-- YOU MISSED THIS
}
print_r($final_result); //shows all result
print_r(json_encode($final_result)); //shows result in JSON
print_r(end($final_result)); //show only last result set
}
I'm trying to store the title(summary) and date(created) of an event in an array. But I think im missing something in my loop.
<?php
$summary = array();
$date = array();
mysql_connect('mysql.server', 'myUsername', 'myPass') or die('Could not connect: ' . mysql_error());
mysql_select_db("mxgsite") or die(mysql_error());
$query_summary = mysql_query('SELECT summary FROM event_info') or die(mysql_error());
$query_date = mysql_query('SELECT created FROM event_details') or die(mysql_error());
$row_summary = mysql_fetch_array($query_summary);
$row_date = mysql_fetch_array($query_date);
$i = 0;
while(($row1 = mysql_fetch_array($query_summary))) {
$row2 = mysql_fetch_array($query_date);
$summary[] = $row['summary'];
$date[] = $row['created'];
echo $summary[$i] . " " . $date[$i] . "<br ?>";
$i++;
}
I know i'm getting values because I can echo out 1 value, but if I want to put all the values in an array and try to echo out that array I keep getting blank values?
It seems to me like you are trying to do too many things here. Since the 2 sets of values are not being stored in a way where they are related/linked to each other, you might as well deal with them in separate while loops. Try something like this:
while ($row = mysql_fetch_array($query_summary)){
$summary[] = $row[0];
}
while ($row = mysql_fetch_array($query_date)){
$date[] = $row[0];
}
If you want to relate the tables, per the comments above, you can try something more like:
$result = mysql_query('SELECT a.eventid, a.summary, b.created
FROM event_info a
join event_details b
on a.eventid = b.eventid');
$events = array();
while ($row = mysql_fetch_array($result)){
$event = array();
foreach ($row as $key=>$value){
$event[$key]=$value;
}
$events[] = $event;
}
I'm having great trouble with "INSERT INTO"...
I have a variable part number so this my code...:
<?php
include ("db_conn.php");
$mem_id = "1";
$descript = "chair";
$qualifier = "sitting";
$major = "Y";
$value = "6";
//$mesh_cell_string = "tree_0,tree_1,tree_2,tree_3,tree_4";
//$mesh_values_string = "'C23','550','291','687','500'";
$part_number = "C23.550.291.687.500";
$parts = explode('.', $part_number);
$n = 0;
foreach ($parts as $something => $number)
{
$mesh_cell_string .= "tree_" . $n . ",";
$mesh_values_string .= "'" . $number . "'," ;
$n++;
}
$mesh_values_string = substr($mesh_values_string, 0, -1);
$mesh_cell_string = substr($mesh_cell_string, 0, -1);
$insert_string = "mem_id,mesh_heading_name," . $mesh_cell_string . ",qualifier_name,major,rank";
$values_string = "'$mem_id','$descript'," .$mesh_values_string. ",'$qualifier','$major','$value'";
$sql = "INSERT INTO mesh_table (" . $insert_string .") VALUES (" . $values_string .")";
$result = mysqli_query($cxn,$sql) or die ("couldn't execute the query");
?>
The strange thing is... i don't get an error ("couldn't execute the query") so i thought it went alright but when i look into my database there aren't any values written... when i un-comment the the 2 variables:
//$mesh_cell_string = "tree_0,tree_1,tree_2,tree_3,tree_4";
//$mesh_values_string = "'C23','550','291','687','500'";
And comment the foreach loop, it works...? So there goes something wrong in the foreach loop, but when i echo the $sql on both methods i get the same:
INSERT INTO mesh_table (mem_id,mesh_heading_name,tree_0,tree_1,tree_2,tree_3,tree_4,qualifier_name,major,rank) VALUES ('1','Chair','C23','550','291','687','500','sitting','Y','6')
I really don't know what i am doing wrong...?
Best regards,
Thijs
change $values_string = "'$mem_id','$descript'," .$mesh_values_string. ",'$qualifier','$major','$value'";
To
$values_string = "'".$mem_id."','".$descript."'," .$mesh_values_string. ",'".$qualifier."','".$major."','".$value."'";
I am wanting to not echo out the comma at the end of the echo after the last row. How can I do that? Here is my code:
<?php
header("Content-type: application/json");
echo '{"points":[';
mysql_connect("localhost", "user", "password");
mysql_select_db("database");
$q = "SELECT venues.id, venues.lat, venues.lon, heat_indexes.temperature FROM venues, heat_indexes WHERE venues.id = heat_indexes.venue_id";
$res = mysql_query($q) or die(mysql_error());
while ($point = mysql_fetch_assoc($res)) {
echo $point['lat'] . "," . $point['lon'] . "," . $point['temperature'] . ",";
}
mysql_free_result($res);
echo ']}';
?>
Could you not use json_encode() instead, rather than hand-crafting the JSON?
$result = array();
//snip
while ($point = mysql_fetch_assoc($res)) {
$result[] = $point['lat'];
$result[] = $point['lon'];
$result[] = $point['temperature'];
}
//snip
header("Content-type: application/json");
echo json_encode( array('points' => $result) );
Use a count query to get the number of rows to begin with.
$query= "SELECT COUNT(*) FROM venues";
$count= mysql_query($q);
Then introduce a conditional and a count decrease each time through...
while ($point = mysql_fetch_assoc($res)) {
$count = $count - 1;
if ($count == 1) {
echo $point['lat'] . "," . $point['lon'] . "," . $point['temperature'];
} else {
echo $point['lat'] . "," . $point['lon'] . "," . $point['temperature'] . ",";
}
Json encode would probably be your best bet, but you could also use trim();
Rather than echoing in the while loop, append to a variable. Once outside the while loop, use $output = trim($output, ',') to remove trailing commas.
About the comma problem, I always target the first item instead of the last:
$first = true;
while ($point = mysql_fetch_assoc($res)) {
if ($first)
{
$first = false;
}
else
{
echo ",";
}
echo $point['lat'] . "," . $point['lon'] . "," . $point['temperature'];
}
You can use mysql_num_rows() to find out how many rows are in the result set passed back from your last query.
...
$res = mysql_query($q) or die(mysql_error());
$num_rows = mysql_num_rows($res);
Then combine that with Scotts answer and you should be set.