Click here to Skip to main content
15,848,451 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a mySQL database and a form page in php. I need the best way to retrieve data from the database based on user input in the form. Now, the form will limit user input to an integer they will be given before hand. The integer will be in the database as an auto increment number that will be called 'rep ID'. The user will input the 'rep ID' to display that rep's information.

I have tried several different approaches, yet have not been able to get a result. This has been ongoing for about a month, with me working on it daily. As of right now, I have the form working the way I want it, the database created, the submission page for collecting the data is perfect... the only problem is I can't manage to pull the data needed based on the user inputting a specific ID.

The code submitted is just the last I have tried. It however, did not work as the several before. Any suggestions would be greatly appreciated, and I just really need a direction to head in. I'm totally lost with this, what I thought was going to be relatively easy, and am growing frustrated. Thank you in advance.

What I have tried:

PHP
<?php 
 $host = 'myhost';
 $user = 'myuser';
 $pass = 'mypassword';
 $db = 'mydatabase';

	$con = mysqli_connect("$host", "$username", "$password") or die("Error connecting to database: ".mysqli_error());
	mysqli_select_db($con, "$db") or die(mysqli_error());

    $query = $_GET['query']; 

	$sql = "SELECT * FROM 'repData' WHERE 'repID' = $query";
	$result = $con->query($sql);

	if ($result->num_rows > 0) {
		echo "";

		while($row = $result->fetch_assoc()) {
			echo "";
		}
		echo "<table><tbody><tr><th>ID</th><th>Name</th></tr><tr><td>".$row["repID"]."</td><td>".$row["repName"]." ".$row["repBio"]."</td></tr></tbody></table>";
	} else {
		echo "<br><h1 align="center">Sorry that ID could not be found in our database.<br>
		Verify the number and try again.</h1>";
	}

	$con->close();
	?>
Posted
Updated 5-Apr-17 5:18am
v2
Comments
W Balboos, GHB 3-Apr-17 12:09pm    
Here's a hint for you:
 $array = mysqli_fetch_row ($result); 


What is $result?

If you copied this as a sample from somewhere you need to actually use values that match your code - not those of the sample.

1 solution

For version 2 of the question:
PHP
while($row = $result->fetch_assoc()) {
    echo "";
}
echo "<table><tbody><tr><th>ID</th><th>Name</th></tr><tr><td>".$row["repID"]."</td><td>".$row["repName"]." ".$row["repBio"]."</td></tr></tbody></table>";

Print the table header first, then the row contents within the while loop and finally the closing table tags:
PHP
echo "<table><tbody><tr><th>ID</th><th>Name</th></tr>";
while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["repID"]."</td><td>".$row["repName"]." ".$row["repBio"]."</td></tr>";
}
echo "</tbody></table>";

Because you are filtering for a single ID, printing the headers outside the while loop here is not really necessary. But you must access $row inside the loop because it is out of scope when using it after the while loop. Even when $row is declared outside the loop if would not work because it is NULL when the loop has terminated.
 
Share this answer
 
Comments
Jawanaut 5-Apr-17 11:17am    
That does make sense about showing the row contents within the loop. Not sure why I didn't think of that. However, I'm still pulling my 'else' statement (Sorry that ID...) when running the query. This is weird, mostly cause there is only one ID in the database (as of now).
Jochen Arndt 5-Apr-17 11:26am    
Then try for testing the query
SELECT * FROM 'repData'

You may also differentiate bewteen !$result (query failed) and $result->num_rows == 0 (no matching recordsets found).
Jawanaut 5-Apr-17 11:49am    
AWESOME!!! You rock! It works and my month + of struggle is over. Thanks for the help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900