I'm really stuck on this guys, and I know where the fault is but I can't seem to fix it.
The while loop itself works it's the if that is causing all the trouble.
Whenever there are 3 groups in the database it only displays 2 the strange part it will only display the items in the while loop. But the first item is somehow tied to the if statement.
public function get_group($user_id){
$sql3="SELECT * FROM groups WHERE user =
$user_id";
$results = mysqli_query($this->db, $sql3);
$user_data = mysqli_fetch_array($results);
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()){
echo "hello";
echo "group :" . $row["group"] . "<br>";
echo "groupdesc:" . $row["groupdesc"] . "<br>";
echo "<a class='btn btn-primary' href='project.php?groupid=" . $row["groupid"] . "'>></a>";
}
}
else {
echo "0 results";
}
}
It seems it can't output the first one the first one just keeps hidden.
You fetch your first row in this line:
$user_data = mysqli_fetch_array($results);
And you do nothing with it.
And please do not mix procedural and object-oriented usage of mysqli.
Related
I am updating all my code to Mysqli, before I did I had this code and it worked:
while($row = mysql_fetch_assoc($WildHorses)){
$InWild[] = $row['id'];
}
$RandomWild = array_rand($InWild);
$RandomHorse = $InWild[$RandomWild];
This is my SELECT statement:
$sql = "SELECT Horse.id, Horse.Name, Horse.Age, Horse.Image_name, Horse.Owner, Horse.Barn, Images.Image_path, Images.Image_name FROM Horse, Images WHERE Horse.Owner = '$colname_WildHorseList' AND Images.Image_name = Horse.Image_name";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " Name: " . $row["Name"]. " ImageName: " . $row["Image_name"]. "<br>";
}
} else {
echo "0 results";
}
The SELECT statement ends up echoing all of the correct information, but I want to make an array of only the Id's so that I can pick one at random each time a button is clicked.
I have tried multiple different copies and pastes of code to try and get what I want, but nothing seems to come out right.
Can someone point me in the right direction or explain what I'm doing wrong?
In your while loop you can simply do this :-
$i=0;
$animals=array();
$animals[$i]=$row["id"]; //puts id in array
And then you can create a random number by "rand()" between the length of 0-$i
and can get the job done.
I'm trying to get rows from my database between two dates. I've used other questions here to get to this point, but I don't know how to move forward from here.
Here is the code, and underneath are the screenshots of the output and a photo running the query in SQL.
Thanks for the help in advance!
--
This first section of code outputs the ID, name, etc.
<?php
$sql = "SELECT * FROM songs ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This section of code outputs "0 Results"
<?php
$from_date = '2015-01-01';
$to_date = '2017-04-04';
$sql = "SELECT * FROM songs WHERE released BETWEEN '" . $from_date . "' AND '" . $to_date . "' ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This is what the page looks like with the above code.
I've successfully selected the rows in SQL.
EDIT: I made a mistake when writing the code into the question for the IF ELSE statement on the 2nd block of code. I just updated it to what I actually have on the site. I didn't change anything based on the solutions, and am still not getting the rows to print.
You done everything right, except using while/else ;
Your code (2-nd) section should look like this:
if ($result->num_rows > 0) {
while () {
//print your rows
}
} else {
//Say that there's 0 results
}
Your results are listed - as you can see from your screenshot. Problem: While along with else does not make sence.
your last example is missing a } so it says: while { } else { echo "0 results"}
But the echo "0 results" should be related to the num_rows-check, not to the while.
Ah man, I just needed to had to comment out this line from the first block:
$link->close();
I apologize to everyone who answered if it wasn't clear that the blocks of code followed one another. Anyways, for anyone else with the same problem, don't close the connection -_-
Here's what is output when I comment out the line from the first block
I am stuck. I want to make a session of the ID that belongs to a certain link. However, the code results into multiple ID's. And multiple links with different ID's coming from one code.
This is what I have:
$sql = "SELECT * FROM `lijsten` ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<a href='lijst.php'> Naam: " . $row["naamlijst"]. " - taal: " . $row["taal"]. "</a> <br>";
}
} else {
echo "0 results";
}
$conn->close();
?
The statement gives links for all the results of the rows naamlijst and taal. But I only want to make a session of ID of the link that I click.
This is my first question on Stackoverflow. I hope someone can help me! :)
Picture of webpage
Here is the webpage, I want to store the ID of the link that I click into sessions
When the number of results is one greater than the total loss is entered correctly, but one is subtracted from the list. For example: Found 12 results but only 11 are listed:
$goto = mysql_fetch_array($query);
$results = mysql_num_rows($query);
if($results == 1){
header('Location: movie.php?id='.$goto['id']);}
echo '<p><h2>Results for search "'.$_GET["search"].'": </h2></p>';
echo '<hr />';
echo "Found ".$total." result(s).</p><br>";
echo '<ol class="list">';
while ($resultado = mysql_fetch_assoc($query)) {
$titulo = $resultado['title'];
echo '<li>';
$link = 'http://localhost/lab/movie.php?id=' . $resultado['id'];
Thank you in advance for your help.
You need to do something like this:
$results = mysql_num_rows($query);
if($results == 1){
$goto = mysql_fetch_array($query);
header('Location: movie.php?id='.$goto['id']);}
}
echo '<p><h2>Results for search "'.$_GET["search"].'": </h2></p>';
echo '<hr />';
echo "Found ".$results." result(s).</p><br>";
echo '<ol class="list">';
while ($resultado = mysql_fetch_assoc($query)) {
Your code is getting the first record at the start, and if there's more than one entry, you're then looping through the rest of the records - binning the first one. This way, you're not populating $goto until you know you need it.
It is because you are using up your first record pointer on this line:
$goto = mysql_fetch_array($query);
If you move this line inside the $results == 1 conditional, everything should work fine.
Right now when you get to your while loop, you are starting with the result set pointer already on the second record.
.I have the following code. I am wondering why does my "echo '<font color="#FF0000">'.$update_date2.'</font><br><br>'.$status.'<br>';" does not show on the screen?
.is it okay to use while statement inside another while statement? as shown in my code below.
<?php
$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$update_date = $rows['update_date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
echo "<div id=\"container\">";
echo "<table>";
echo "<tr>";
echo "<div id=\"conid\">$id</div>";
echo "<div id=\"condate\">$date</div>";
echo "<div id=\"conproject\">$project</div>";
echo "<div id=\"contask\">$topic</div>";
echo "<div id=\"conselect\">$instby</div>";
echo "<div id=\"conselect1\">$inst</div>";
echo "<div id=\"condline\">$dline</div>";
echo "<div id=\"conocome\">";
$updatesquery = "SELECT * FROM it_task_update WHERE update_id=$id";
while($rows1 = mysql_fetch_array($updatesquery));
{
$update_date2 = $rows1['update_date'];
$status = $rows1['ocome'];
$update_id = $rows1['update_id'];
echo '<font color="#FF0000">'.$update_date2.'</font><br><br>'.$status.'<br>';
}
echo "</div>";
echo "<div id=\"concomm\">$comm</div>";
echo "<div id=\"confin\">$fin</div>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
?>
.where have i done wrong in this set of codes. can anyone guide me pls. TIA! More power! :)
$updatesquery = "SELECT * FROM it_task_update WHERE update_id=$id";
<---dude, where's my query?
while($rows1 = mysql_fetch_assoc($updatesquery));
You're not actually executing the query. You're trying to perform a fetch on a string, which is an error condition in MySQL, causing the fetch call to return FALSE, which makes the while() loop terminate immediately..
in other words, you're missing a call to mysql_query() in between those lines.
on another note, if the inner loop is going to be executed "many" times, you should look at rewiting both queries as a single one with a JOIN clause. It's almost always after to do a single large query then run multiple smaller ones.
Do you have any results from SELECT * FROM it_task_update WHERE update_id = $id?
Try doing
print_r($rows1); before line $update_date2 = $rows1['update_date'];
and view the results.
You have a semicolon after the inner while
while($rows1 = mysql_fetch_assoc($updatesquery));
{
// stuff
}
should be
while($rows1 = mysql_fetch_assoc($updatesquery))
{
// stuff
}
You're trying to retrieve results inside processing your query from a query you never execute. Notice that you called mysql_query before you started looping through your results. Then notice you tried to start fetching results from a query you never executed inside your inner while loop.
Yes, it is OK, to have a while or another type of loop inside another one, because it depends on your business logic. You have forgotten to execute the second query before the while loop, though. It would be better if you did this with an INNER JOIN or something so you don't waste the resources.
Also, I would suggest you not to print out the DOM elements with echo, but have them there like this <div id="username">Username: <?php echo $username; ?></div>. However, even in your case there is place for keeping your code clean. Here is my suggestion:
<?php
$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$update_date = $rows['update_date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
echo '<div id="container\">'.
'<table>'.
'<tr>'.
"<div id=\"conid\">$id</div>".
"<div id=\"condate\">$date</div>".
"<div id=\"conproject\">$project</div>".
"<div id=\"contask\">$topic</div>".
"<div id=\"conselect\">$instby</div>".
"<div id=\"conselect1\">$inst</div>".
"<div id=\"condline\">$dline</div>".
"<div id=\"conocome\">";
$updatesquery = "SELECT * FROM it_task_update WHERE update_id=$id";
// you need to execute the query
$updatesresult = mysql_query($updatesquery);
while($rows1 = mysql_fetch_assoc($updatesresult)) // <-- no semicolon here
{
$update_date2 = $rows1['update_date'];
$status = $rows1['ocome'];
$update_id = $rows1['update_id'];
echo '<font color="#FF0000">'.$update_date2.'</font><br><br>'.$status.'<br>';
}
echo '</div>'.
"<div id=\"concomm\">$comm</div>".
"<div id=\"confin\">$fin</div>".
'</tr>'.
'</table>'.
'</div>';
}
?>
Update
Your inner while statements should not be terminated with a semicolon at the logic point. Please check the code above to see where I have placed the comment for you.