i have a database table 'movies'. in this table there are 25 columns of information per row. Ie, movie title, poster, cast, synopsis etc.
At the moment i am fetching the information like this
$query = "SELECT * FROM `movies` WHERE `title`='$moviename'";
$result = $con->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$moviedetails['title']=$row['title'];
$moviedetails['poster']=$row['poster'];
}
}
else {
echo 'NO RESULTS';
}
because i have 25 columns its long work writing out each variable. is there a way to fetch the information and i can then call to it by using
$moviedetails['column name']
ie
im new to php and mysql so any help appreciated.
thanks
lee
$moviedetails['title']
fetches the information from the 'title' column.
while($row = $result->fetch_assoc()) {
foreach ($row as $key => $value){
$moviedetails[$key]=$value;
}
}
This input the same key and the same value into new array
Is that what you're looking for?
$query = "SELECT * FROM `movies` WHERE `title`='$moviename'";
$result = $con->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($moviedetails = $result->fetch_assoc()) {
//just use moviedetails for what you need
}
} else {
echo 'NO RESULTS';
}
Try this:
while($moviedetails[]=$result->fetch_assoc()){}
then you loop by it like this:
foreach($moviedetails as $num->row)
{
echo $row['title'];
}
Related
/* To sort the id and limit the post by 40 */
$sql = "SELECT * FROM requests";
$result = $conn->query($sql);
$sqlall= "SELECT * FROM requests ";
$resultall = $conn->query($sqlall);
$i = 0;
if ($result->num_rows > 0) {
// Output data of each row
$idarray= array();
while($row = $result->fetch_assoc()) {
echo "<br>";
// Create an array to store the
// id of the blogs
array_push($idarray,$row['id']);
}
}
else {
echo "0 results";
}
?>
<?php
for($x = 1; $x < 40; $x++) {
// This is the loop to display all the stored blog posts
if(isset($x)) {
$query = mysqli_query(
$conn,"SELECT * FROM `requests`");
$res = mysqli_fetch_array($query);
$email1 = $res['email1'];
$msg1= $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
the output is 40 cards reading data from the first row in my database. can anyone help?
I'm using xampp.
This code is to show the loop, but if anyone wants the full code is here
You are storing all the IDs in the array $idarray, but then you don't really use them properly. You loop over them, but you just run SELECT * FROM requests` 40 more times, and always extract the same first row. You never use the ID to change the query.
But it really makes no sense to run lots of separate queries anyway. If you just want the first 40 rows then use MySQL's LIMIT keyword. It usually works best when combined with ORDER BY as well. Something like this:
$sql = "SELECT * FROM requests ORDER BY id LIMIT 40";
$result = $conn->query($sql);
while ($res = $result->fetch_assoc()) {
$email1 = $res['email1'];
$msg1 = $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
//example output, just for demo:
echo $email1." ".$msg1." ".$subject1." ".$name1." ".$id;
}
Documentation: https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html
I am trying to collect all rows from a table of mySql database. But I am getting 1 row lesser than the original rows. Suppose the table has 3 rows but I am getting data of 2 rows. I am missing the first one always. Here is the image of my table.
Here is my code:
$query = $db->query("SELECT * FROM `POS_refund`");
if($query->fetchColumn() > 0){
foreach($query as $row){
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}
}
Where is the fault?
PDOStatement::fetchColumn Returns a single column from the next row of a result set
You should use fetch() or fetchall() in your case
<?php
$query = $db->query("SELECT * FROM `POS_refund`")->fetchall();
foreach($query as $row){
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}
?>
If you want to see if there's records returned you could use count() since fetchall() returns array, then just count your array elemenets
<?php
$query = $db->query("SELECT * FROM `POS_refund`");
$results = $query->fetchall();
if(count($results) > 0){
foreach($results as $row){
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}
}else{
echo "No results";
}
?>
I would use a while loop. it would look like this:
while ($row = $query->fetchall) {
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}
I'm creating a personal advisor page with 3 advisors in my database, I'm trying to create a dropdown box where someone can choose which advisor they'd like. At the moment my dropdown only displays the word 'Array' three times. Here's what I have so far.
<select name="advisor">
<?
$sqlQ = "SELECT concat(firstName,' ',lastName) FROM adv WHERE advisor IS NULL";
$array=array();
$res = $db->prepare($sqlQ);
$res->execute();
echo("<option>Advisor</option>");
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$array[] = $row;
}
foreach($array as $info)
{
echo("<option>$info</option>");
}
Your $row is already an array, so no need to insert your $row into a new array. Just loop the results like this
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $info)
{
echo("<option>$info</option>");
}
}
// give the result of concat() an alias so you can easily access it in the result set
$sqlQ = "SELECT concat(firstName,' ',lastName) as name FROM adv WHERE advisor IS NULL";
[...]
while ( $row = $result->fetch(PDO::FETCH_ASSOC) ) {
// $row is an array, its members correspond with the fields/aliases you've selected
// apply htmlspecialchars() so that the contents of $row['name'] can't break your html structure
echo '<option>', htmlspecialchars($row['name']), '</option>';
}
I'm looking for a way to SELECT from database, then check the result, and then output rows in a while loop (IF the result was above zero)
I really want to avoid using a separate count query
Right now I use this:
$sql = 'SELECT id, username, usercity, usercountry FROM siteusers WHERE userage > 50';
$STH = $conn->query($sql);
$arr = $STH->fetchAll();
if (count($arr) > 0) {
echo '<div id="users">';
foreach ($arr as $row) {
echo '<h1>'.$row['username'].</h1>';
}
echo '</div>';
}
It works. But isn't there a way I can check result/numrows and loop the rows, without using fetchAll and custom for-each loop?
Or does it not matter at all? (is for-each just as good as while loop?)
If I do it like this, the first row is not included in the while loop:
$sql = 'SELECT id, username, usercity, usercountry FROM siteusers WHERE userage > 50';
$STH = $conn->query($sql);
if ($row = $STH->fetch()) {
echo '<div id="users">';
while ($row = $STH->fetch()) {
echo '<h1>'.$row['username'].</h1>';
}
echo '</div>';
}
EDIT: I DO need to check the result, for dynamic layout purposes
You can use the PDO method rowCount to verify before your foreach if there are rows
$STH = $conn->query($sql);
if ($STH->rowCount())
{echo '<div id="users">';
foreach ($STH->fetchAll() as $row)
{
echo '<h1>'.$row['username'].'</h1>';
}
echo '</div>';
}
http://php.net/manual/en/pdostatement.rowcount.php
note that this uses up a lot of memory as all your results are loaded at once in memory with fetchAll(). if you have very large result sets, consider using a while instead of the foreach
while ($row = $STH->fetch())
{// foo with $row
}
$row is being set to the first row of your results inside of your if statement. This means that your while loop will start at the second row.
$sql = 'SELECT id, username, usercity, usercountry FROM siteusers WHERE userage > 50';
$STH = $conn->query($sql);
echo '<div id="users">';
while ($row = $STH->fetch()) {
echo '<h1>'.$row['username'].</h1>';
}
echo '</div>';
The while loop will run if there is any results to fetch, and if there aren't, then respectively it won't run.
I need all values from a table column put into an assoc array. I am having trouble finding the answer.
the table is only six rows deep.
example:
|--id--|--name--|--A--|--B--|--C--|
|--01--|--xl33--| 1.30| 2.45| 4.40|
i would like to get an assoc array for column B
name[xl33]=2.45
name[xl34]=....and so on
The trick is that the form will tell the script which column to fetch. A,B,C,D,E,F OR G
I know i could re-format the table and accomplish what i want but I need it structured the way i have it.( i have left out some columns for simplicity)
function return_column($letter){
$result = mysql_query("SELECT name, $letter FROM example") or die(mysql_error());
while($row = mysql_fetch_assoc( $result )) {
$return[$row['name']] = $row[$letter];
}
return $return;
}
$results = return_column('A');
print_r($results);
Something like :
$col = 'B';
$name = array();
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$name[$row['name']] = $row[$col];
}
This creates an array $name and uses the name column as the key and the $col column for the value ...
You are looking for the mysql_fetch_assoc() function.
Example/
$query = $this->query($YOUR_QUERY);
$returnMap = array();
while ($row = mysql_fetch_assoc($query)) { array_push($returnMap, $row); }
Use mysql_fetch_assoc — Fetch a result row as an associative array.
while ($row = mysql_fetch_assoc($result)) {
$data[$row["column1"]]= $row["column2"];
..............
.........
}