php mysql_fetch_array - php

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'];
}

Related

Fetch multiple records from php/mysql

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

How to get total number of row by using this function php mysql?

How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>

Populating an HTML Table from SQL DB with PHP

I'm attempting to populate a table in HTML with data from a DB. It's not working properly (a blank white page is displayed), but I can't find the source of the error.
<?php
$sql = "SELECT * FROM Orders";
$result = mysql_query($sql)or die(mysql_error());
echo "<table>";
while($row = mysql_fetch_array($result)){
$order_id = $row['orderID'];
$order_due = $row['order_due'];
$order_subject = $row['order_subject'];
$order_level = $row['order_level'];
$order_pages = $row['order_pages'];
$order_cost = $row['order_cost']);
echo "<tr><td>".$order_id."</td><td>".$order_due."</td><td>".$order_subject."</td><td>".$order_level."</td><td>".$order_pages."</td><td>".$order_cost."</td></tr>";
}
echo "</table>";
?>
$order_cost = $row['order_cost']);
you have an extra paranthesis
Also change this
while($row = mysql_fetch_array($result))
to
while($row = mysql_fetch_assoc($result))
I think you need to change third line to:
result = mysql_query($sql) or die(mysql_error());
(space before "or")

PHP loop not performing as it should

$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

Num Rows always ='s 1 for some reason. QUICK FIX

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...

Categories