All I need to do is echo array pocket $row[0].
while ($row = mysqli_result::fetch_array($result)) {
echo '<br>' . $row[1];
}
echo $row[0];
I have googled, but results haven't helped. I know I'm missing something stupid, but I'd appreciate the help!
I'm trying to echo the database column, 'ID'. An example of the data I want to echo would be '3'.
Copy $row[0] as you go.
$lastZero = "";
while ($row = mysqli_result::fetch_array($result)) {
echo '<br>' . $row[1];
$lastZero = $row[0];
}
echo $lastZero;
For debugging the whole row, write this:
var_dump($row)
To output the column ID, write this:
while ($row = mysqli_result::fetch_array($result)) {
var_dump($row); // This will output all values for each row as they're looped through
echo '<br>' . $row['ID'];
}
var_dump($row); // dumps the last row of the returned results, or false if no rows.
echo $row['ID']; // echos the ID of the last row. However if there are no rows, this will not work.
Be aware, as #Daedalus pointed out, $row is an array, so the var_dump line is really just for debugging. Because an array has multiple values, PHP doesn't know how you want to display it. You'll need to program how each element of $row is displayed.
Related
When I used while loop to print the values of a particular column using mysqli_fetch_array(), I got the correct output from the database.
while($row=mysqli_fetch_array($data))
{
echo $row['name'];
}
But when I used the below mentioned code without the loop, the output was different.
$row=mysqli_fetch_array($data);
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
When I used the below mentioned code, I could see that I get all the rows in the form of an associative array using mysqli_fetch_array().
$con=mysqli_connect("localhost", "root", "", "student_details");
$data=mysqli_query($con,"select * from `registration`");
while ($row = mysqli_fetch_array($data))
{
$rec[]=$row;
}
echo "<pre>";
print_r($rec);
echo "</pre>";
?>
My question is how to get all the values of a particular column (using a while loop), without usage of any loop.
You can use mysqli_fetch_all to get all data.
Check in php docs
-> mysqli_result::fetch_all -- mysqli_fetch_all —
-> Fetches all result rows as an associative array, a numeric array, or both
First of all, this code:
while($row=mysqli_fetch_array($data))
{
echo $row['name'];
}
is not equivalent to:
$row=mysqli_fetch_array($data);
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
But rather to something like (pseudocode):
$row = mysqli_fetch_array($data);
if (!$row) return; //break the loop
echo $row['name'];
$row = mysqli_fetch_array($data);
if (!$row) return; //break the loop
echo $row['name'];
...
And you could just use multiple mysqli_fetch_array() calls to achieve what you need, but there are other ways to achieve it. One of which is the usage of mysqli_fetch_all(). Both work in a similar way, but mysqli_fetch_all() returns an array of all results you received from the query, rather than one result at a time.
Now, it returns an array of arrays, so to access it, rather than:
$row['name'];
You would need to use:
$row[ROW_NUMBER]['name']
//example
$row[0]['name']
Examples:
1) mysqli_fetch_array()
$row = mysqli_fetch_array($data);
echo $row['name']; //output name from the first row
$row = mysqli_fetch_array($data);
echo $row['name']; //output name from the second row
2) mysqli_fetch_all()
$row = mysqli_fetch_all($data);
echo $row[0]['name']; //output name from the first row
echo $row[1]['name']; //output name from the second row
You can use mysqli_fetch_all to get all data in array but you might need to use foreach for further action.
$con=mysqli_connect("localhost", "root", "", "student_details");
$data=mysqli_query($con,"select * from `registration`");
$row = mysqli_fetch_all($data, MYSQLI_ASSOC);
echo "<pre>"; print_r($row); die;
// Will show all data coming in $row as Array
For further processing, you might need to use foreach like below:
foreach ($row as $item) {
print_r($item); // see what your item contains
}
I am having a bit of an issue if there is no categories I need to echo no categories if there is I need it to echo There are categories. it shows if there is categories, but don't show if there is not.
<tr>
<?php
$db = dbconnect();
$stmt = $db->prepare("SELECT * FROM discussion_categories");
$stmt->execute();
$result = $stmt->get_result();
while (($row = mysqli_fetch_assoc($result)) == true) {
$CategoryID = $row['CategoryID'];
$Name = $row['Name'];
$Description = $row['Description'];
$Photo = $row['Photo'];
if(!empty($CategoryID['CategoryID'])){
echo "<td>No categories</td>";
} else {
echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>";
echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>";
echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>";
}
}
?>
</tr>
That's an easy to solve problem.
If there is no category the while loop gets never executed because there are no rows in your db result.
Try and check the row amount first:
if (mysqli_num_rows($result) == 0) {
//there are no categories
echo('no categories');
} else {
//there are categories
echo('there are categories');
//in case you want to loop through your categories now
while (($row = mysqli_fetch_assoc($result)) == true) {
$CategoryID = $row['CategoryID'];
echo($CategoryID);
//your code
}
}
This should help you!
If there are no categories, then this condition:
while (($row = mysqli_fetch_assoc($result)) == true)
Which is a long-winded way of writing this:
while ( $row = mysqli_fetch_assoc($result) )
Will never be true, so you'll enter the loop zero times - there will never be a "truthy" value for $row.
If we write out your code as pseudo-code, we get:
inspect each result
if the result has a non-empty CategoryID, echo "No categories"
if the result has an empty CategoryID, echo "There are categories"
end of loop
The two if checks are the wrong way around, but more importantly they are inside the loop.
What you probably meant was something like this:
set found_results flag to false
inspect each result
if the result has a non-empty CategoryID, set found_results flag to true
perform other operations on the result, or use "break;" to end the loop early
end of loop
if found_results flag is true, echo "There are categories"
if found_results flag is false, echo "No categories"
I'll leave it to you to translate that back into PHP. :)
Of course, if you really only need to know if there are results or not, you can write this much neater by:
counting the rows returned using mysqli_num_rows
using SELECT COUNT(*) in your SQL instead of SELECT *, possibly also with a WHERE CategoryId IS NOT NULL clause
if you have no results, then it wont even go in the while loop, so your conditional statement is redundant (which is why you get no output).
Youre better off, as mentioned in some of the comments to check for the results before you try to do anything with them.
if($result->num_rows > 0) {
// you now know there are results
while ($row = mysqli_fetch_assoc($result)) {
// do your business in here as you would have, but you dont need to worry about nothing to process
}
} else {
// do something in here to send back a null result, or whatever you like
}
while (($row = mysqli_fetch_assoc($result)) == true)
According to the above part of the code. It will only go inside the while loop if there is any data returned or fetched from the database else if no data has been fetched it will not enter the loop.
If there is no categories then it will go out of the loop and so this echo "<td>No categories</td>";will never be shown.
Also since you have put an echo saying 'No Categories', I assume that it means you want to echo an output if there is no category. But your if condition is wrong since, if u want to check if some variable is empty you need to do as below
if(empty($CategoryID['CategoryID'])){
echo "<td>No categories</td>";
} else {
echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>";
echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>";
echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>";
}
where if(empty()) will be true if it is empty and if(!empty()) will be true if it is not empty.
If I understand it correctly, you are should use empty() not !empty().
The code run like:
if(!empty($CategoryID['CategoryID'])){ //if cat is not empty it says no cat
//notice the ! in front of empty tag
echo "<td>No categories</td>";
} else {
echo "<td>There are categories</td>";
}
according to your code, if the categories are empty, it will display there are categories.
By calling the While loop you are saying that, if the query return at least one category in the result.
The loop is thus confused when there is no category. Try replacing the empty() with count of CategoryID greater than zero and see what happens.
if > 0
exist
else
does not exist
Can someone please point out what I'm sure is a stupidly obvious error in my code? The string "string" in my while loop is displaying the correct amount of times but not the results in row[0].
if (!isset($_GET['city']) & !isset($_GET['county'])) {
$getResults = "SELECT DISTINCT region FROM `locations` WHERE country = 'England'";
echo "No region or county set";
if ($result = $mysqli->query($getResults)) {
echo "Found results";
while ($row = $result->fetch_assoc()) {
echo "string";
echo $row[0];
}
}
}
To see the contents of the $row array dump it out like so var_dump($row).
I'm guessing you just need echo $row['region'] rather than $row[0]
You are using fetch_assoc() but you try to access the row using a index numbers.
Use fetch_row() instead.
I have a list of urls that I need to echo from my "menu" table. Here is what I have so far, but I can't seem to figure out the rest of it. The urls below are obviously there to show the format of the original HTML.
<?php
$results = mysql_query("SELECT * FROM menu WHERE level='$level'");
$row = mysql_fetch_array($results);
?>
<li>Achievments</li>
<li>Avatar</li>
.... more urls ....
You need to do it in a loop, usually a while loop:
<?php
$results = mysql_query("SELECT * FROM menu WHERE level='$level'");
while ($row = mysql_fetch_array($results)) {
echo'<li>'.$row['name'].'</li>';
}
?>
I've improvised on your column names (uri and name), they will probably be something different.
Close. Personally I would use put it into a while loop to read all the values. then echo them one by one.
while($row = mysql_fetch_array($results)){
echo "<a href = \"". $row['fieldtouse'] . "\" />". $row['textforlink'] . "<a/>";
}
That is the idea. Loop through results. echo results.
I believe this is a simple case of iterating over the result list. And as you already mentioned you should not use a single mysql_fetch_array, but in a loop like this:
while ($row = mysql_fetch_array($results)) {
print "<li><a href='.../$row[0]' target='evil'>$row[1]</a></li>";
}
Now $row[0] and $row[1] will have to be adapted. Prefer mysql_fetch_assoc to get named result columns, and then apply e.g. $row[url] and $row[title] instead of the numeric keys.
Please give me a hand retrieving data from the database and showing it.
I'm learning and this is holding me from advancing :(
I have:
$result = mysql_query($sql);
$num_reg = mysql_num_rows($result);
echo $num_reg; // this shows 3
while($row = mysql_fetch_assoc($result))
{
foreach($row as $value)
{
//Now I need to operate with the rows values.
echo $row['PHONE'];
But this instead of printing the phone numbers, prints them 5 times each
What am I doing wrong?
Thanks a lot
Remove foreach loop
while($row = mysql_fetch_assoc($result))
{
echo $row['PHONE'];
echo $row['NAME'];
echo $row['OTHER_FIELD'];
}
you don't need foreach in that case
while($row = mysql_fetch_assoc($result))
{
echo $row['PHONE'];
}
Probably your $row has 2 elements, and inside foreach your echo $row['PHONE'] is called once for every element
do a print_r($row) in your while loop and a print_r($value) in your foreach loop.
Then ask you why do you echo $row in a loop on $row elements.
You don't need a second foreach() loop inside the while() loop to work on the values. What you're doing here is looping through the rows then looping through the values but if you already have access to the values via the $row variable you don't need to loop again. The fact you get the phone number 5 times suggests you have 5 columns in your table.
An example - remove the foreach() loop:
while($row = mysql_fetch_assoc($result)){
echo $row['PHONE'];
echo $row['NAME'];
echo "<br />";
}
I'm guessing you have a variable called "name" but if not just swap it for one you do have. The last echo just prints a new line to make it easier to read.