Page 1 of 1

Database

Posted: Sun May 19, 2002 10:29 pm
by andy18
Hello guys,new to PHP and trying to create a database but no luck..below are the sample script I created :

Code: Select all

<?php
	require("password.php");
	
	if($Category == "windows")&#123;
		retrieve($Windows);
	&#125;
	else if($Category == "linux")&#123;
		retrieve($Linux);
	&#125;
	else&#123;
		retrieve($General);
	&#125;
	
	function retrieve($Table)&#123;
		
		$Tablename = $Table;
		mysql_connect(localhost,$User,$Password);
		@mysql_select_db($DBName) or die( "Unable to select database");
		$query="SELECT * FROM $Tablename";
		$result=mysql_query($query);

		$num=mysql_numrows($result); 
	
		mysql_close();

		print("<b><center>Database Output</center></b><br><br>");

		$i=0;
		while ($i<$num)&#123;

		$question=mysql_result($result,$i,"question");
		$answer=mysql_result($result,$i,"answer");
	
		print("<TR> ALIGN=CENTER VALIGN=TOP>\n");
		print("<TD ALIGN=CENTER VALIGN=TOP>$question</TD>\n");
		print("<TD ALIGN=CENTER VALIGN=TOP>$answer</TD>\n");
		print("</TR>\n");
		++$i;
	
		&#125;
	&#125;
?>
This is the html script that will invoke this phpp script :

Code: Select all

<body bgcolor="#FFFFFF" text="#000000">
<form method="post" action="fetchdata2.php">
<select name="Category">
	<option value=windows>Windows</option>
	<option value=linux>Linux</option>
	<option value=general>General</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
** the "password.php includes all the username,password,databasename($DBName),and the host.

When I make the selection from the html script,the php files will execute and show me unable to select database error.Can you guys assist me what I have done wrong in the scripts?

thanks 8O

ok, here we go...

Posted: Mon May 20, 2002 9:41 am
by cwcollins
first...
get rid of this

Code: Select all

if($Category == "windows")&#123; 
retrieve($Windows); 
&#125; 
else if($Category == "linux")&#123; 
retrieve($Linux); 
&#125; 
else&#123; 
retrieve($General); 
&#125;
and replace it with this

Code: Select all

retrieve($Category);
that block is your main problem... you are passing the retrieve function variables that do not exist. next, and this is just a recommendation, while you are working, remove teh '@' in front of

Code: Select all

@mysql_select_db($DBName) or die( "Unable to select database");
having this there prevents the regular error from being displayed. these errors often have information that can lead you to a solution.

hope this helps....

c.w.collins

Posted: Mon May 20, 2002 7:53 pm
by andy18
Dear Collins,

thanks for the reply ,I have try out the code as you had written.But no luck, the code still cannot work.Below are the modified code:

retrieve($Category);

function retrieve($Table){
mysql_connect(localhost,$User,$Password);
mysql_select_db($DBName) or die( "Unable to select database");
$query="SELECT * FROM $Table";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$question=mysql_result($result,$i,"question");
$answer=mysql_result($result,$i,"answer");

echo "<b>Question : $question</b><br>Answer : $answer<br>";

++$i;
}
}

I had removed the if..else condition but I still get the unable to select database error even though I had also remove the "@".Is there still anything that I missing?


Thanks...

Posted: Mon May 20, 2002 8:57 pm
by andy18
Hello Collin,

Sorry, I had solved the previous problem but now exist another problems that is I got this error :
Supplied argument is not a valid MySQL result resource in line 35

which is the following highlighted code :

<?php

/*$username="username";
$password="password";
$database="your_database";*/

retrieve($Category);

function retrieve($Table){
include("password.php");
if($Table == "linux"){
$Tablename = Linux;
}
else if($Table == "windows"){
$Tablename = Windows;
}
else{
$Tablename = General;
}
$Link = mysql_connect(localhost,$User,$Password);
mysql_select_db($DBName,$Link) or die( "Unable to select database");
$result=mysql_query("SELECT * FROM $Table",$Link);

$num = mysql_num_rows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$question=mysql_result($result,$i,"question");
$answer=mysql_result($result,$i,"answer");

echo "<b>Question : $question</b><br>Answer : $answer<br>";

++$i;
}
}

?>

I just don't know what is wrong with the syntax there as I follow what is in the php help file shows.

Thanks and once again sorry for being so troublesome.

Posted: Tue May 21, 2002 3:23 am
by ILoveJackDaniels
The problem is not with the num_rows line, as that is fine, but with the line above. Try replacing the two lines above with the code below and see if that makes any difference:

Code: Select all

mysql_select_db($DBName) or die( "Unable to select database"); 
$result=mysql_query("SELECT * FROM $Table");

Posted: Tue May 21, 2002 7:59 am
by cwcollins
ok, let's try this... change

Code: Select all

retrieve($Category);
to:

Code: Select all

if(!empty($Category)){retrieve($Category);}
if this removes the error, $Category is probably not being passed correctly. you could also try echoing $Category right into the page, to make sure it's getting through as you expect it. change the method to get, and take a close look at the address line after you submit. of course, change all of these things back after you get it working.

-c.w.collins

Posted: Tue May 21, 2002 8:11 am
by jason
andy18:

After you do your mysql_query() function do:

Code: Select all

echo mysql_error()
and see what it prints out.

Posted: Tue May 21, 2002 8:19 am
by cwcollins
ya know, jason, i'm beginning to think you might be better than me at this. :wink:

-c.w.collins

Posted: Tue May 21, 2002 1:40 pm
by sam
mysql_connect(localhost,$User,$Password);

You are using variables as the username and password but haven't set these anyware in the function so your not able to connect to the database.

Another thing is that the way collins told you to do the $catagory thing might not be the best way to do it. If you have sensitave data that is not a very secure plan.
try:

Code: Select all

if($Category == "windows" || $Category == "linux")&#123; 
      retrieve($Category); 
   &#125;
   else&#123; 
      retrieve("general"); 
   &#125;
Cheers sam