Is it possible to output all database results outside of while loop?
I want to use it inside another echo.
$sql = $db->prepare("SELECT color FROM table WHERE type = :key");
$sql->bindValue(':key', $type, PDO::PARAM_STR);
$sql->execute();
while($rows = $sql->fetch(PDO::FETCH_BOTH)) {
$colors = $rows ['color'];
echo $colors; //Outputs all results
}
echo $colors; //Outputs only last result
Sure just store them to an array:
$sql = $db->prepare("SELECT color FROM table WHERE type = :key");
$sql->bindValue(':key', $type, PDO::PARAM_STR);
$sql->execute();
$colors = [];
while($rows = $sql->fetch(PDO::FETCH_BOTH)) {
$colors[] = $rows ['color'];
}
print_r($colors)
Instead of doing the while loop to save to an array you can just get all the results at once as an array with the PDOStatement::fetchAll method
$sql = $db->prepare("SELECT color FROM table WHERE type = :key");
$sql->bindValue(':key', $type, PDO::PARAM_STR);
$sql->execute();
$colors = $sql->fetchAll(PDO::FETCH_BOTH));
print_r($colors);
Related
I'm having some trouble with php coding. What I want to do is following:
Create an array ($rows) and fil it with the results of a mysqli_query ($query1) --> OK
for each element in that array, replace the value of a certain key (pilot_rule_id) with the result of another mysqli_query ($query2). (the second query will return one row, since the id of the pilot table is the primary key).
So far I have
$id = "96707ac6-ecae-11ea-878d-005056bbb446";
$rows = array();
$query1 = mysqli_query($con, "SELECT * FROM pilot_time_schedule WHERE pilot_id='$id'");
while($r = mysqli_fetch_assoc($query1)) {
$rows[] = $r;
}
foreach($rows as $pilotRuleId) {
$pilotRuleId->$pilot_rule_id;
$query2 = mysqli_query($con, "SELECT name FROM pilot_rule WHERE id='$piloteRuleId'");
while($r = mysqli_fetch_assoc($query2)) {
$result[] = $r;
}
// Don't know how to continue from here
You can something like this:
$id = "96707ac6-ecae-11ea-878d-005056bbb446";
$stmt = $con->prepare('SELECT * FROM pilot_time_schedule WHERE pilot_id=?');
$stmt->bind_param('s', $id);
$stmt->execute();
$rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row) {
$stmt = $con->prepare('SELECT name FROM pilot_rule WHERE id=?');
$stmt->bind_param('s', $row['pilot_rule_id']);
$stmt->execute();
// replace with the `name` returned from the above statement.
$row['pilot_rule_id'] = $stmt->get_result()->fetch_row()[0] ?? null;
}
However, you really should learn about SQL joins instead. Using SQL joins you can avoid N+1 queries to the database.
$id = "96707ac6-ecae-11ea-878d-005056bbb446";
$stmt = $con->prepare('SELECT pilot_time_schedule.*, pilot_rule.name
FROM pilot_time_schedule
JOIN pilot_rule ON pilot_rule.id=pilot_time_schedule.pilot_rule_id
WHERE pilot_id=?');
$stmt->bind_param('s', $id);
$stmt->execute();
$rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row) {
echo $row['name']; // contains the name from pilot_rule
}
I am having a problem with creating an array from the values received from a mySql query. I have the following code but I can't populate the array with the returned values. I assume that there is a problem with the while statement. Can anybody help me out?
$return_arr = array();
$searchTerm=$_GET['searchTerm'];
$query = "SELECT Actor.FirstName, Actor.LastName FROM Actors WHERE Actor.ActorFirstName LIKE '%$searchTerm%' ";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->store_result();
$numrows = $stmt->num_rows;
$stmt->bind_result($firstName, $lastName);
while($row = mysql_fetch_assoc($stmt) {
$return_arr[] = $row['FirstName'];
}
change your while loop
while($stmt->fetch())
{
$return_arr[] = $firstName;
}
I have seperate tables full of data and I require the same data from each table. For example the first table I am selecting from has the value 3623 and the second table has the value 3852.
I am trying to get both of these values into an array to then be plotted on a graph later down the line. The code I am using can be seen below, the issue is that on the value from the first foreach loop gets added and not the second one. so I end up with just 3623 and not the 3852 as well which is an issue.
$datay1 = array();
$yes = "not-set";
$sql = "SELECT * FROM `0530-0605` WHERE SearchTerm = :yes";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":yes", $yes);
$stmt->execute();
foreach($stmt as $row) {
$datay1[] = $row['Clicks'];
}
$sql = "SELECT * FROM `0606-0612` WHERE SearchTerm = :yes";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":yes", $yes);
$stmt->execute();
foreach($stmt as $row) {
$datay1[] = $row['Clicks'];
}
print_r($datay1);
You can use UNION ALL to merge result of two query as
$sql = "SELECT * FROM `0530-0605` WHERE SearchTerm = :yes
UNION ALL
SELECT * FROM `0606-0612` WHERE SearchTerm = :yes1";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":yes", $yes);
$stmt->bindParam(":yes1", $yes);
$stmt->execute();
foreach($stmt as $row) {
$datay1[] = $row['Clicks'];
}
I am using mysqli to fetch data from the database and put it into an array with a while loop. When i echo out the array i get an empty array however in a function i previously did this code worked but it had a different result from the database. I know that the database is giving out good data because when i echo out the result $idGroup it gives me 2 which is correct.
Ps i know it will keep replacing itself because i don't specify an index
private function Groups()
{
$functionRun = 0;
$i = 0;
$helperArray = array();
$this->grouplist = array();
$query = "SELECT GroupName, Groups.idGroup
FROM Groups
INNER JOIN Members
ON Groups.idGroup = Members.idGroup
WHERE Members.idMember = ? ";
//prepare query and execute
if($stmt = $this->connection->prepare($query))
{
$stmt->bind_param('s', $this->id);
$stmt->execute();
$stmt->bind_result($GroupName, $idGroup);
while ($stmt->fetch())
{
$helperArray[] = $idGroup;
}
echo $helperArray;
}
Use print_r when dealing with arrays. Use echo on strings.
Try this
$query = "SELECT GroupName, Groups.idGroup
FROM Groups
INNER JOIN Members
ON Groups.idGroup = Members.idGroup
WHERE Members.idMember = ? ";
//prepare query and execute
if($stmt = $this->connection->prepare($query))
{
$stmt->bind_param('s', $this->id);
$stmt->execute();
$stmt->bind_result($GroupName, $idGroup);
$helperArray =array();
while ($stmt->fetch())
{
$helperArray[] = $idGroup;
}
print_r($helperArray);
}
Im trying to assing the AS variables in my mySQL statement to variables so I can process the results. However I am unable to assign the to these vairables.
mySQL SELECT statement is as follows
$sql = "SELECT min(ups) AS minups, max(ups) AS maxups, min(downs) AS mindowns, max(downs) AS maxdowns, min(score) AS minscore, max(score) AS maxscore, min(comments) AS mincomments, max(comments) AS maxcomments, min(totalVotes) AS mintotalvotes, max(totalVotes) AS maxtotalvotes FROM reddit WHERE movie = ':movie'";
$stmt = $conn->prepare( $sql );
$stmt->bindValue( ":movie", $redditMovies->reddit, PDO::PARAM_INT );
$stmt->execute();
$row = $stmt->fetch();
I am trying to assign them to these variables
$minups = $row ['minups'];
$maxups = $row ['maxups'];
$rups = (int)($maxups - $minups);
print_r($rups);
$mindowns = $row ['mindowns'];
$maxdowns = $row ['maxdowns'];
$rdowns = (int)($maxdowns - $mindowns);
$minscore = $row ['minscore'];
$maxscore = $row ['maxscore'];
$rscore = (int)($maxscore - $minscore);
$mincomments = $row ['mincomments'];
$maxcomments = $row ['maxcomments'];
$rcomments = (int)($maxcomments - $mincomments);
$mintotalvotes = $row ['mintotalvotes'];
$maxtotalvotes = $row ['maxtotalvotes'];
$rtotalvotes = (int)($maxtotalvotes - $mintotalvotes);
What do I need to change to resolve this problem?
Try removing parenthesis in your query like,
movie = ':movie' to movie = :movie
USe this code. use extract($row) is used for directly assign the value for the key's in a $row array
$stmt = $conn->prepare("SELECT min(ups) AS minups, max(ups) AS maxups, min(downs) AS mindowns, max(downs) AS maxdowns, min(score) AS minscore, max(score) AS maxscore, min(comments) AS mincomments, max(comments) AS maxcomments, min(totalVotes) AS mintotalvotes, max(totalVotes) AS maxtotalvotes FROM reddit WHERE movie = ':movie'");
$stmt->bindValue( ":movie", $redditMovies->reddit, PDO::PARAM_INT );
$stmt->execute();
$row = $stmt->fetch();
extract($row);
echo $minups; // it prints the minups value from the result set $row.
$rups = (int)($maxups - $minups);
$rdowns = (int)($maxdowns - $mindowns);
$rscore = (int)($maxscore - $minscore);
$rcomments = (int)($maxcomments - $mincomments);
$rtotalvotes = (int)($maxtotalvotes - $mintotalvotes);
this extract($row); is used for assign the value for the key's
example:
$row['maxups']=5;
$row['minups']=2;
extract($row);
echo $maxups."-".$minups;
output: 5-2
Try this
echo '<pre>';
print_r($row);
If you are getting no values in the array then check the query whther there is any error