Why does only one of these .php functions work - php

I'm currently trying to put php results into an HTML table, and not sure why it is not working. The first block returns the data I need, so I know it's not a sql query issue. The second block (where I try to make it a table) returns the headers but no data.
Here is my code:
<h1>
<?php
while (odbc_fetch_row($weekadmissions)) // while there are rows
{
print odbc_result($weekadmissions, "date") . ", " .
odbc_result($weekadmissions, "attendance") .
"<br />\n";
}
?>
</h1>
<?php
print "<table><tr>";
print "<th>Date</th>";
print "<th>Attendance</th></tr>";
while (odbc_fetch_row($weekadmissions)) // while there are rows
{
print "<tr><td>" . odbc_result($weekadmissions,"date") . "</td>";
print "<td>" . odbc_result($weekadmissions, "attendance") . "</td></tr>";
}
print "</table>";
?>
Thanks!

The answer was that the result was consumed by the first loop - thank you FirstOne for the solution

Related

PHP displays only one word from MySQL record in URL variable

A PHP page displays only one word from a MySQL record in a URL variable, while it displays the correct 2 words in the HTML output, I tried many solutions such '".$row['book_category']."' and {$row['book_category']} etc. but I still get the same one result.
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "" . $row['book_category'] . "" . "</td>";
echo "</tr>";
}
?>
So now first $row insert is only one word in the url, while the second $row outputs two words properly as expected; the problem is I need the two words to be passed as variable in the URL.
URLs cannot have a space character, this is why you are seeing the first word only, you need to encode the value received using rawurlencode().
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "<a href='sidebar_cat_display.php?book_cat=" . rawurlencode($row['book_category']) . "'>" . $row['book_category'] . "</a>" . "</td>";
echo "</tr>";
}
?>
Keep in mind you will also need to adjust the sidebar_cat_display.php page to fetch the book_cat parameter from the URL accordingly if you aren't already.

one PHP function to handle different MySQLi select statements

I have two MySQL tables with number of columns. The table structure is given below,
1.pictures
postedON
caption
imageName
thumbName
imageLocation
thumbLocation
2.Videos
postedOn
category
Link
I am using the folowing PHP function to fetch data from DB using a select command.
function select($table){
if($this->db_connection){
$query = 'SELECT * FROM '. $table;
$result = mysqli_query($this->db_connection,$query) or die($this->db_connection->error);
//print_r($result);
//echo "Affected rows: " . mysqli_affected_rows($this->db_connection);
//var_dump($result);
echo "<table>";
echo "<tr>";
echo "<th>Date Posted</th>";
echo "<th>Category</th>";
echo "<th>Link</th>";
echo "</tr>";
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>" . $row['postedOn'] . "</td>";
echo "<td>".$row['category']. "</td>";
echo "<td>" . $row['link'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "db_connection is = " . $this->db_connection;
}
}
}
The problem with this function as you can see, it can only serve only one table and not dynamic. Can someone please explain the way to dynamically fetch data from different table with different number of columns using only one PHP function? Thanks
Try using mysqli_fetch_fields()
<?php
function select($table){
if($this->db_connection){
$query = 'SELECT * FROM '. $table;
$result = mysqli_query($this->db_connection,$query) or die($this->db_connection->error);
$fieldinfo = mysqli_fetch_fields($result);
echo "<table>";
echo "<tr>";
foreach ($fieldinfo as $val)
{
echo "<th>".$val->name."</th>";
}
echo "</tr>";
while($row = $result->fetch_assoc()){
echo "<tr>";
foreach ($fieldinfo as $val)
{
echo "<td>" . $row[$val->orgname] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}else{
echo "db_connection is = " . $this->db_connection;
}
}
It could be hard to explain given all the wrong premises you approach is based on, but I'll try.
First of all, you have to understand that a query like SELECT * FROM table has a very little use, next to none. Most of time you are always have some WHERE or at least LIMIT clause. So, such a function will have no use at all.
Next, you have to learn by heart that database interaction should never be intermixed with any output stuff like HTML.
Given these two premises above, your function should accept a fill qualified query as a parameter and return an array with data as a result.
For which array, in turn you can write a helper function to display its contents in the form of HTML table.
But again, such a generalized output function will be of little use as well, because, as you can see from your own example, different fields will need different formatting. So it's better to write output each time by hand.

HTML - MySQL Table delete entry

I have a problem. I want a Table on my page, which shows things from a MySQL DB. This works just fine:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=********', '********', '********');
$sql = "SELECT subject, givenOn, toDate, teacher, what FROM homework";
echo "<table>";
foreach ($pdo->query($sql) as $row){
// echo "Fach:" . $row['subject'] . " - Lehrer:" . $row['teacher'] . " - Was: " . $row['what'] . $
echo "<tr>";
echo "<td>", $row['subject'], "</td>";
echo "<td>", $row['teacher'], "</td>";
echo "<td>", $row['what'], "</td>";
echo "</tr>";
}
echo "</table>";
?>
But I want another column behind the "what" - one which contains a Link to remove the Line. I dont know how its possible to say the Links what he has to delete. My idea was a PhP Page which has arguments like:
delete.php?subject=math&teacher=smith&what=p13
But this is very unsecure, isnt it?
Have u any Ideas how I can solve this problem?
dunklesToast
yes, it is.
1, you need to control who can delete an item, which is a rbac system.
2, delete should use POST rather than GET.
3,use PDOStatement::bindParam to prevent sql injection.

php - using a hyper link to send variable used in data base to use get command to echoe results

I have a table with arrays pulling information from a database, I have linked the fix to be a hyperlink "click me for fix" I have entered the link to send the variable to a php that will use $GET to echoe the information.
code below , i am new to php and been racking brains . the only out put i get is Welcome . (done welcome to test if information was being passed)
<div id=list>
<?php
// Create connection
$con=mysqli_connect('172.16.254.111',"user","password","Faults"); //(connection location , username to sql, password to sql, name of db)
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//where statement in the sql syntax will select where in db to get infor, use AND to add another condition
$result = mysqli_query($con,"SELECT * FROM Fixes WHERE Product='Serv1U' AND Fault_type='Broadcast Manager'"); //this creates a variable that selects the database
//below is the echo statment to create the results in a table format, list collumn titles
echo "<table id=tables border='1'>
<tr>
<th>Products</th>
<th>Fault_type</th>
<th>Fault_Description</th>
<th>Fix</th>
</tr>";
//below is script to list reults in a table format, $row [row name on table]
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Product'] . "</td>";
echo "<td>" . $row['Fault_type'] . "</td>";
echo "<td>" . $row['Fault_Description'] . "</td>";
echo "<td>Click for Fix</td>"; //this is how you link into an echo, alsothe id=" hopefully means i can send ID information.
}
echo "</tr>";
echo "</table>";
// below closes the coonection to mysql
mysqli_close($con);
index.php:
Welcome <?php echo $_GET["Fix"]; ?>.
I'm lost. Any help is appreciated.
Thanks
?>
Is it just a typo here? $GET must be $_GET.
And it should be $row['Fix'] not $rows['Fix']! Note the 's'!

trying to take sql data and then define just one of the rows in a php variable

Hi I have the details of two car drivers getting pulled from a database but then I want to just grab one of the two and echo it out in php. I am able to take both drivers details and echo them both out using a while loop but not target just one of them:
By using the code below I echo out both records. Is there a way to just grab one of them?
while($row = mysql_fetch_array($result))
{
echo "A driver:<br />";
echo "Name:". $row['FirstName'] . " " . $row['LastName']."<br />";
echo "Latitude:". $row['CoordLat']."<br />";
echo "Longitude:". $row['CoordLong'];
$driverlat = $row['CoordLat'];
$driverlong = $row['CoordLong'];
}//end of while loop
Just remove the while loop ?
$row = mysql_fetch_array($result))
echo "A driver:<br />";
echo "Name:". $row['FirstName'] . " " . $row['LastName']."<br />";
echo "Latitude:". $row['CoordLat']."<br />";
echo "Longitude:". $row['CoordLong'];
$driverlat = $row['CoordLat'];
$driverlong = $row['CoordLong'];
You don't need a while loop at all.
Just do $row = mysql_fetch_array($result); on it's own separate line.
This will display the first row result.
You can just put a break; at the end of the while loop. Then only the first row is processed.
Better way would be to use no loop: Just assign the $row and do the render stuff.

Categories