Hello. I am new to PHP + MySQL. I appreciate if any friends at this forum can help me about a problem at retreiving data from MySQL.
My environment:
Windows 2000 Pro SP4
Zend Studio Server 3.0.0
PHP 4.3.2
MySQL 4.0.20a
MySQL includes:
database: database_user
table: table_password
There are two columns inside table_password:
user_name
user_password
I want to query the corresponding user_name in table_password by submitting user_password.
My codes are:
<?php
//Connect to the databse with the following 3 pieces of data defined as constants.
define ("DB_HOST", "localhost");
define ("DB_USER", "root");
define ("DB_PASSWORD", "password");
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ("Fails to connect to MySQL.");
//Select the database.
$database = "database_user";
$dbs = mysql_select_db ($database, $dbc) or die ("Failure to select database ($database).");
//Return the corresponding user name after looking up the code in the table_password.
$sql = 'SELECT user_name
'
. ' FROM table_password
'
. ' WHERE 1 AND user_password
'
. ' LIKE \'$code\' LIMIT 0, 30';
$query_psq_user_id = mysql_query ("$sql") or die ("Failure to query table (password).");
?>
I receive Failure to query table (password) each time I execute the codes.
However if I remove . ' WHERE 1 AND user_password
' . ' LIKE \'$code\' LIMIT 0, 30' from $msql, the codes executes just fine.
How should I correct the codes?
In fact, my ultimate purpose is to use the user_name to query some other data in another table. Is there a shortcut other than to retreive the user_name first and to query by user_name later?
Thanks in advance.