I have Two MySQLi Queries and two result variables as below
$result = mysqli_query($con, $query);
$result1 = mysqli_query($con, $query1);
what I want to do is make an array of all results , but the condition is it must contain one result from $result then the next one should be from $result1 and next one from $result and so on until both variables have values.
while($row = mysqli_fetch_assoc($result))
{
$links[] = $row["url"];
while($row1 = mysqli_fetch_assoc($result1))
{
$links[] = $row1["ads_id"];
}
}
Currently I have this this works like this , adds One value from $results next adds all values from $results1 and next one by one adds all values from $result .
Can anybody help me out with this logic, I can not work out the logic for this thing.
You could try something like this. It loops through $result and $result1 at the same time, finishing up with any leftover rows from $result1:
while ($row = mysqli_fetch_assoc($result)) {
$links[] = $row["url"];
if ($row1 = mysqli_fetch_assoc($result1))
$links[] = $row1["ads_id"];
}
while ($row1 = mysqli_fetch_assoc($result1)) {
$links[] = $row1["ads_id"];
}
Related
I am fetching records as follows:
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'] = $row;
}
This returns only the last record in the table. I need to return all records from the sql statement.
change you $aResult['query_result'] = $row; to $aResult['query_result'][] = $row;
You've override the result each time, so you just get one.
It seems your loop constantly overwrites the value and hence you will only ever seen the last row. I think you might see better results if you do something like:
while($row = mysqli_fetch_array($result))
{
$aResult[] = $row;
}
so that each new row gets appended to your array
Try with following code, currently You are initiating the values to the same array key :
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'][] = $row;
}
for more Detail On Array
I need to append result of mysql query with existing result of another query, and both queries performed on different table, actually what I really want to do is take value from main table and get some data from another table using first value, and combine all together using json encode.
$query = "select ID,TIMESTAMP,UUID from TABLE1 where USERNAME='$user'";
$result = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($result);
if($numrows>0)
{
$res=$a;
$myArray = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$eachuuid = $row[UUID];
$query1 = "select ID,IMAGEURL from TABLE_IMAGES where IMG_UUID='$eachuuid'";
$result1 = mysqli_query($conn, $query1);
$numrows1 = mysqli_num_rows($result);
if($numrows1>0){
$res1;
$myArray1 = array();
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$myArray1[] = $row1;
}
$row['IMG_URL']=$myArray1;
}
$myArray[] = $row;
}
$res['Data']=$myArray;
echo json_encode($res);
Result:
{"Response":"OK","Data":[{"ID":"62",
"TIMESTAMP":"26 January",
"UUID":"12345",
"IMG_URL":[{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}]}]}
And I need to get the output like,
{"Response":"OK","Data":[{"ID":"62",
"TIMESTAMP":"26 January",
"UUID":"12345",
"IMG_URL":{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}}]}
Means need to remove [] from IMG_URL section so that I can parse the data easily.
You can do this
$row['IMG_URL']=$myArray1[0];
Or
$myArray1 = null;
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$myArray1 = $row1;
}
$row['IMG_URL']=$myArray1;
I hope this helps.
$query = "SELECT * FROM table WHERE data = '$userinput'";
$row = mysql_query($query);
while ($row = mysql_fetch_array($row))
{
echo $row['data'];
}
Ok So my questions are:
What exactly does mysql_fetch_array return?
AND if my WHERE clause finds multiple matches, shouldn't the loop execute numerous times?
I am running a program and I can only get the first result to print
You're overwriting $row. Instead use a different variable for the query result.
$query = "SELECT * FROM table WHERE data = '$userinput'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo $row['data'];
}
Try using mysql_fetch_assoc($row)
You should not use the same variable as the index and the loop variable.
$row = mysql_query
while( $row = ...
Replace the first $row with $result
Hy,
I'm new in php and I'm having some problems with mysql_fetch_array().
$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";
$result = mysql_query($sql);
$list = mysql_fetch_array($result);
There are more than 100 entries in the database but the mysql_fetch_array() delivers only one. When I'm trying it with a while-loop it isn't working either.
Here it goes with my while-loop
$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";
$result = mysql_query($sql);
while($list = mysql_fetch_array($result));
Update:
You are not doing anything inside your loop:
while($list = mysql_fetch_array($result));
Try:
while($list = mysql_fetch_array($result){
echo $list['mandant_kurz'];
}
Also try running your query in MySQL client to make sure it actually returns 100 rows.
You will have to use loop:
while($row = mysql_fetch_array($result)){
echo $row['mandant_kurz'];
}
This echoes just first row.
$list = mysql_fetch_array($result);
echo $list['mandant_kurz'];
Moves pointer to first row and echoes all rows
mysql_data_seek($result,0);
while( $list = mysql_fetch_array($result) ) {
echo $list['mandant_kurz'];
}
I don't know if i am doing it right, this is what I got:
while($row = mysqli_fetch_assoc($result)) {
$items = mysqli_num_rows($row);
}
It always sets $items = to 1 for some reason.
Here is my mysqli_query...
$top10_query = "SELECT * FROM users WHERE userid='$userid'";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
$row = mysqli_fetch_assoc($result);
Well, $row only contains one row so....
$items = mysqli_num_rows($result)
should give you the correct number of items
Anyway, why are you doing that in a loop? The number of rows is constant...