newbie PDO question...
I think (hard to tell - buried in a wrapper class) I am doing a select query using:
PDO::FETCH_ASSOC
wrapper:
return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
I am getting back just 1 record - like to display the results of just current customer....
My query is:
$results = $db->select("mytable", "id = 201"); //just 1 exact record)
then I can loop like:
foreach ($results as $result) {
....
?>
<tr>
<td><?php echo $result["First"]; ?></td>
<td><?php echo $result["Last"]; ?></td>
<td><?php echo $result["id"]; ?></td>
</tr>
This all works fine, but since I only have 1 exact CUSTOMER record - I don't need to LOOP anything.
My question is:: How do I display the columns without a loop?
The following failed:
echo $results["First"];
echo $results["First"][0];
echo $results["First"][1];
So what can I use to make it work?
Use fetch instead of fetchAll
$row = $pdostmt->fetch(PDO::FETCH_ASSOC);
You have to use the fetch function of PDO library.
Related
I have this code in php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["Sec_No"]; ?></td>
<td><?php echo $row["Sec_Sub"]; ?></td>
<td><?php echo $row["Sec_Lec"]; ?></td>
<td><a href="Theo_Entery_Sheet.php?Sec_No=<?php echo $row["Sec_No"]; ?>"> ُEnter Marks/>a></td>
</tr>
<?php
I want to pass the field [Sec_Sub] beside the field [Sec_No] that is exist in href statment as query string to another page (Theo_Entery_Sheet.php)
Theo_Entery_Sheet.php
i want the part of SQL "WHERE" take two conditions Sec_No AND Sec_Sub that is comming from above page
...
...
...
$sql = "SELECT theo_stu_sections.Sec_No, theo_stu_sections.St_No, students.Name, theo_stu_sections.Mid,
. "FROM students INNER JOIN theo_stu_sections ON students.St_ID = theo_stu_sections.St_No\n"
. "WHERE Sec_No='".$_GET['Sec_No']."'\n"
. "ORDER BY students.Name;";
thank You
I want the page to receive two parameters from the page which send query string
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
I'm trying to bring the user details by clicking on "detail" in a table (all the rows in the table are from the db) I bring the info to the table with this (code found it somewhere here),
<?php foreach ($link->query('SELECT * from data') as $row){ ?>
<td><?php echo $row['fullname'] ?></td>
this works perfect, and on the table I add this.
<?php echo 'Details'; ?>
now I'm trying to get the details for each user by using the same code in the query I used before but it doesn't work.
<?php $id = $_GET['AutoID'];
foreach ($link->query('SELECT * from data where AutoID=$id') as $row){ ?>
<td><?php echo $row['fullname'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['phone'] ?></td>
Thanks a lot, I hope I explain myself well.
Change this
$link->query('SELECT * from data where AutoID=$id')
to that
$link->query('SELECT * from data where AutoID='.$id)
BTW. consider use some filter for $_GET params
I'm writing a simple php page for an assignment, and one of the criteria is to use both mysqli_fetch_assoc and mysqli_fetch_row. I'm using the same query for both:
<?php
// Perform database query
$query = "SELECT * FROM player JOIN stat ON player.playerId = stat.playerId";
$result = mysqli_query($dbconnection, $query);
if (!$result) {
die("Database query failed");
}
?>
When I run this query in my database, it returns 3 rows as expected. In my webpage, I use mysqli_fetch_assoc($result) first, and it renders an unordered list with the information as expected. I then proceed to use mysqli_fetch_row($result) to display more information, but the second while loop doesn't yield any table data (just the content of the th tags).
<h1>The Players</h1>
<ul>
<?php
// Use return data with mysqli_fetch_assoc
while($row = mysqli_fetch_assoc($result)) {
// output data from each row
?>
<li><?php echo $row["playerId"]; ?></li>
<li><?php echo $row["fname"] . " " . $row["lname"]; ?></li>
<li><?php echo $row["team"]; ?></li>
<li><?php echo $row["position"]; ?></li>
<?php echo "<hr />";
}
?>
</ul>
<h1>The Stats</h1>
<table>
<th>Player</th>
<th>Batting Avg</th>
<th>Homeruns</th>
<th>RBIs</th>
// DATA BELOW THIS POINT IS NOT RENDERED BY THE WEBPAGE
<?php
// Use return data with mysqli_fetch_row
while($row = mysqli_fetch_row($result)) {
?>
<tr>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[8]; ?></td>
<td><?php echo $row[9]; ?></td>
<td><?php echo $row[10]; ?></td>
</tr>
<?php
}
?>
</table>
<?php
// Release returned data
mysqli_free_result($result);
?>
I'm going to be writing a lot of php for this class in the coming weeks, so I'd really like some tips on how to troubleshoot these kinds of errors myself. Are there methods I can use to easily check the content of $result to see if any resources were actually passed through? It appears to me that $row in the second loop isn't getting assigned to anything, so it just doesn't execute the echo commands.
Ghost is correct. When you iterate over the records at the end it doesnt re-set by itselt and it's not a circular list. Because of that you have to manually set it to 0 with $obj->data_seek(int, ie 0); or in procedural style mysqli_data_seek($result,0);
Here is my current code:
$varVeh=$_POST['Veh_num'];
$sql_HiScores = "SELECT
c.course_name as course,
e.distance as distance, e.score as score,
e.time as time, e.user as User
FROM hc_entries e
LEFT JOIN hc_course c on e.course=c.course_num
WHERE e.vehicle=$varVeh
ORDER BY course, score DESC";
$result_HiScores = mysql_query($sql_HiScores);
$sql_vehName="SELECT Veh_name FROM hc_vehicle_type WHERE Veh_num=$varVeh ";
$result_vehName = mysql_query($sql_vehName);
$vehName=mysql_fetch_assoc($result_vehName);
echo "<table><tr><th>Best Scores for ".$vehName['Veh_name']."</th></tr></table>";
echo "<table border='1'>";
echo "<tr><th>Course</th><th>Score</th><th>Distance</th><th>Player</th><th>Time</th></tr>";
while($row = mysql_fetch_array($result_HiScores))
{
echo "<tr>";
echo "<td>" .$row['course'] . "</td>";
echo "<td>" .$row['score'] . "</td>";
echo "<td>" .$row['distance'] . "</td>";
echo "<td>" .$row['User'] . "</td>";
}
echo "</table>";
What I think I have to do is create a query that selects * from e.course that builds an array. Then cycle through the existing query with the array results. Finally, I would like to display individual tables for each course and limit it to the top 5 results for each course.
Can anyone confirm or deny my logic, and point me in a direction?
First of all, you shouldn't be using the mysql_ functions, they're deprecated. At the least, you should switch to mysqli_ (a pretty easy switch), or better, learn how to use PDO. It's a bit different and more involved to switch, but your code will be better and safer for it.
With that out of the way: your logic is pretty accurate. Limiting your results to the top 5 results for each course in one query isn't something that's easily done with SQL to my knowledge, so your plan is good: query a list of courses, then cycle through them with your existing query, running it once for each course, with a LIMIT 5 to get the top 5.
You might as well keep the table generation within this loop as well, since it's a table-per-course. You'd want to move the VehName query out of the loop, since you only need to run that once.
Also, some unsolicited PHP advice: any text outside of the tags will just be output directly, so take advantage of its built-in-templating and alternative syntax to make your table generation code nicer:
<?php
/* Gather your data here... */
?>
<table>
<tr><th>Best Scores for <?php echo $vehName['Veh_name'] ?></th></tr>
</table>
<table border='1'>
<tr>
<th>Course</th>
<th>Score</th>
<th>Distance</th>
<th>Player</th>
<th>Time</th>
</tr>
<?php while($row = mysql_fetch_array($result_HiScores)): ?>
<tr>
<td><?php echo $row['course'] ?></td>
<td><?php echo $row['score'] ?></td>";
<td><?php echo $row['distance'] ?></td>";
<td><?php echo $row['User'] ?></td>";
</tr>
<?php endwhile; ?>
</table>
put the entire table html inside the while loop and add 'LIMIT 5' to the end of your query
I would like to use a for loop to access 10 objects of the same class.
The reason is that I want to make a table of the data and I don't find it proper to write by hand all the html markup for each row (object) on the table.
My code is :
<?php
for ($i=1;$i<=10;$i++){
?>
<tr>
<td><? echo $i;?></td><td><?php echo $office1->pc;?></td>
<td><?php echo $office1->pc*$office1->pcPowerPerUnit;?></td>
<td><? echo $office1->printer;?></td>
<td><?php echo $office1->printer*$office1->printerPowerPerUnit;?></td>
<td><? echo $office1->lights;?></td>
<td><?php echo $office1->lights*$office1->lightsPowerPerUnit;?></td>
<td><? echo $office1->aircondition;?></td>
<td><?php echo $office1->aircondition*$office1->airconPowerPerUnit;?></td>
<td><? echo $office1->server;?></td>
<td><?php echo $office1->server*$office1->serverPowerPerUnit;?></td>
</tr>
<?php } ?>
What I thought that could be done is to change the references $office1->pc (for example) to $office[$i]->pc or something like that but that doesn't seem to work. I also searched for object iteration in the php manual but that wasn't helpful.
The number of objects is fixed (10) and the properties are already calculated and ready to be echoed out.
You can use a special syntax to refer to your variables like that. Ty this:
<?php echo ${'office'.$i}->pc;?>
Use variable variables:
for ($i = 1; $i <= 10; $i++) {
// ...
$officeVar = 'office' . $i;
// Now you can use $officeVar as a variable variable:
$$officeVar->pc;
// equivalent to:
$office1->pc;
// when $i == 1
// ...
}
foreach( array( $office1, $office2, $o3, $o4) as $o ) {
?>
...
... <?php echo $o->pc;?> ...
<?php
}
Extend the array as needed. Object variables hold a handle refering to the object so this is a cheap operation and not a copy.
First of all store the column output values into an array:
$columns = array($i, $office->pc, $office->pc*$office->pcPowerPerUnit, ...);
you can then output the columns independent to the values in your variables:
echo '<tr><td>', implode('</td><td>', $columns), '</td></tr>';
The only thing left is that you iterate over all those variables you have. Taken an array is much better than using $office1 to $office10:
foreach($offices as $i => $office)
{
$columns = array($i, $office->pc, $office->pc*$office->pcPowerPerUnit, ...);
echo '<tr><td>', implode('</td><td>', $columns), '</td></tr>';
}
Done. However if you don't want to change due to some reason (e.g. not changing too much at once), you can do similar:
foreach(range(1, 10) as $i)
{
$office = ${'office'.$i};
$columns = array($i, $office->pc, $office->pc*$office->pcPowerPerUnit, ...);
echo '<tr><td>', implode('</td><td>', $columns), '</td></tr>';
}