Page 1 of 1

taking from the database

Posted: Sat Jun 14, 2003 3:10 pm
by stc7outlaw
I'm trying to get column info from a database. There is only 1 row per table and all i want to do is put it on an html page. Here is my code so far:

Code: Select all

<form method="post" action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>">
<input type="text" name="projectid">
</form>
<br>
<?php
   
   if (!empty($_POST&#1111;'projectid']) ) &#123;

   $projectid = $_POST&#1111;'projectid'];
   $storenumber = "SELECT storenumber FROM $projectid WHERE storenumber > 0";
    
   $user = 'blahuser';
   $pass = 'blahpass';
   $db = 'projectsdb';
   
   
   $link = mysql_connect('localhost', $user, $pass) or die('<p>Could not connect to MySQL server.</p>'); 
   mysql_select_db($db) or die('<p>Could not select '.$db.' database');
   &#125;
     
 
?>
Store#:<?php echo $storenumber; ?>
After Store#: all it says shows is

Code: Select all

SELECT storenumber FROM $projectid WHERE storenumber > 0
. Shouldnt it show the info in my database?
Is there something wrong with what I am doing, or is there an easier way or more complex way?
Thank you,

Outlaw

Re: taking from the database

Posted: Sat Jun 14, 2003 4:11 pm
by delorian
Yes, you are not executing your query.

You must use mysql_query() function.

Code: Select all

<form method="post" action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>">
<input type="text" name="projectid">
</form>
<br>
<?php
   
   if (!empty($_POST&#1111;'projectid']) ) &#123;

   $projectid = $_POST&#1111;'projectid'];
   $storenumber = "SELECT storenumber FROM $projectid WHERE storenumber > 0";
    
   $user = 'blahuser';
   $pass = 'blahpass';
   $db = 'projectsdb';
   
   
   $link = mysql_connect('localhost', $user, $pass) or die('<p>Could not connect to MySQL server.</p>'); 
   mysql_select_db($db) or die('<p>Could not select '.$db.' database');


// execute a query $storenumber on database and return a resource to $result
   $result = mysql_query($storenumber);

// creating an array which has the information returned by mysql_query
   $row = mysql_fetch_array($result);
   &#125;
     
 
?>
Store#:<?php echo $row&#1111;0]; ?>
It should do the trick.

If you're using a mysql database and want to get information from it you have to stick to following scheme:

Open connection: mysql_connect()
Select a database: mysql_select_db()
Execute a query: mysql_query()
Get the results from query: many types of functions
Close connection: mysql_close()

Read the PHP manual http://www.php.net/manual/en/ref.mysql.php