For some reason this is returning everything from the member_details table when all i want is to return a single row that equals to the selected item in the drop down list.
Here is the php for it: i thought it would return the row which equaled the value of the drop down but its just listing it all.
<?php
if (isset($_POST['members'])) {
$ResultSet = getTableResults("member_details");
echo "<h1> Member Details </h1>";
echo "<table border='1' cellpadding='6'>";
echo "<tr> <th>Id</th> <th>Name</th> <th>Job</th> <th>Wage</th> <th>Hobby</th> ";
foreach ($ResultSet as $row) {
echo "<tr>";
echo "<td>" . $row ['member_id'] . "</td>";
echo "<td>" . $row['first_name'] . " " . $row ['second_name'] . "</td>";
echo "<td>" . $row['job'] . "</td>";
echo "<td>" . $row['wage'] . "</td>";
echo "<td>" . $row['hobby'] . "</td>";
echo "</tr>";
}
echo "<table>";
}
?>
If you want to see more code to make more sense of it please ask and il edit and update the question.
function getTableResults() {
$sql = "SELECT DISTINCT member_details.member_id, members.first_name, members.second_name, member_details.wage, member_details.job, member_details.hobby
FROM members
INNER JOIN member_details
ON members.member_id=member_details.member_id";
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
You do not have any Condition while requesting your information, getTableResults() fetches all Data that is stored to your table. You need to change that function and add a WHERE xy condition. Probably something like this:
function getTableResults( $id ) {
$sql = "SELECT DISTINCT member_details.member_id, members.first_name,members.second_name, member_details.wage, member_details.job, member_details.hobby
FROM members
INNER JOIN member_details
ON members.member_id=member_details.member_id WHERE member_details.member_id = '$id'";
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
Then Call the function with the requred member-id as parameter.
Anyway, I'd recommend you to inform yourself about prepared statements to prevent SQL-Injection here!
Related
Finally jumping into some PHP for the first time and I've written this program and i'm stuck. I've searched all over the place for about 2 hours to find a solution.
Basically I'm connecting to my local database and trying to grab all the rows from my 'songs' table, and display them by their names. Instead of getting their names, i'm getting a notice that says "Notice: Array to string conversion in C:\xampp\htdocs\musiclibrary\index.php on line 47"
My current output looks like this:
Title Artist Genre
Array Array Array
Array Array Array
Array
And then my code is...
<?php
// Require configuration file
require_once 'config.php';
// Connect to the database
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
// Check for database connection error
if(!$db_server)
{
die("Unable to connect to MySQL: " . mysql_error());
}
// Select a database
// The mysqli_select_db() function is used to change the default database for the connection.
mysqli_select_db($db_server, $db_database);
$prompt = array('Story title', 'Time', 'Person');
$prompt = array('Story title', 'Time', 'Person');
// Page title
echo "<h1>My Music Collection</h1>";
// Get music collection
$query = "SELECT * FROM songs";
$result = mysqli_query($db_server, $query);
$rows = mysqli_num_rows($result);
// If rows exist
if($rows > 0)
{
// Create HTML table
echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";
// Loop through each row in the database table
for($j = 0; $j < $rows; $j++)
{
// Build HTML table row
//PROBLEM LIES HERE ON THESE MYSQL_FETCH_ASSOC PARTS
echo "<tr>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'title') . "</td>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'artist') . "</td>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'genre') . "</td>";
echo "</tr>";
}
echo "</table>";
}
// If there are no songs in the database table
else
{
echo "There are currently no songs on file.";
}
?>
Any solutions to output the names of the rows in my database? Thanks!
Use the code below to replace your code
extract the values to an array first
show the values in table row
$values = mysqli_fetch_assoc($result);
echo "<tr>";
echo "<td>" . $values ['title']. "</td>";
echo "<td>" . $values ['artist'] . "</td>";
echo "<td>" . $values ['genre')]. "</td>";
You need to loop the mysqli_fetch_assoc function to count row then loop the row result to get value
Here is the code :
if($rows > 0){
echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";
// Loop through each row in the database table
while($row = $result->mysqli_fetch_assoc()){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
Hope it helps!
mysqli_fetch_assoc() function accepts only one parameter.
array mysqli_fetch_assoc ( mysqli_result $result )
Correct way to do this would be:
$query = "SELECT * FROM songs";
if ($result = mysqli_query($db_server, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
}
I wrote this code to retrieve some rows form database
session_start();
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = " . $_SESSION['user_id'] ." and password = " . $_SESSION['user_pass'];
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result))
{
echo "this academic transcripts for " . $row["name"];
echo " and the id is " . $row["id"];
}
$q1 = " SELECT student_record.course,student_record.grade,student_record.term,coe_courses.crd
FROM student_record INNER JOIN coe_courses ON student_record.course_number = coe_courses.course_number
where student_record.id = ".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q1 ) ;
if($row = mysqli_fetch_array($result))
{
echo "<br />";
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>coe_courses</th>";
echo "<th>terms</th>";
echo "<th>Grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row["course"]. "</td>";
echo "<td>" . $row["term"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
echo "</table>";
}
The problem is that only shows the first row while I have three rows in phpMyAdmin.
enter image description here
You need to call fetch_* repeatedly to retrieve all rows from your result set; each time you call it it retrieves the next row in the result set.
In your sample code above, you would replace
if ($row = mysqli_fetch_array($result))
{
with
while ($row = mysqli_fetch_array($result))
{
This will loop until fetch_array tries to read beyond the last record in $result, at which point fetch_array returns false and the loop exits.
Ok i am playing around with this idea for a system i am working on. I want to display data from mysql into a table. I can do this easy. What i would like to do is this. For the ID i would like to make it a link where i pass the id to the next page.
<?php
$con=mysqli_connect("example.com","username","password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
What i want to do is in the (echo "" . $row['ID'] . "";) I would like to convert it to a link so it would be something like this: example.com/report.php=24 or something like that. What i need to do is find out how to pass the ID and also how to convert the echo to a hyperlink.
Also on the report page it will display all data for id=24 or what ever the id is into a form that i have setup.
Can anyone help me with this.
Try This
echo '
<td>'.$row["ID"].' </td>';
To make a clickable link you use an anchor tag <a></a> In the href attribute you add the form name report.php and follow that with a ? to seperate that from the parameter list. You then add name=value pairs after the ? for the data you wish to pass eg href="report.php?id=24"
So to make the first page contain a clickable link you do something like this
echo '<td><?php echo $row['ID']; ?></td>';
Now in the report.php script you access the passed data by looking at the $_GET array in PHP
<?php
// check incoming params exist
if ( ! isset($_GET['id'] ) {
// missing param, go to an error page for example
header('Location: error.php');
exit;
}
// You can now use $_GET['id'] which will be the id number passed
// any way you want.
// For example using a PDO connection called $pdo
$table = "table1";
$sql = "SELECT * FROM $table WHERE id = :id";
try {
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->FetchAll(); // $rows now contains all the results of the query
}
catch( PDOException $e) {
echo $e-getMessage();
}
foreach ( $rows as $row ) {
// do things with the $row['column_name'] data
}
I always prefer using direct HTML for something like this.
<td>Link Name</td>
Try this -
while($row = mysqli_fetch_array($result))
{
$link = "example.com/report.php?id=".$row['ID'];
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
I want to show all data from my table.
But if I use/add ORDER BY id DESC or any code after $sql="SELECT * FROM $tbl_name, then last row is not showing.
<?php
include "db.php";
$tbl_name="report"; // Table name
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$ro = mysql_fetch_array($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count>=1) {
echo "<table border='1' align='center' cellpadding='10'>
<tr>
<th>Reporter</th>
<th>Message</th>
<th>Reporter Ip Address</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . $row['to'] . "</td>";
echo "<td class='middle'>" . $row['ip'] . "</td>";
echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
else {
print "<p align='center'>Nothing found.</p>";
}
?>
Of course when you used the DESC, it starts off the highest ID. Then the invocation of:
$ro = mysql_fetch_array($result); // this is the first row.
It fetches the first row.
Then your loop: while($row = mysql_fetch_array($result)) starts off with the second row.
So just remove this $ro = mysql_fetch_array($result); unneeded fetching line.
Obligatory Note:
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Sample PDO Usage:
<?php
$db = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
$query = $db->query('SELECT * FROM report ORDER BY id DESC');
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
if(count($rows) > 0) {
echo "
<table border='1' align='center' cellpadding='10'>
<tr>
<th>Reporter</th>
<th>Message</th>
<th>Reporter Ip Address</th>
<th>Action</th>
</tr>
";
foreach($rows as $row) {
echo "<tr>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . $row['to'] . "</td>";
echo "<td class='middle'>" . $row['ip'] . "</td>";
echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";
echo "</tr>";
}
echo '</table>';
} else {
echo "<p align='center'>Nothing found.</p>";
}
?>
You have an extra mysql_fetch_array($result); before the loop.
You must generete the Sql query String correctly. Like this:
$sql = "SELECT * FROM ".$tbl_name." ORDER BY id DESC";
In Php there are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
More info -> http://php.net/manual/en/language.operators.string.php
Hope it helps.
I need to filter a session table, normally this is how I show the table list.
<?php
$con=mysqli_connect("localhost","root","","esc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>PIN</th>
<th>Author</th>
<th>Author ID</th>
<th>Entry Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sid'] . "</td>";
echo "<td>" . $row['spin'] . "</td>";
echo "<td>" . $row['spuname'] . "</td>";
echo "<td>" . $row['suid'] . "</td>";
echo "<td>" . $row['sdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I was wondering if it's possible on this part...
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
to add Where spuname = 'spuname'
I guess the way I'm doing it is wrong.
What I wanted to do is to show filtered tables, so the one that has the session will see only his logs.
mysqli_query("SELECT * FROM sponsor Where spuname = 'spuname'",$con);
try putting $con after your query
You can use where clause in your query. it would be
$result = mysqli_query($con,"SELECT * FROM sponsor Where spuname = 'spuname'");