I'm having a hard time figuring out how to get my table to display as a web page.
Everything else is working fine. I've been able to use a form to write records TO the table, but I'm simply unable to display the table.
Here's my code:
$host = "***";
$userName = "***";
$passWord = "***";
$db = "doctorWho";
mysql_connect($host,$userName,$passWord);
mysql_select_db($db) or die( "Unable to access database");
$query = "SELECT * FROM patients";
$result = mysql_query($query);
echo "<table border='1'>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Address</th>
<th>Age</th>
<th>Sex</th>
<th>Marital Status</th>
<th>Medication</th>
<th>Date Rx'd</th>
<th>Quantity</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['sex'] . "</td>";
echo "<td>" . $row['maritalStatus'] . "</td>";
echo "<td>" . $row['medication'] . "</td>";
echo "<td>" . $row['medsWhen'] . "</td>";
echo "<td>" . $row['medsQuant'] . "</td>";
echo "</tr>";
}
echo "</table>";
You want mysql_fetch_array(). Currently, you are mixing two different extensions. Each takes something different, relevant to the extension.
That aside, you should consider either PDO or MySQLi, which is where the wrong function you attempted to use is from. This article should help you decide which you could possibly use.
From php ref.
mixed mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )
mysqli_fetch_array need to mysqli_result, but you are using mysql_query, you should try mysql_fetch_array
// I think your connection should look like this
$conn = mysql_connect($host,$userName,$passWord);
mysql_select_db($conn,$db) or die( "Unable to access database");
// another is your using mysql as connection so you can use mysqli functions on that rofl.
//Instead use mysql functions
mysql_fetch_array();
note: Another one is instead of using # sign to handle error. Its much more best practice to use.. ini_set() or configuring php.ini in handling displaying of errors.
Just change:-
while($row = mysqli_fetch_array($result))
to
while($row = mysql_fetch_array($result))
Related
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 get the data from my database. The page does not change when I upload the file. Where am I wrong?
verifycheck.php
<?php
$con=mysql_connect ("###", "###", "###");
mysql_select_db ("db_name", $con);
$result = mysql_query($con,"SELECT * FROM db_tablename");
echo "<table border='1'>
<tr>
<th>username</th>
<th>email</th>
<th>password</th>
<th>confirm_password</th>
</tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['confirm_password'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Connection object should be second parameter and query string should be first parameter.
Try this
$result = mysql_query("SELECT * FROM db_tablename",$con);
Instead of
$result = mysql_query($con,"SELECT * FROM db_tablename");
Mysql function is deprecated and will remove in future go for Mysqli or PDO for preventing sql injection
Remove $con, from the mysql_query
As mysql_query() expects first parameter to be the SQL Query, not the SQL connection.
$result = mysql_query("SELECT * FROM db_tablename");
Please Check with your connection parameters as localhost,username password, database and also changing the
$result = mysql_query($con,"SELECT * FROM db_tablename");
as
$result = mysql_query("SELECT * FROM db_tablename",$con);
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'");
I'm trying to throw together a simple inventory database for a small customer of mine (I normally don't do WebDev stuff) but I'm a little stumped. I have what I think should work, but I get no results in my table. I know the query is good since I get the expected results when querying directly to the database, unless PHP expects different formatting of my SQL statement. here is my page:
<html>
<head>
<title>Inventory</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","user","pass","db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT
products.name,
products.sku,
inventory.quantityfry,
inventory.quantityjuv,
inventory.quantityadult,
inventory.notes,
inventory.location,
inventory.owner
FROM
products
INNER JOIN
inventory
ON
products.sku=inventory.sku";
$result = mysqli_query($query);
echo "<table border='1'>
<tr>
<th>Species</th>
<th>SKU</th>
<th>Fry Count</th>
<th>Juvie Count</th>
<th>Adult Count</th>
<th>Notes</th>
<th>Location</th>
<th>Owner</th>
</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['sku'] . "</td>";
echo "<td>" . $row['quantityfry'] . "</td>";
echo "<td>" . $row['quantityjuv'] . "</td>";
echo "<td>" . $row['quantityadult'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['owner'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
When I load the page, all I see is my HTML table headers, but no data. No error messages, either. What am I missing?
you don't see error messages because you don't bother checking for them. You're calling mysqli_query incorrectly, and since you don't check for errors, never see them:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
^^^^---required
Since you used it incorrectly, the query call returns false. You then blindly try to fetch result rows from that boolean FALSE, which would lead to further errors and your while() loop never executing at all.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JSON encode MySQL results
This is using the Mysql fetch array to display the result. how can i replace the following tags using JSON encode.
I am trying to get a table from a database, and this does the job of outputting a data... this is using the Mysql fetch array to display the result. how can i replace the following tags using JSON encode.
PHP File
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<?php
while($r = mysql_fetch_assoc($result)) $rows[] = $r;
print json_encode($rows);