Alright so I've been trying to replace a number from the database for the actual name, unfortunately it only returns nothing at all.
I'm not sure what I'm doing wrong here, Tried it multiple ways, as explained on other related questions, yet it doesn't seem to work.
The first while seems to work, but the second doesn't do its job..(although in this setup it gives nothing at all)
here is my code:
<?php
include_once 'db_connect.php';
$query = "SELECT bestel_nummer, bestel_datum, bestel_tijd, kkt_id, mwr_id
FROM bestellingen";
$response = mysqli_query($mysqli, $query);
if ($response){
echo '<table align="left" cellspacing="5" cellpadding="8">
<td align="left"><b>bestel nummer<b></td>
<td align="left"><b>bestel datum<b></td>
<td align="left"><b>bestel tijd<b></td>
<td align="left"><b>klanten nummer<b></td>
<td align="left"><b>Medewerker<b></td></tr>';
while($row = mysqli_fetch_array($response)){
$mwr_id = $row['mwr_id'];
$query2 = "SELECT 'voornaam' FROM 'medewerkers' WHERE 'mwr_id' = $mwr_id LIMIT 1";
$response2 = mysqli_query($query2, $mysqli);
while($row2 = mysqli_fetch_array($response2)){
echo '<tr><td align="left">'.
$row['bestel_nummer'].'</td><td align="left">'.
$row['bestel_datum'].'</td><td align="left">'.
$row['bestel_tijd'].'</td><td align="left">'.
$row['kkt_id'].'</td><td align="left">'.
$row2['voornaam'].'</td><td align="left">';
echo'</tr>';
}
}
echo'</table>';
}
else{
echo 'couldnt issue database query';
echo mysqli_error($mysqli);
}
mysqli_close($mysqli);
?>
Thanks in advance.
Maybe it's not the while loop, but the mysqli_query
Seems like the arguments in the second query have a wrong order
first query
$response = mysqli_query($mysqli, $query);
second query
$response2 = mysqli_query($query2, $mysqli);
This means no result, no while loop, check the response2 variable, the while loop should work, but actually the code itself looks unperformant.
If you wanna have the most performance you should do it with one query, with something like Group and Join. I don't know your structure but it should be possible.
If you try to query the db, in a loop it's a alway a very bad idea, image 100, 1000 users will trigger this code.. ohoohooo your server will probably go down men.
Related
I executed this code in localhost but it kept looping the table infinitely. How do I fix this? Is there something I should add? The looping never stops until I click the x button to stop loading the page.
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$query=("SELECT * FROM songs ORDER BY songid DESC");
$result = mysql_query($query) or die(mysql_error());
?>
<html>
<head>
<title>View</title>
</head>
<body>
Add New Data<br/><br/>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Song ID</td>
<td>Title</td>
<td>Artist</td>
<td>Genre</td>
<td>Language</td>
<td>Lyrics</td>
<td>Updated by</td>
</tr>
<?php
$counter == 1;
$res = mysql_fetch_array($result);
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
do {
echo "<tr>";
echo "<td>".$res['songid']."</td>";
echo "<td>".$res['title']."</td>";
echo "<td>".$res['artist']."</td>";
echo "<td>".$res['genre']."</td>";
echo "<td>".$res['language']."</td>";
echo "<td>".$res['lyrics']."</td>";
echo "<td>".$res['update']."</td>";
echo "<td>Edit | Delete</td>";
} while($res)
?>
</table>
You are missing $res = mysql_fetch_array($result); at the end of the loop. Currently, the while statements checks if $res is (still) truthy, but it never changes, so it will always evaluate to TRUE (and so, the loop will always continue to run).
I was just about to write the same thing as Daan Meijer. res never changes and so its always true for the while-statement. I just want to add that mysql_fetch_assoc($result) works a littlebit faster than mysql_fetch_array($result). So if you have a big database, mysql_fetch_assoc($result) is the better choice.
So I've been wrestling with this issue on and off for quite a while now, and just like driving around lost in a strange city, I am finally breaking down for direction! I am developing table with values from a database, but also need a column that will process user input. I have been able to display the table but my input is not updating the necessary database element. Code below:
<?php
include("pogsatbetbuddy.inc.php");
$cxn=mysqli_connect($host,$username,$password,$db_name)
or die("Did Not Connect");
$query="SELECT * FROM $tbl2_name ORDER BY $tbl2_name.$col_name ASC";
$result=mysqli_query($cxn,$query)
or die("Query Not Working");
echo"<table border='1'
<form name='payments' action='' method='POST'>
<tr>
<td class='update' colspan='5'>
<button data-theme='b' id='submit' type='submit'>Update</button>
</td>
</tr>
<tr>
<th class='profile'>Last Name</th>
<th class='profile'>First Name</th>
<th class='profile'>Saturday Payment Owing</th>
<th class='profile'>Enter Payment</th>
<th class='profile'>Saturday Balance</th>
</tr>";
while ($row=mysqli_fetch_assoc($result))
{
extract ($row);
echo"<tr>
<td class='profile'>$lastname</td>
<td class='profile'>$firstname</td>
<td class='profile'>$owingsat</td>
<td class='profile'><input type='number' name='paidsat' value=''/></td>
<td class='profile'>$owingsat-$paidsat</td>
</tr>";
}
echo "</form>";
echo "</table>";
This displays the table in the way I want. Having worked through the results of the following code, it seems that I am returning a null value, so I am thinking I have an issue with either the form action or the submit Update button, but can not find the solution after much experimentation and searching. Balance of code below:
if(isset($_POST['paidsat']))
{
$paidsat = $_POST['paidsat'];
if(($paidsat) != null)
{
$stmt = $cxn->prepare("UPDATE $tbl2_name SET paidsat = ? WHERE firstname=? and lastname=?");
$stmt->bind_param('sss', $paidsat, $firstname, $lastname);
$status = $stmt->execute();
if($status === true) //To check if the execute was successful
{
echo("<p class='click'>You have successfully added the payment for $firstname $lastname\n<br /></p>");
}
}
else echo"Not Successful";
}
else echo "<p class='click'>Make your changes as required</p>";
mysqli_close($cxn);
Everything comes to a crashing halt at the second if statement.....or should I say, although things look pretty, they don't function! Thanks in advance, appreciate any help!
Be sure you have a proper value for $tbl2_name checking
var_dump($tbl2_name)
in your code before the update
and for debug try using a string concatenation like
"UPDATE " . $tbl2_name . " SET paidsat = ? WHERE firstname=? and lastname=?";
and try use
if( $paidsat != NULL )
and last check if you have proper value for update
paidsat = ? WHERE firstname=? and lastname=?
Try
var_dump( $paidsat);
var_dump( $firstname);
var_dump( $lastname);
and build a proper select for test if you value math the rows you think and
test this select in you db console
I am retrieving information from the database and displaying it in a table, However i have noticed i only retrieve some and not all columns, It seems to be in 2s i.e:
No2, No 4, No6, No8 etc all show up correctly, but no 1, no 3, no5 doesnt show up at all, How would i solve this my retrieving code is:
include ("mysqldb.php");
$out = mysqli_query ($dbc, "SELECT * FROM `User-Comments`");
echo "<table align='center' border='1' cellspacing='3' cellpadding='3' width= '75%'>
<tr>
<td align='left'><b>Comments</b></td>
</tr>";
while ($row = mysqli_fetch_array ($out)){
echo "<tr>
<td align='left>".$row ['Usercom']."</td>
</tr>";
}
echo "</table>";
mysqli_close($dbc);
Is it a problem with my markup?.
An apostrophe (') is missing on this line:
<td align='left>".$row ['Usercom']."</td>
after "left".
Other than what others pointed out, let us go directly to the Query..
SELECT * FROM `User-Comments.
That is all you have. Try this:
SELECT * FROM `User-Comments` ORDER BY UserCom ASC
I have these two objects:
$userinfo->pilotid;
$departures->total;
I'm trying to get $departures->total for specific pilotid in $userinfo = $userinfo->pilotid.
However, I'm not sure how can I link them so it echoes A for B. I have something like this but it does not display anything.
<?php echo $pilotid->$departures->total; ?>
Additionally, the first object is called like this:
$pilotid = Auth::$userinfo->pilotid;
This is the structure of the table where the objects are gathered from, using a query.
Stemming from the data provided by the OP, I am assuming, that $departures has a 1:n relationship with $userinfo, $userinfo being the 1 containing the pilotid.
So, in oder to find out how many departures that pilot had in total, there's two possible ways, one by using a subquery, which would mean something like this:
SELECT (SELECT COUNT(*) FROM `departures` WHERE `pilot_id` = ID) as total, * FROM pilots;
In this case, your total would be in the total column of your $userinfo query.
The second attempt makes use of actual PHP. In this scenario, you do the counting yourself.
First step: Getting the pilot information:
$userinfo = array();
while($row = fetch()) {
$row->total = 0;
$row->departures = array();
$userinfo[$row->pilotid] = $row;
}
These lines will give you the pilot data keyed to their IDs in an array.
Step two. Glueing the departures to the pilots.
while($row = fetch()) {
if(isset($userinfo[$row->pilotid])) {
$userinfo[$row->pilotid]->departures[] = $row;
++$userinfo[$row->pilotid]->total;
}
}
If this isn't what you're looking for, I will be needing more information from you, however like this you will be able to get the departures of the pilots either by making use of the total variable in the $userinfo object, or by simply calling count on the departures array.
Another variant, which keeps the actual departures and the pilots apart would look like this:
First step: Getting the pilot information:
$userinfo = array();
while($row = fetch()) {
$row->total = 0;
$userinfo[$row->pilotid] = $row;
}
These lines will give you the pilot data keyed to their IDs in an array.
Step two. Glueing the departures to the pilots.
$departures = array();
while($row = fetch()) {
if(isset($userinfo[$row->pilotid])) {
$departures[] = $row;
++$userinfo[$row->pilotid]->total;
}
}
I hope you will find these suggestions useful.
Edit:
After a few additional information from the OP, I suggest changing the query used to access the information in question.
This is the original code by the OP
$dep_query = "SELECT COUNT(pilotid) as total, depicao, pilotid FROM phpvms_pireps GROUP
BY depicao, pilotid ORDER BY total DESC LIMIT 5";
$fav_deps = DB::get_results($dep_query);
foreach($fav_deps as $departure)
{
$dep_airport = OperationsData::getAirportinfo($departure->depicao);
$pilotid = Auth::$userinfo->pilotid;
?>
<tr class="awards_table1">
<td width="10%"><?php echo $departure->depicao; ?></td>
<td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
<td width="60%"><?php echo $dep_airport->name; ?></td>
<td width="20%"><?php echo $pilotid->{$departures->total}; ?></td>
</tr>
<?php
}
?>
First thing we'll change is the query used to get the departures. Why fetch all the information, if we actually only want the one of the pilot in question?
$pilotid = $userinfo->pilotid; //As per Chat discussion
$dep_query = "SELECT COUNT(depicao) as total, depicao FROM phpvms_pireps WHERE pilotid = $pilotid GROUP BY depicao ORDER BY total DESC LIMIT 5";
This query will return the Top 5 of the departures from the different airports, which have been run by the pilot in question. As for the rest:
$fav_deps = DB::get_results($dep_query);
if(is_array($fav_deps)) { //For the general use
foreach($fav_deps as $departure) {
$dep_airport = OperationsData::getAirportinfo($departure->depicao); ?>
<tr class="awards_table1">
<td width="10%"><?php echo $departure->depicao; ?></td>
<td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
<td width="60%"><?php echo $dep_airport->name; ?></td>
<td width="20%"><?php echo $departure->total; ?></td> //Here is the actually changed Layout code
</tr>
<?php
}
} else echo "This pilot didn't have any departures yet.";
?>
With these alterations, your code should output the desired result. It is completely untested though. However it should give you the right idea.
I think what you need is this (note the curly brackets):
<?php echo $pilotid->{$departures->total}; ?>
Unless I'm misunderstanding the question...
I have a code that I have used over and over again before and now it's messing up. All I want to do is list information from the database into the table on the page, but now it will only show one result, instead of all the results it has found.
<table>
<tr><td style="background-color:#009745; color:#FFFFFF"><center><strong>Address Book</strong></center></td></tr>
<tr>
<?php
$getids = mysql_query("SELECT id, first_name, last_name FROM accounts WHERE s1='$id' ORDER BY id DESC", $db);
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
?>
<td><?= $ab_id ?> - <?= $ab_fn . " " . $ab_ln ?></td>
<?php
} else {
?>
<td><center>No Contacts</center></td>
<?php
}
?>
</tr>
</table>
please help me with this.
Thank You for your help :)
I love this site!! I can always get answers when I need them.
I saw two thing wrong
you are using mysql_fetch_array and later you are using string indexes to print the result
print the things in loop it is overriding values and just storing last row
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_assoc($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
echo '<td>'.$ab_id.' -'. $ab_fn.''.$ab_ln.' </td>';
}
In this messy code you're closing the while loop too early:
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
Only the last retrieved row is used later on. Also, don't use mysql_fetch_array if you're not accessing the numeric indeces of your result. Use mysql_fetch_assoc instead.