I'm having trouble iterating through MySQL rows. This is my current code:
$query = "SELECT * FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result= mysqli_query($query);
$numrows = mysqli_num_rows($result);
$row2 = mysql_fetch_row($result);
if ($numrows > 0) {
while($eachrow = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo $eachrow[0];
echo ", ";
echo $numrows;
}
}
The result of this is:
6, 2
But if there are 2 rows, why is the while loop ending after only 1 iteration? What am I understanding wrong?
EDIT: It appears to be displaying ONE LESS than the correct amount of rows. I.E. the while loop is running 1 less time than it should.
Found problem. I was fetching the first row with
$row2 = mysql_fetch_row($result);
outside of the while loop, thus causing it to start on the second row and skip over the first.
Related
entry in row "cars" is "BMWx1"
$sql = "SELECT cars FROM table LIMIT 1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
$rows[] = $result->fetch_row();
$result->free();
}
echo json_encode($rows[0]);
output: ["BMWx1"]
So far so good. Now how do I get my variable $cars to be equal to "BMWx1"
e.g.:
echo $cars;
output: BMWx1
Thanks you
If you're just reading a single row, don't push the row onto an array. fetch_assoc returns an associative array containing the row that was retrieved from the query (or false if there was nothing selected). Just use that and access the column you want.
$result = $con->query($sql);
$row = $result->fetch_assoc();
if ($row) {
$cars = $row['cars'];
}
Bit of an issue with my PHP / SQL here. This is the code:
Code:
$query = "SELECT DISTINCT student FROM classes LIMIT 100";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
print_r($row);
Having run the query manually I get about 40 returned values. All good there. However when I do the print_r, I'm only getting the first returned value i.e. The $row array only has one entry. Link and db are fine, it just appears to be my array handling.
Any Thoughts?
The function mysqli_fetch_array only fetches one row at a time. Consider using it in a loop:
while($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
print_r($row);
}
You need to loop through the returned rows. Try this instead:
$query = "SELECT DISTINCT student FROM classes LIMIT 100";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
print_r($row);
}
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'];
}
//get the current member count
$sql = ("SELECT count(member_id) as total_members from exp_members");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 0) {
while($row = mysql_fetch_array($result)) {
$total_members = $row['total_members'];
}
}
//get list of products
$sql = ("SELECT m_field_id, m_field_label from exp_member_fields where m_field_name like 'cf_member_ap_%' order by m_field_id asc");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 0) {
while($row = mysql_fetch_array($result)) {
$m_field_id = $row['m_field_id'];
$m_field_label = $row['m_field_label'];
$sql2 = ("SELECT count(m_field_id_".$m_field_id.") as count from exp_member_data where m_field_id_".$m_field_id." = 'y'");
$result2 = mysql_query($sql2) or die(mysql_error());
$num_rows2 = mysql_num_rows($result2);
if ($num_rows2 != 0) {
while($row2 = mysql_fetch_array($result2)) {
$p = ($row2['count']/$total_members)*100;
$n = $row2['count'];
$out .= '<tr><td>'.$m_field_label.'</td><td>'.number_format($p,1).'%</td><td>'.$n.'</td></tr>';
}
}
}
}
It's easier to help if you can describe in non-code terms what you're trying to accomplish. But one indicator of a problem is seeing a php loop on rows from one query with another query executing for each row.
There are ways to query for subtotals. But it would be easier to explain if you can explain the goal a bit.
count query would always return 1 row, so you don't need the loop
$sql = ("SELECT count(member_id) as total_members from exp_members");
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$total_members = $row['total_members'];
Other than that i am not sure how you can make it better. You can do the same for both of your count queries.
As these are straight forward queries, any bottleneck i guess now would be on the MySQL end
The first COUNT query ("get the current member count") should execute almost instantaneously.
The second query ("get list of products") may be slow depending on your indexes. You are querying on m_field_name and then ordering on m_field_id so you may need a combined index of the two.
The third query, which is executed repeatedly (once for each product), is querying on m_field_id_* (i.e. any of a number of possible fields), so you should probably make sure they are indexed.
In summary, you need to a) figure out which query is running slow, b) index things that need to be indexed, and c) combine queries if possible.
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...