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'];
}
Related
I want to put data in array which then put in excel file but it does not work.
$sql="SELECT `Jobc_id`, `Customer_name`, `Veh_reg_no`, `MSI_cat`, `Mileage` FROM `jobcard`";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
array( $row["job_id"],$row["Customer_name"],$row["Veh_reg_no"],$row["MSI_cat"],$row["Mileage"]);
}
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
Whereas, below code work..
$rows = array(
array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
How can I make first code to work :(
pls help
you are not assigning the array to a variable, therefore you can't access your fetched data after the while loop. This should fix it:
$rows = [];
while($row = $result->fetch_assoc()){
$rows[] = [$row["job_id"],$row["Customer_name"],$row["Veh_reg_no"],$row["MSI_cat"],$row["Mileage"]];
}
// now you can use $rows
I'm trying to get all data from table 1 in json format.
$arr = array();
$sql = "SELECT * FROM table1 ORDER BY price DESC";
$statement = $connect->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
foreach ($result as $val){
$arr['id'] = $val['item'];
$arr['price'] = $val['price'];
}
echo json_encode($arr);
Result:
{"id":"item00125","price":"112.35"}
The problem is that I get only one record. Why isn't the foreach(){ not sending all the records?
Try:
foreach ($result as $val){
$arr[] = array(
"id" => $val['item'],
"price" => val['price']
);
}
That's because your array is being over-written every time.
Try this:
$i = 0;
foreach ($result as $val){
$arr[$i]['id'] = $val['item'];
$arr[$i]['price'] = $val['price'];
$i++;
}
echo json_encode($arr);
You got only one result because you override array key at every itration of loop
Just pass result set into json_encode Without using loop as
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
here your result variable get the all records but the problem is in the for each loop you are put the each value in position of ['id'] and ['price'] so every time for each loop put the value at that same position so means replace the value so you are getting the last value so I thing you have to put the every record in different different position like this:
$i = 0;
foreach ($result as $val)
{
$arr[$i]['id'] = $val['item'];
$arr[$i]['price'] = $val['price'];
$i++;
}
echo json_encode($arr);
I hope it works fine
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;
}
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'];
}
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.