Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Access information in rows, rows indexing MySQL SELECT

30 views
Skip to first unread message

Alla

unread,
Aug 2, 2016, 3:44:56 AM8/2/16
to
Hello!

I will be grateful for your help. I am taking a free online course, and I have written the code below
for that course's assignment. The goal is to show a table that contains information from mysql table,
i.e. 0 to several rows, each of which contains 6 columns.

The query() function that is used in the code is a custom function written by the course's staff.
Upon success, this function returns an array of rows if SELECT command is passed to it.

I have the following issue with the code:

As far as query() returns an array of rows, I assumed I need to use indexes to access each row and
thus information contained in each row. For this I have tried to count those rows, store the result
in $row_count and then use $row_count as an index together with while loop.

This method didn't work, I get errors. Also I have been told that in fact I do not need a while
loop and I do not need to count rows to print information contained in columns. I am learning,
and php seems a bit more difficult than C for me now, although it looks more straightforward
language.

When I tried the same code but without while loop, without row_count and without index, I get
error that indicates the need to use indexes for rows.

If I use something like $rows_two[0]["Symbol"], and if the query returns an array of
two elements, then i, obviously, get two identical rows printed out on the screen - because I
use the same 0 index.

Please, help me to understand the way these things work in php.

//retrieve username and cash from table users with custom query() function
$rows_one = query("SELECT username, cash FROM users WHERE id = ?", $_SESSION["id"]);

//retrieve name, symbol, shares from portfolios table
$rows_two = query("SELECT Company_name, Symbol, Number_of_Shares FROM Portfolios WHERE Users_id = ?", $_SESSION["id"]);

$username = $rows_one[0]["username"];
$total_cash = $rows_one[0]["cash"];

//count how many rows were retrieved by query("SELECT...")
$row_count = count($rows_two);

while($row_count)
{
$positions = [];
foreach ($rows_two as $row)
{
$stock = lookup($rows_two[$row_count]["Symbol"]);
if ($stock !== false)
{
$positions[] = [
"Company_name" => $stock["name"],
"Stock_Symbol" => $rows_two[$row_count]["Symbol"],
"Number_of_Shares" => $rows_two[$row_count]["Number_of_Shares"],
"Price_per_share" => $stock["price"]
];
}
}

// render portfolio with custom render function
render("portfolio.php", ["Username" => $username, "Total_cash" => $total_cash, "positions" => $positions, "title" => "Portfolio"]);

//update row_count
$row_count--;
}

Here is the code without a while loop:

$rows_one = query("SELECT username, cash FROM users WHERE id = ?", $_SESSION["id"]);

$rows_two = query("SELECT Company_name, Symbol, Number_of_Shares FROM Portfolios WHERE Users_id = ?", $_SESSION["id"]);

$username = $rows_one[0]["username"];
$total_cash = $rows_one[0]["cash"];

$positions = [];

foreach ($rows_two as $row)
{
$stock = lookup($rows_two[index]["Symbol"]);
if ($stock !== false)
{
$positions[] = [
"Company_name" => $stock["name"],
"Stock_Symbol" => $rows_two[index]["Symbol"],
"Number_of_Shares" => $rows_two[index]["Number_of_Shares"],
"Price_per_share" => $stock["price"]
];
}
}

// render portfolio
render("portfolio.php", ["Username" => $username, "Total_cash" => $total_cash, "positions" => $positions, "title" => "Portfolio"]);

}
Thank you very much!

Alla

unread,
Aug 2, 2016, 4:14:02 AM8/2/16
to
<snip>

Sorry for this post! I got it ))) My mistake was in using $rows_two within foreach loop
instead of using $row. Solved )

Härra Ramob

unread,
Jan 2, 2022, 6:50:08 AM1/2/22
to
ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ

ʕʘ̅͜ʘ̅ʔ
0 new messages