sorry but complete newbie to php and mysqli. I have this in my code:
$sql = "SELECT tutorial_title, tutorial_author FROM tutorial_info WHERE tutorial_id<=3;";
$result = $conn->query($sql);
if($result===FALSE) {
echo "Select failed <br>";
}
else {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Tutorial Title: " . $row["tutorial_title"]. " - Name: " . $row["tutorial_author"]. "<br>";
}
} else {
echo "0 results";
}
This works fine but I don't understand how the line while($row = $result->fetch_assoc()) works. From what I understand it fetches an array so I have something like while($row=$somearray). Why does this iterate though all the rows though? Does fetch_assoc() have a loop built in that iterates though all the rows and returns one at a time?
THere are different answer but one is:
mysqli_fetch_assoc — Fetch a result row as an associative array.
Also, Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.
Read mysqli_fetch_assoc in detail.
Related
I am new to php, but I had used while loop and it worked. But in this case i don't know why it is not working.
I am using following query to fetch data from mysql data base.
$query="SELECT * FROM `groupMembers` WHERE group_id = '$group_id'";
if ($result = mysql_query($query)) {
# code...
echo mysql_num_rows($result);
}
It prints 2 as number of rows. But the problem is in following while loop:-
while ($data = mysql_fetch_array($result)) {
# code...
echo $data['member_id'];
}
I prints only one member's id. (the second one == member_2)
The above query returns 2 rows when run in mysql :-
member_id | group_id
----------|----------
member_1 | group_1
member_2 | group_1
i had same problem,i was calling following code two times
mysql_fetch_array($result)
Show more code - what happens before while
first try mysql_num_rows in how many rows is returns.
if "0" then can't fetch any record from your mysql and check your sql query. if return no of row then try below code:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "<br>";
}
} else {
echo "0 results";
}
Enjoy Coding.
That is because when it create array for your MySQL query it will change me with to same value "group_1"
Try to convert mysqli_fetch_array to mysqli_fetch_assoc instead
$query="SELECT * FROM `groupMembers` WHERE group_id = '$group_id'";
Here,$group_id can't be place between '';
use $results->fetch_assoc(); which get your data
<?php
// your database connection
if ($results->num_rows > 0) {
while ($row = $results->fetch_assoc()) {
echo 'Member Id:' . $row["member_id"] . '<br>';
}
} else {
echo " No Result";
}
?>
Hi I would like to compare two tables from two different databases.
Select from the first database
$sql= mysqli_query ("SELECT * FROM emasa.staff_detail");
$row = mysqli_fetch_array($sql);
Select from the second database
$sql2 = mysqli_query ("SELECT * FROM employee");
$row2 = mysqli_fetch_array($sql2);
Then I compare the two table
if ($row['icnum'] == $row2['emp_ic'])
{
echo "Data already exist in both database.";
}
else
{
while ($row = mysqli_fetch_assoc($sql))
{
echo "<td align='center' height='30'>" . $row ['name'] . "</td>";
echo "<td align='center'>" .$row['icnum'] . "</td>";
}
}
But my problem is it only compares the first row in the database.
My output should only display the staff name that is not available in the other database. However, this is my output.
Based on the output it only compares the first row. So how do I change my code so that it compares the whole row. Please help me, thank you!
You're not looping through the results of the query. You're simply getting back the first row and going with that.
Ex:
$sql= mysqli_query ("SELECT * FROM emasa.staff_detail");
This is returning a result set which you then need to use
$row = mysqli_fetch_array($sql);
to get the actual values. The problem comes when
$row = mysqli_fetch_array($sql);
by itself only gives you one row.
Solution 1
You must use code like this:
while($row = mysqli_fetch_array($sql))
{
//Do some comparison
}
Since you're going to have to loop through two different result sets, you're going to have to do a loop within a loop, build an array of results and then loop through the results afterwards to output your HTML.
Ex:
while($row = mysqli_fetch_array($sql))
{
while($row2 = mysqli_fetch_array($sql2))
{
if ($row['icnum'] == $row2['emp_ic'])
{
//add to array of equal data
}
else
{
//add to array of not equal data
}
}
}
foreach($array as $not_equal_or_equal_data)
{
//output your desired HTML
}
Solution 2
Depending on what you actually care about, you could do a sql statement like this
$sql = "
SELECT
*
FROM
emasa.staff_detail AS sd
JOIN db2.employee AS e
ON sd.icnum = e.emp_ic";
This would return all the rows where those two columns were equal
I know this may sound like a stupid question from a programming-newbie, but I just want to make sure I understand correctly.
After a query, what does $row[0] stand for/ result in?
Is my understanding correct that $row[0] shows ALL results?
HERE ARE EXAMPLES:
$query = "SELECT count(commentid) from comments where jokeid = $jokeid";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
if ($row[0] == 0)
{
echo "No comments posted yet. \n";
} else
{
echo $row[0] . "\n";
echo " comments posted. \n";
AND THIS ONE
$query = "Select count(prodid) from products where catid = $catid";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row[0] == 0)
{
echo "<h2><br>Sorry, there are no products in this category</h2>\n";
}
else
{
$totrecords = $row[0];
Thanks in advance.
$row[0] will simply echo the first column in your database.
0 is the first because all arrays in PHP (and in most programming languages) are zero-based - they simply start with zero.
$row[0] will be the value of the first column in your results. If you use mysql_fetch_assoc($result) you will have an array in the form:
array(column_name => column_value);
e.g.
$row = mysql_fetch_asssoc($result);
$value_column_1 = $row['column_1'];
You can also use mysql_fetch_object($result) to get an object with column names as the parameters.
$row = mysql_fetch_object($result);
$value = $row->column_name
mysql_fetch_array() takes the next (in your examples first) row out of the resultset and stores the data in an array $row.
$row[0] now represents the first value of that row.
So in total in your examples the variable holds the first value of the first row of your resultset.
I am attempting to loop through rows in MySQL with the following code. Unfortunately, the mysql_fetch_row function is only returning the first row of my query (I confirmed this with print_r). When I use mysql_num_rowsit returns the correct number of rows for that query(2). Any idea why mysql_fetch_row is returning only the first row?
$query = 'SELECT firstName FROM profiles WHERE city="Phoenix" and lastName ="Smith"';
$result = mysql_query($query);
$row = mysql_fetch_row($result);
print_r($row); //This returns only 1 array item: Array ( [0] => John)
$a=mysql_num_rows($result);
print_r($a); //This returns 2
I have also run the same query in MySQL and it returns both rows ("John" and "Jim").
Thanks for the help.
Just perform mysql_fetch_row twice (or in a loop). By definition (if you read the documentation) it returns only one row at once. So:
while ($row = mysql_fetch_row($result)) {
var_dump($row);
}
You want to put it in a while loop:
while ($row = mysql_fetch_array($result)) {
$firstName = $row['firstName'];
echo("$firstName<br />");
}
What this does is for each result (row) it finds, it fetches the data you want and displays it. So it gets the first result, displays the name, loops back to the top, fetches the second result, displays that name and so on, then once you have no more results that match the query criteria, the while loop is ended.
PHP accesses the SQL Results in order of how they're buffered. You'll need to run a loop to access all the contents.
If you wish to load everything into an array:
$allRows=array();
while($row=mysql_fetch_assoc($result) {
$allRows[]=$row;
}
You can use any of the following one:
while ($row = mysql_fetch_array($result))
{
echo $row['firstName'];
}
or
while ($row = mysql_fetch_assoc($result))
{
echo $row['firstName'];
}
or
while ($row = mysql_fetch_object($result))
{
echo $row->firstName;
}
$query = "SELECT * FROM table";
$result = mysql_query($query, $db);
$all = mysql_fetch_assoc($result);
echo mysql_num_rows($result) . ":" . count($all);
This returns
2063:7
I have not used count before, so I'm not 100% sure it's not counting the table columns. It's late and I might be going nuts.
Here's another example of what's happening:
$result = mysql_query($query, $db);
echo "Rows: " . mysql_num_rows($result) . " <BR />";
$player_array = mysql_fetch_assoc($result);
echo "<pre>";
print_r($player_array);
echo "</pre>";
Which outputs:
Rows: 9
Array
(
[playerID] => 10000030
)
TL;DR: I submit queries which return multiple rows, but fetch_array only gives me a small portion of those rows in the resulting array.
mysql_fetch_assoc returns only one row in once you have to use loop to retrieve all rows
while($row = mysql_fetch_assoc($result))
{
print_r($row);
}
Every call to mysql_fetch_assoc($result); gives you one row of the result set:
(from the documentation)
mysql_fetch_assoc — Fetch a result row as an associative array
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
You have to use the function in a loop:
$all = array();
while(($row = mysql_fetch_assoc($result))) {
$all[] = $row;
}
The example in the document shows how it is done.
mysql_fetch_assoc doesn't work that way, you need to call it multiple times to get all rows. Like this:
while ($row = mysql_fetch_assoc($db_result))
{
print_r($row);
}