Solved

php query

Posted on 2016-07-30
6
33 Views
Last Modified: 2016-07-30
$sql = "SELECT id, file_name FROM files";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
	// output data of each row
	echo '<table width="" align="center">';
	$i = 0;
	while ($row = $result->fetch_assoc()) {
		if (0 === ($i % 4)) {
			echo '<tr valign="top">';
		}
		echo "<td class='tableclass'>ID: " . $row["id"] . "   Name: " . $row["file_name"] . "</td>";
		echo "<td class='imagefont smooth'>&nbsp;   Image: <a href='" . $largeimage . $row["id"] . "'><img src=" . $image . $row["file_name"] . " width='50px' height='50px' /></a><a href='" . $deleteimage . $row["id"] . "'>$link Delete Image from database</a>$linkend&nbsp;</td>";
		if (3 === ($i % 4)) {
			echo "</tr>";
		}
		$i++;
	}
} else {
	echo "Current Images in Database:";
}
$conn->close();
echo '</table>';

?>

</br>

<?php
$sql2 = "SELECT id, f_name FROM documents";
$result2 = $conn->query($sql2);

if ($result2->num_rows > 0) {
	// output data of each row
	echo '<table width="" align="center">';
	$i = 0;
	while ($row = $result2->fetch_assoc()) {
		if (0 === ($i % 4)) {
			echo '<tr valign="top">';
		}
		echo "<td class='tableclass'>ID: " . $row["id"] . "   Name: " . $row["f_name"] . "</td>";
		echo "<td class='imagefont smooth'>&nbsp;   Image: <a href='" . $largeimage . $row["id"] . "'><img src=" . $image . $row["f_name"] . " width='50px' height='50px' /></a><a href='" . $deleteimage . $row["id"] . "'>$link Delete Image from database</a>$linkend&nbsp;</td>";
		if (3 === ($i % 4)) {
			echo "</tr>";
		}
		$i++;
	}
} else {
	echo "Current Images in Database:";
}
$conn->close();
echo '</table>';

?>

Open in new window


In the above, the first query works perfectly but the second one does not return any results
The records are in the database and the field names are correct
0
Comment
Question by:doctorbill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 7

Accepted Solution

by:
Gauthier earned 500 total points
ID: 41735757
I'm not sure you can reuse the $conn once it is closed...
so remove the first $conn->close()
1
 

Author Comment

by:doctorbill
ID: 41735759
I tried changing the second query to $conn2 but still no luck
0
 

Author Comment

by:doctorbill
ID: 41735762
Sorry - managed to sort it
I know why the $conn2 did not work
I just realised that my dbconnect.php connections reference file did not have a reference to $conn2

All good now
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:doctorbill
ID: 41735763
Your solution also works
0
 

Author Closing Comment

by:doctorbill
ID: 41735766
Solution
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41735770
The question could be stupid, but have you open a $conn2 connection to the database? Usually one includes a script with the db connection in every script which have to access the db, so you should hve only $conn available, not an $conn2...
What happens removing the first $conn->close() as Gauthier suggested above?
1

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question