what I am trying to do is extract elements of an array and running an sql query with the array element as the condition. The problem what I am facing is that the query does not return anything. The code is given below
//extracting the array elements
foreach ($t as $value) {
extract($value);
}
$sql = "SELECT * FROM daily_log where employee_log_id='$employee_log_id' AND log_date='$value'</br>";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['in_time'];
echo $row['out_time'];
The echo $row['in_time'] and echo $row['out_time']; does not show anything.
Can anybody help me to figure out what the problem is
Thanks in advance.
The problem is your sql query code is outside the foreach loop. The $value variable doesn't exist in that scope.
Try this:
foreach ($t as $value) {
extract($value);
$sql = "SELECT * FROM daily_log where employee_log_id='$employee_log_id' AND log_date='$value'</br>";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['in_time'];
echo $row['out_time'];
}
EDIT:
But I can't understand what you are trying to do. I think you either need the extract() function or the foreach loop. Also, why is there a </br> html tag in your sql query string?
Do you want something like this?
foreach ($t as $key -> $value) {
$sql = "SELECT * FROM daily_log where employee_log_id='$key' AND log_date='$value'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['in_time'];
echo $row['out_time'];
}
Related
I am having an issue with the code below. It will show only one row of 13 in the table. What am I doing wrong? I want it to show all the rows.
$query = 'SELECT xyz FROM mytable';
foreach ($query as $row) {
$row->column1;
}
$result = $row->column1;
echo $result."<br>\n";
You're not actually doing anything with the loop.
Putting the echo in the loop will solve this.
$query = 'SELECT xyz FROM mytable';
foreach ($query as $row) {
echo $row->column1."<br>\n";
}
Can someone please help? I'm new to PHP and struggling to make this bit of code to work. For example I have a sql database table with the following schema and data:
Type....rent_price
a..........100
b..........200
c..........300
I want to be able to echo say, "a", in one section and "200" in another. The following code will display "a" but then I can't seem to get it to display anything from the rent_price column using a second array.
$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");
for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row['type']);
for ($set1 = array (); $row = $result->fetch_assoc(); $set1[] =$row['rent_price']);
?>
<?php echo $set[0];?>
<?php echo $set1[1];?>
You loop through the results twice, without resetting. Try to loop only once:
$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");
$set = array ();
$set1 = array ();
while ($row = $result->fetch_assoc())
{
$set[] = $row['type'];
$set1[] =$row['rent_price'];
}
?>
<?php echo $set[0];?>
<?php echo $set1[1];?>
Depending on what you mean by '"a" in one section and "200" in another', you may be able to forgo creating the intermediate arrays and just print the values from your query as you fetch them. Two cells in a table row, for example:
while ($row = $result->fetch_assoc()) {
echo "<tr><td>$row[type]</td><td>$row[rent_price]</td></tr>";
}
your data is in the first element of array
$set1[0]
but youre probably better off maintaining the naming throughout
$results = array();
while ($row = $result->fetch_assoc()){
$results[] = $row;
}
foreach ($results as $result){
echo $result['type'];
echo $result['rent_price'];
}
OR
$results = array();
while ($row = $result->fetch_assoc()){
$results['types'][] = $row['type'];
$results['rent_prices'][] = $row['rent_price'];
}
foreach ($results['types'] as $type){
echo $type;
}
foreach ($results['rent_prices'] as $rent_price){
echo $rent_price;
}
while($row = mysql_fetch_assoc($interst_list_select_result))
it's work one value to many value
I need to work without while loop but i need to all value in table.
how to i work this??
You can use a foreach loop.
$stmt = $db->prepare('SELECT * FROM tbl');
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $row => $val){
echo $val['colname1'],' ',$val['colname2'];
}
If you need only the first result I think you can do this way
If( $row = mysql_fetch_assoc($interst_list_select_result)){
echo $row['you_column'];
}
if you need all
$rows = $stmt->fetchAll();
foreach ($rows as $row => $index){
echo $index['you_column'];
}
I have the following:
while($myRow = odbc_fetch_array( $result )){ <--lots of rows
$thisResult['name'] = $myRow["name"] ;
$thisResult['race'] = $myRow["race"] ;
$thisResult['sex'] = $myRow["sex"];
$thisResult['dob'] = $myRow["dob"];
}
I can't figure out how to print this back out.
I want to get each row and iterate through each row in the array like a datareader. I'm not sure what to do. I do not want to do the echo in the while. I need to be able to print it out elsewhere. But I don't think I've done it right here to be able to print it later.
I also tried, this, however:
while($myRow = odbc_fetch_array( $result )){ <--lots of rows
print($thisResult[$myRow["name"]] = $myRow);
}
I then tried:
while($myRow = odbc_fetch_array( $result )){ <--lots of rows
print (odbc_result($myRow,"name"));
}
but got an error.
Thank you for any help.
EDIT: when I do this:
while($myRow = odbc_fetch_array( $result )){
print ($myRow["name"]);
}
I get undefined index name. I am mainly concerned with saving to an array but I have to be able to do it in the loop first.
Declare an array before and assign the values to it:
$rows = array();
while($myRow = odbc_fetch_array( $result )){ <--lots of rows
$rows[] = $myRow;
}
Then you can print it e.g. this way:
foreach($rows as $row) {
foreach($row as $key => $value) {
echo $key . ': '. $value;
}
}
or however you want to.
You don't have to access and assign $thisResult['name'] = $myRow["name"] in your while loop as $myRow already is an array. You just copy the values which is unnecessary.
You say you have a lot of rows. Depending of what you really want to do with data, it might be better to put all this functionality into the while loop to avoid creating an array.
How about something like:
$output = '';
while($myRow = odbc_fetch_array( $result )) {
$output = $output."Your name is {$myRow["name"]} and your race is {$myRow["race"]}\n";
}
// print output later...
I'm having trouble getting my data from fetchAll to print selectively.
In normal mysql I do it this way:
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs)){
$id = $row['id'];
$n = $row['n'];
$k = $row['k'];
}
In PDO, I'm having trouble. I bound the params, then I'm saving the fetched data into $rs like above, with the purpose of looping through it the same way..
$sth->execute();
$rs = $query->fetchAll();
Now comes the trouble part. What do I do PDO-wise to get something matching the while loop above?! I know I can use print_r() or dump_var, but that's not what I want. I need to do what I used to be able to do with regular mysql, like grabbing $id, $n, $k individually as needed. Is it possible?
Thanks in advance..
It should be
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$n = $row['n'];
$k = $row['k'];
}
If you insist on fetchAll, then
$results = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row) {
$id = $row['id'];
$n = $row['n'];
$k = $row['k'];
}
PDO::FETCH_ASSOC fetches only column names and omits the numeric index.