Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm recently started to learn php & mysql,
i wanna know what's wrong with this
function show(){
$query=mysql_query(" SELECT * FROM family ");
if (!$query) {
die("invalid query ".mysql_error());
}
while ($row=mysql_fetch_array($query)) {
echo "<tr>
line 24 ===><td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['lname']; ?></td>
</tr>";
}
}
as you can see i have a function for show my data fields,
and i have a table in another page that i want to by just including & calling the function,my variables appear in the table,but i keep getting this stupid error:
help plz
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\php\lib.php on line 24
for those who wanna suggest this below code:
echo "<tr>
<td>". $row['id'] ."</td>
<td>". $row['name'] ."</td>
<td>". $row['lname'] ."</td>
</tr>";
i did try this and it works but it wont show my date in the table rows
my purpose of this is that i want it to show my each data variables in a table row, any other good way you know?
Change <td><?php echo $row['id']; ?></td> to <td>".$row['id']."</td>
You're trying to bring php code inside a string, which won't work. Just concatinate the strings using the 'dot' operator. Take a look here for more information.
Edit as requested:
echo "<table>";
while ($row=mysql_fetch_array($query)) {
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['name']."</td>
<td>".$row['lname']."</td>
</tr>";
}
echo "</table>";
Instead of echoing, you need to just close and reopen your PHP tags:
function show(){
$query=mysql_query(" SELECT * FROM family ");
if (!$query) {
die("invalid query ".mysql_error());
}
while ($row=mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['lname']; ?></td>
</tr>
<?php
}
}
Alternately, without opening and closing:
function show(){
$query=mysql_query(" SELECT * FROM family ");
if (!$query) {
die("invalid query ".mysql_error());
}
while ($row=mysql_fetch_array($query)) {
echo "<tr>
<td>". $row['id'] ."</td>
<td>". $row['name'] ."</td>
<td>". $row['lname'] ."</td>
</tr>";
}
}
try this
function show()
{
$query = mysql_query("SELECT * FROM `family`");
//check if query failed
if (!$query) {
die("invalid query ".mysql_error());
}
//if there are more than 0 rows in table, we create the table
if (mysql_num_rows($query) > 0)
{
echo "<table>";
//loop thru array and create table rows
while ($row = mysql_fetch_array($query))
{
echo "<tr>
<td>". $row['id'] ."</td>
<td>". $row['name'] ."</td>
<td>". $row['lname'] ."</td>
</tr>";
}
echo "</table>";
}
else //no rows, we display an error
{
echo 'Table is empty';
}
}
also use mysqli_* because mysql_* is depreciated
Related
I have a MySql database online for transcriptions of document images. Those images are located on another website. I want to include the link to the actual image in the database so that the person researching can click the link and go directly to the image.
I have done the following:
Created a column named IMAGE
In my excel spreadsheet that will be uploaded via ODBC to PhpMyAdmin for my datbase, i have included the IMAGE
When the file is uploaded via ODBC, I can see the link in the Table on PhpMyAdmin for my database.
I have also included the IMAGE column to be shown on my results page and the column does appear.
But the hyperlink does NOT appear at all on the results page for any entry that has a link in the Image column so there is nothing to click on to go directly to the image
What am I doing wrong?
I wrote this up quickly to try and help you out. You will need to change DBHOST, DBUSERNAME, yourtablename etc to your databases information.
<?php
$con=mysqli_connect("DBHOST","DBUSERNAME","DBPASSWORD","DBNAME");
$result = mysqli_query($con,"SELECT * FROM 'yourtablename'");
echo "<table border='1'>
<tr>
<th>Notes</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NOTES'] . "</td>";
echo "<td>" . $row['IMAGE'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Edit: Here is an alternate solution that will put the link formatting in for you.
I wrote this up quickly to try and help you out. You will need to change DBHOST, DBUSERNAME, yourtablename etc to your databases information.
<?php
$con=mysqli_connect("DBHOST","DBUSERNAME","DBPASSWORD","DBNAME");
$result = mysqli_query($con,"SELECT * FROM 'yourtablename'");
echo "<table border='1'>
<tr>
<th>Notes</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NOTES'] . "</td>";
echo "<td><a href='" . $row['IMAGE'] . "'>Link</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I've an csv file with plant code like X001, X005, X019...
In mysql table plantcode=username.
I want to fetch all the plantcodes from my DB table where username are not present in my CSV file and deleted =0.
mycode
<?php
$file = fopen("aa.csv","r");
echo "<pre>";
//print_r(fgetcsv($file));
?>
<tr>
<td width="10%"><label>Username</label></td>
<td width="10%"><label>service center id</label></td>
<td width="10%"><label>service center name</label></td>
</tr>
<?php
$arr= fgetcsv($file);
foreach ($arr as $k => $v) {
$q= "SELECT * FROM `service_center` WHERE `deleted` = 0 AND username NOT LIKE '$v'";
$r = mysql_query($q);
$nr = mysql_num_rows($r);
if ($nr > 0) {
WHILE ($rows = mysql_fetch_array($r)) {
?>
<td><?php echo $rows['username'] ?></td>
<td><?php echo $rows['service_center_id'] ?></td>
<td><?php echo $rows['service_center_name'] ?></td>
<?php
}
}
else{
?>
<tr>
<td><?php echo $k;?></td>
<td><?php echo $v; ?></td>
<td><?php echo "No service center found";}?></td>
</tr>
<?php
}
fclose($file);
?>
I get output for each query like this
If username=X008 then from CSV i dont get X008 for one loop and in the next loop I get X008 and ,dont get present value plant code, But I want something like this
select * from service_center WHERE username != "X001, X007.." AND deleted = 0;
use NOT IN($name_array)
Pass username array in NOT IN
select * from service_center WHERE username NOT IN ("X001", "X007") AND deleted = 0;
This page is supposed to echo posts from the post table, and link them with images that have the same post_id in the images table. Each post can contain multiple images or no images at all. I want to be able to echo all the images that are linked to a specific post.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("db.php");
$select_post = "select * from post as p
union
select img from images as i
on i.post_id = p.post_id";
$run_post = mysqli_query($conn, $select_post);
while ($row_post=mysqli_fetch_array($run_post)){
$post_id = $row_post['post_id'];
$post_title = $row_post['post_title'];
$post_date = $row_post['post_date'];
$post_author = $row_post['post_author'];
$post_content = substr($row_post['post_content'],0,100);
$post_image = $row_post['img'];
$post_image .= '<img src="post_image/'.$post_image.'" width="60" height="60"/>';
?>
<tr align="center">
<td><?php echo $post_id; ?> </td>
<td><?php echo $post_date; ?></td>
<td><?php echo $post_author; ?></td>
<td><?php echo $post_title; ?></td>
<td><?php echo $post_image; ?></td>
<td><?php echo $post_content; ?></td>
<td><center><button>X</button></center></td>
<td><center><button>Edit</button></center></td>
</tr>
<?php
}
?>
First, your query should be something like:
$select_post = "SELECT p.*, i.img
FROM post p
LEFT JOIN (
SELECT
group_concat(img) as img,
post_id
FROM images
GROUP BY post_id
) i ON i.post_id = p.post_id";
// this query can be optimized a bit i think, but it should do the job.
Then, you need to $images = explode(',', $row_post['img']); inside your while to produce an array of images.
Loop over $images and process them as you need.
$post_image = '';
if(count($images) > 0){
foreach($images as $image){
$post_image .= '<img src="post_image/'.$image.'" width="60" height="60"/>';
}
}
I did try some solutions from the same problem with mine but nothings working. I bet it's totally in my code. I'm trying to place them in every table that has been looped, It worked I was able to get a button on every table but I can't get them to work. Here's my code
<?php
$con = new PDO('mysql:host=localhost; dbname=library_member', $username = 'root', $password = '');
$q = "SELECT * FROM user_list";
$stmt = $con->prepare($q);
$stmt -> execute();
?>
<table border="1">
<td>ID</td>
<td>Firstname</td>
<td>Lastname</td>
<td>Address</td>
<td>Contact</td>
<td>Email</td>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><button><a href='library-delete.php?id=$id/'>Delete</a></button></td>
</tr>
<?php } ?>
</table>
And this is the code for where the deleting happens.
<?php
$con = new PDO('mysql:host=localhost; dbname=library_member', $username = 'root', $password = '');
$q = "DELETE FROM user_list WHERE id = :id";
$stmt = $con->prepare($q);
$stmt->bindParam( ':id' , $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
echo "<script type='text/javascript'>
alert('Information have been deleted.');
window.location='library-showlist.php';
</script>";
?>
It would just directly send me on the prompt part. None of my input data is deleted.
Try to change your code like this:
library-delete.php?id=<?php echo $row['id']?>
View image: http://i.stack.imgur.com/yxE4u.jpg
Although this is in PHP, I'm trying to (with HTML) get two words (which use the same class) to push next to each other without calling the class seperately or using the two words in the same if/if else function. I'm sure it's just a simple HTML element.
<?php
if (something){
echo "<p class='message'>hello</p>";
}
if (somethingelse){
echo "<p class='message'>world</p>";
}
?>
As the comments aren't what I'm after, I'll show what I'm using:
<?php
if (!empty($message)){
echo "<p class='message'>" . $message . "</p>";
}
if (!empty($errors)){
echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
One statement or both at the same time may prove true. Hopefully you can see why I can't wrap it all around an HTML tag.
i think you want to do this
<p class="messasge"> <?php echo $message; ?> </p>
RE-FIXED according to updated specifications :
<?php
if (!empty($message)){
echo "<p class='message'>" . $message;
}
if (!empty($errors)){
if (empty($message) echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
}
if ((!empty($message))||(!empty($errors))) echo "</p>";
?>
if JUST message is true it'll print <p class='message'>MESSAGE</p>
if JUST errors is true, it'll print <p class='message'>ERRORS</p>
if BOTH are true, it'll print <p class='message'>MESSAGEERRORS</p>
I think you probably want some variant on the following code:
<?php
$output = '';
if (something)
$output .= 'hello';
if (somethingelse)
$output .= 'world';
if ($output)
echo "<p class='message'>$output</p>";
?>
For your example, you could replace all of the echos in the first two conditions with appending to $message, then only output if there is something to output. There are numerous options but I think this is the most flexible / elegant.
In your case it can be simplified to,
<?php
$output = $message;
if (!empty($errors))
foreach ($errors as $error)
$output .= " - $error<br />";
if (!empty($output))
echo "<p class='message'>$output</p>";
?>
Maybe the dl list is useful
<?php
if (!empty($message)){
echo "<dl class='message'><dt>" . $message . "</dt>";
}
if (!empty($errors)){
foreach ($errors as $error){
echo "<dd> - " . $error . "</dd>";
}
echo "</dl>";
}
?>
<?php
$message = "";
if (something) $message = "hello";
if (somethingelse) $message += " world";
echo "<p class='message'>".$message."</p>";
?>