This question already has answers here:
How to select multiple rows from mysql with one query and use them in php
(4 answers)
Closed 6 years ago.
I've included this PHP file in my index.php, but I only get the first item from the database. How can I get all items from my database?
<?php
$sql = "SELECT name,price FROM items ORDER BY `id`";
$res = mysqli_query($conn, $sql);
$page = mysqli_fetch_assoc($res);
mysqli_close($conn);
?>
<h2><?=$page["name"]?></h2>
<?=$page["price"]?>
As you said you want all records from the database, so you need to use while loop like below:-
<?php
$sql = "SELECT name,price FROM items ORDER BY `id`"; // query
$res = mysqli_query($conn, $sql); // execute query
while($page = mysqli_fetch_assoc($res)){ // loop
echo "<h2>". $page["name"]."</h2>".$page["price"]; // print all record (You can change pattern of printing according to your wish )
}
mysqli_close($conn); // close connection
?>
This is because you need to loop your results that are in $page array. Try this:
<?php foreach($page as $value): ?>
<h2><?=$value["name"]?></h2>
<?=$value["price"]?>
<?php endforeach; ?>
if you need more info about foreach method go here
Related
Hi and thank you for your time,
I am having problems with the following code:
<?php
include './include/config.php';
$sqla = mysqli_query($conn, "SELECT * FROM sector");
while ($rowa = $sqla->fetch_assoc()) {
$codigo = $rowa['codigo'];
$sector = $rowa['sector'];
echo $sector. "<br>";
$sqlb = mysqli_query($conn, "SELECT * FROM cfamilia WHERE sector = '$codigo'");
while ($rowb = $sqlb->fetch_assoc()) {
$cfamilia= $rowb['cfamilia'];
echo $cfamilia. " - ";
}
}
?>
When I run the code, it only echo´s the first result of the first loop and then seems to get stuck on the inner loop, what am I doing wrong? All data in the database is available and correct.
I am probably doing it the wrong way, but I am no way a programmer.
Thanks for your help
I have searched to check if all data is correct and that all variables were clean.
This question already has answers here:
How to check if a string is one of the known values?
(4 answers)
Closed 3 years ago.
I'm trying to echo entries from a database that have a lend_status of 1 or 2 and have written my while loop to get the entries but I'm not sure if my IF statement is correct or if this is the best way to go about this. I'm very new to PHP and SQL so any help would be really appreciated!
I've tried using an echo to see where it breaks and it seems to be the IF statement. I know the connection to the database works as I can echo information from it and have tested that.
<?php
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
$STH = $DBH->prepare("SELECT * FROM laptop_system");
// DOESN'T HAVE TO BE AN ARRAY BUT GOOD PRACTICE TO PUT THIS IN (BELOW)
$STH->execute(array());
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
while ($row = $STH->fetch()) {
$lend_status = $row->lend_status;
$lend_id = $row->lend_id;
$requestee = $row->user_id;
$first_name = $row->first_name;
$last_name = $row->last_name;
$active_status = array(1,2);
if($lend_status == $active_status) {
echo 'hello';
?>
<div>
<?php echo $requestee_name . '\n'; ?>
<?php echo 'hi'; ?>
</div>
<?php }} ?>
The problem is that you are comparing a string / integer - one of the fields of your database row - to an array:
$lend_status = $row->lend_status;
...
$active_status = array(1,2);
if($lend_status == $active_status) {
If you want to check if the status is one of the values in the array, you can use for example in_array():
if (in_array($lend_status, $active_status)) {
I want to create an SQL statement, where all the entries for my total_questions are added together, then I want to display that number.
My SQL looks like that:
<?php
include ("../script/db_connect.php");
$quiz_percentage_total = 'select sum(total_questions) as total from quiz_result where '
. 'quiz_creator = '
. $_SESSION["id"];
$quiz_percentage_total_number = mysqli_query($con, $quiz_percentage_total);
$show_quiz_percentage_total_number = mysqli_num_rows($quiz_percentage_total_number);
mysqli_close($con);
?>
My HTML is empty except for all the necessary HTML information, the HTML code is:
<?php echo $show_quiz_percentage_total_number; ?>
Did I use the wrong variable to display or is there another mistake?
You need to call a fetch function to get the data from a query.
$result = mysqli_query($con, $quiz_percentage_total);
$row = mysqli_fetch_assoc($result);
$quiz_percentage_total_number = $row['total'];
The number of rows will always be 1, there's no point in calling mysqli_num_rows().
This question already has answers here:
Notice: Array to string conversion in
(6 answers)
Closed 7 years ago.
I want to get the id of a table and make it a variable so I can use it in a link.
But I am getting Notice: Array to string conversion in .
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error());
$pid = mysqli_fetch_assoc($res);
<a id='del' href='deletepost.php?del=$pid'>Delete</a>
This is just a part of the code where I have problems.
You need to do like this:-
<?php
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error($conn));
while($pid = mysqli_fetch_assoc($res)){
echo "<a id='del' href='deletepost.php?del=$pid['id']'>Delete</a><br/>";
}
Note:- this is because $res is an result-set array object having 1 or more than value. So you need to iterate like this.
$pid is an array, you need to access the ID from the array then you can use it in the link.
Example:
$id = $pid['id'];
<a id="del" href="deletepost.php?del=$id">Delete</a>
This question already has answers here:
PHP variable variables
(6 answers)
Closed 1 year ago.
I'm trying to query my database with an array, I'm trying to count the number of rows returned from the query for each country in my database, and also count the rows for each of these countries with a digit equalling 1. Using the following code:
<?php
include ('mysqli_connect.php'); // indclude mysql connection functions
$countries = array('united states','canada','united kingdom');
foreach($countries as $country){
//e.g. $r_spain holds the results from the query below with spain as $country
$r_.$country = mysqli_query($dbc,"SELECT * FROM feelings WHERE country = '$country' AND digit='1'");
//loop through the results
while($row = mysqli_fetch_array($r_.$country, MYSQLI_ASSOC)){
$rowCount = mysqli_num_rows($r_.$country);
echo $country."=".$rowCount;
}
}
?>
This is the error message I get:
Catchable fatal error: Object of class
mysqli_result could not be converted
to string in
/home2/designwr/public_html/uwe/notalone/updates/percentage.php
on line 9
Can anybody point me in the right direction?
You are using a string concatenation for your variable name: $r_.$country is the result of the two strings added together.
I would suggest using an array for the result set like:
$r = array(); // after $countries
....
$r[$country] = mysqli_query......
Change:
$r_.$country
to:
${'r_'.$country}
And delete the loop (not it's contents):
while($row = mysqli_fetch_array($r_.$country, MYSQLI_ASSOC))
So in the end the code would look like this:
<?php
include ('mysqli_connect.php'); // indclude mysql connection functions
$countries = array('united states','canada','united kingdom');
foreach($countries as $country){
//e.g. $r_spain holds the results from the query below with spain as $country
${'r_'.$country} = mysqli_query($dbc,"SELECT * FROM feelings WHERE country = '$country' AND digit='1'");
$rowCount = mysqli_num_rows(${'r_'.$country});
echo $country."=".$rowCount;
}
?>
References:
http://php.net/manual/en/language.variables.variable.php
http://www.php.net/manual/en/mysqli-result.num-rows.php
I take it line 9 is
$r_.$country = mysqli_query($dbc,"SELECT * FROM feelings WHERE country = '$country' AND digit='1'");
I'm not sure what you're trying to do with $r_.$country = mysqli_query(...); but that's invalid syntax. Use an array:
$r[$country] = mysqli_query(...);
check out the manual on mysqli/query. There you will find simple working examples to get the basics straight. counting is done by the count statement in the SQL-request.