/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Return Values from Database

I’m just getting started with databases and PHP. I’m basically building a Top Friends function but I’m running into an issue getting account numbers to be returned from the database properly. Here’s the code I’m using to test the values.

This is an include and the database connection is opened and closed in other includes.

[code=php]
<?php
$acctId = $_SESSION[“acctNum”];

$friendQuery = “SELECT * FROM friends WHERE addedById = {$acctId} OR addedId = {$acctId};”;
$result = mysql_query($friendQuery);
while($row = mysql_fetch_array($result)){
$friendAddedBy = $row[“addedById”];
$friendAdded = $row[“addedId”];
}

for($i = 0; $i < 9; $i++){
echo “AddedBy: ” . $friendAddedBy[$i] . “<br />”;
echo “Added: ” . $friendAdded[$i] . “<br />”;
}
?>
[/code]

There are two records in this table, the values are:
addedById

1

1

addedId
1
571j

I tried switching addedId to varchar instead of int and threw that “j” in there to just to make sure.

The output I get is:

[code=php]
AddedBy: 1
Added: 5
AddedBy:
Added: 7
AddedBy:
Added: 1
AddedBy:
Added: j
AddedBy:
Added:
AddedBy:
Added:
AddedBy:
Added:
AddedBy:
Added:
AddedBy:
Added:
[/code]

What would keep the addedId to come through in one piece. Thanks!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJul 31.2010 — you are adding each row data to the 2 array incorrectly

maybe ise this as a guide:

[code=php]
<?php
$acctId = $_SESSION["acctNum"];

$friendAddedBy = array();
$friendAdded = array();

$friendQuery = "SELECT * FROM friends WHERE addedById = {$acctId} OR addedId = {$acctId};";
$result = mysql_query($friendQuery);
while($row = mysql_fetch_array($result)){
$friendAddedBy[] = $row["addedById"];
$friendAdded[] = $row["addedId"];
}

for($i = 0; $i < 9; $i++){
echo "AddedBy: " . $friendAddedBy[$i] . "<br />";
echo "Added: " . $friendAdded[$i] . "<br />";
}
?>
[/code]
Copy linkTweet thisAlerts:
@MindzaiJul 31.2010 — $friendAddedBy is a string not an array and when you use array notation on a string it retrieves the character at the designated position. You are also overwriting $friendAddedBy in your while loop so you will only ever get the values from the last row. Also even if you [I]did[/I] store the data in an array correctly, your for loop is going to give you Notice level errors whenever your result set contains fewer than 8 items because some of the array elements will not be defined.

A better approach might be to build the list directly (notice I'm using mysql_fetch_assoc too to avoid getting double rows):

[code=php]
while($row = mysql_fetch_assoc($result)){
echo "AddedBy: $row[addedById]<br />";
echo "AddedId: $row[addedId]<br />";
}[/code]


Or if you need to store the results for iteration later, use an array and then iterate it with foreach:

[code=php]
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}

// later...

foreach ($rows as $row) {
echo "AddedBy: $row[addedById]<br />";
echo "AddedId: $row[addedId]<br />";
}
[/code]
Copy linkTweet thisAlerts:
@empireapathyauthorJul 31.2010 — you are adding each row data to the 2 array incorrectly

maybe ise this as a guide:

[code=php]
<?php
$acctId = $_SESSION["acctNum"];

$friendAddedBy = array();
$friendAdded = array();

$friendQuery = "SELECT * FROM friends WHERE addedById = {$acctId} OR addedId = {$acctId};";
$result = mysql_query($friendQuery);
while($row = mysql_fetch_array($result)){
$friendAddedBy[] = $row["addedById"];
$friendAdded[] = $row["addedId"];
}

for($i = 0; $i < 9; $i++){
echo "AddedBy: " . $friendAddedBy[$i] . "<br />";
echo "Added: " . $friendAdded[$i] . "<br />";
}
?>
[/code]
[/QUOTE]



Thank you, that worked perfectly!
Copy linkTweet thisAlerts:
@MindzaiJul 31.2010 — If you use the above as is, note that you will still be generating notice level errors (even if you do not have error_reporting set to an adequate level to display them).
×

Success!

Help @empireapathy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...