Get all mysql selected rows into an array
I would suggest the use of MySQLi or MySQL PDO for performance and security purposes, but to answer the question:
while($row = mysql_fetch_assoc($result)){
$json[] = $row;
}
echo json_encode($json);
If you switched to MySQLi you could do:
$query = "SELECT * FROM table";
$result = mysqli_query($db, $query);
$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json );
PHP - How to get all rows from a MySQL Query
Try following code:
$result = mysql_query("SELECT * FROM messageInfo WHERE senderUsername='$username' OR recieverUsername='$username'");
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
I think that UNION
is not really needed in this query, OR
statement would be just enough.
How fetch all rows which are 10 min apart in single query in PHP MySQL?
This is more complicated than you think. You cannot just look over intervals between consective rows, you need to keep track of the last row that was selected to identify the next one. This implies an iterative process; in SQL, this is usually implemented using a recursive query - which MySQL supports starting version 8.0 only.
Consider:
with recursive cte as (
select value, time
from device
where time = (select min(time) from device)
union all
select d.value, d.time
from cte c
inner join device d on d.time = (
select min(d1.time) from device d1 where d1.time >= c.time + interval 10 minute
)
)
select * from cte
Selecting Any Row in MySQL Result
fetch_all() may help you to fetch all rows at once.
If you cannot use fetch_all(), you can still use fetch_assoc() cycle just once:
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
var_dump($rows[4]);
Fetch all rows based on the query into an array
$result = mysql_query("SELECT * FROM $tableName");
$arr_json = array();
while ($row = mysql_fetch_assoc($result)) {
$json = json_encode($row);
$arr_json[] = $json;
}
EDIT: Looking a j08691's answer, it looks like I might have misunderstood.
Anyway, if you don't know how many columns you have, do this:
$arr = array();
while ($row = mysql_fetch_assoc($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
PHP- How to get all rows from a table satisfying a condition?
Use this,
$query = mysql_query("SELECT fromemail,toemail FROM invitations WHERE sent = 1");
if(mysql_num_rows($query)!=0)
{
while($row = mysql_fetch_assoc($query)) {
echo $row['toemail'];
echo $row['fromemail];
}
}
MySQL and PHP - how to display all rows where field value equals x?
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$result = mysql_query("SELECT Player, Team, Pass_Yds, Pass_TDs, Int_Thrown, Rush_Yds, Rush_TDs, Overall_Pts, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'QB' ORDER BY Pass_Yds DESC;");
while($row = mysql_fetch_array($result))
{
echo $row['Player'];
echo $row['Team'];
....
}
mysql_close($con);
?>
Related Topics
Replace Deprecated Preg_Replace /E with Preg_Replace_Callback
How to Take an Array, Divide It by Two and Create Two Lists
Detecting File Upload Size on the Client Side
Laravel 5.3 Withcount() Nested Relation
Turn Plain Text Urls into Active Links Using PHP
How to Convert Array to a String Using Methods Other Than JSON
When and Why Should $_Request Be Used Instead of $_Get/$_Post/$_Cookie
Fatal Error: Call to a Member Function Query() on Null
Parsing JavaScript (Not JSON) in PHP
How to Access a Variable Across Two Files
Inserting Utf-8 Encoded String into Utf-8 Encoded MySQL Table Fails with "Incorrect String Value"