Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting a select query result into table/array 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
I am trying to step towards searching a database for a matched
value inputted by a user, a form of log-in.

I think because no errors come up, I am connected to the database with the following code. Can someone show or point me in the direction to get the single column database data onto the page, and into a variable array to check aginst a user input. Many thanks

<html>
<table border="0" CELLSPACING="0" CELLPADDING="10" align="center">

<?php

//db connect
$db = @mysql_connect("localhost", "user", "password");
if( ! ($db = @mysql_connect("localhost", "user", "password")) ) {
} else {
mysql_select_db("database",$db) or die("Select DB Error: ".mysql_error());
}
$result=mysql_query ("select * from table");

CAN SOMEONE FILL THIS SLOT? THANKS

?>

</table>
</html>
 
Say, if the column name is col1

Code:
$qry =mysql_query ("select * from table");
while ( $result = mysql_fetch_array($qry) ) {
   $colvalue = $result["col1"];
   //here you can use this $colvalue to assign to array or compare it with the user input. 
   .....
}

-------Information is not knowledge-------
 
Many thanks Surtam. I tried the code below, trying to establish values of $colvalue, however it only prints the leading character of a field? Whats wrong. Also do I go back to HTML to compare the string (presume its one) $colvalue to say an input B1. Thanks

<html>
<table border="0" CELLSPACING="0" CELLPADDING="10" align="center">


<body>
<?php

$db = mysql_connect("localhost", "User", "password");
mysql_select_db("Database",$db) or die("Select DB Error: ".mysql_error());

$qry =mysql_query ("select * from Validnames");

while ( $result = mysql_fetch_array($qry) ) {


$colvalue = $result["Names"];

//here you can use this $colvalue to assign to array or compare it with the user input.

printf("<tr><td>%s</td>",$colvalue["Names"]);

printf("<tr><td>%s</td>",$result["Names"]);


}


?>


</table>
</html>
 
its not
Code:
printf("<tr><td>%s</td>",$colvalue["Names"]);

Change it to

Code:
printf("<tr><td>%s</td>",$colvalue);



-------Information is not knowledge-------
 
Surtam, many thanks again. Have a star. So now I have a variable $colvalue, which presumeably I can compare to an inputted name/string by the user. Comming from VB, the If/Else/Then part of PHP is a complete mystery. I would want it to recyle back to being inputted again if it were wrong, with a message, or open a new page. Hell knows how long thats going to take me to work out if just putting something in a variabl has taken so long. I have looked at the PHP Manual site, and pasted in a few attempts, but it must be old code in parts as it gets rejected. Do you know any shortcut links I might find to accomplish the above task? Many thanks again.
 
If you are going to compare just one name against the values stored in the database, you can very well do it with the sql query, I am not sure, why you want to compare each and evey value.

There are many ways to accomplish the task, but this is one, which is very simple to implement. For redirection, take a look at the header function.

Code:
<html>
<table border="0" CELLSPACING="0" CELLPADDING="10" align="center">
<body>
<?php
//Presume that you are getting the name from the previous form and you call it as $username.
// $username = "smith";

$db = mysql_connect("localhost", "User", "password");
mysql_select_db("Database",$db) or die("Select DB Error: ".mysql_error());

$sql = "select * from Validnames where Names = '" . $username . "'";
if ( $qry =mysql_query($sql) ) {
	$numrows = mysql_num_rows($qry);
	if ( $numrows > 0 ) {
		printf("<tr><td>Valid Name %s</td>",$username);
	}
	else {
		printf("<tr><td>Not a Valid Name %s</td>",$colvalue);
		$url = "redirect_page.php";
		echo "<script>location.href='$url'</script>";
	}

}

?>
</table>
</html>

-------Information is not knowledge-------
 
Moved along another inch! I now have the code below giving me the If/Else action.
Can I ask 3 questions please:

1.As the page name was given an extension of php
in order for the server to see/deal with it, I have lost the ability to go into page design in Frontpage. Do I have to change the extension back each time?

2. How do I change the hardcoded name of Bill into a user entered (textbox) entry.

3. In the If and Else bit, how do I instigate an action to open another page.

Sorry to ask a lot, but hours of searching proving a disaster. Thanks

-------------------------------------------------------


$qry =mysql_query ("select * from Validnames");

while ( $result = mysql_fetch_array($qry) ) {

//use $colvalue and assign to array or compare it with the user input.

$colvalue = $result["Names"];

if ( $colvalue == "Bill" )
{echo( "Welcome," );}
else
{echo( "" );}

}
?>
 
Thanks Surtam, I must have been putting together my posting while yours was going through the system. I think you have answered one of my questions - in you presume I got the users name from a previous form. I thought the page could act as a user page (ie normal web page) as well as handling PHP, is this not the case? If my user entry is on a previous page, then how do you pass the variable with the user name over / and how in the previous page would you call the PHP page to do its bit. This really is a bumpy ride. Appreciate your help, regards.
 
Surtam,

I just tried your code and that answers a lot. Seeing the redirect bits has now made sense. I think I just need to know how you pass variables around pages and I should have enough to keep me moving. Really did appreciate your help. Regards
 
Thanks Surtam, wish the forum would allow more stars. Really been helpful pointing the way. Best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top