Retrieving data from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Retrieving data from database

Post by phpnoobyyy »

Hi guys

I have a database with fields 'a', 'b', 'c' and 'd'. I want to retrieve info from the database and display it using a form where the info in fields a, b, c or d equals the info selected in the form. I have the following php code and form to achieve this:

PHP code:

Code: Select all

if ($_POST['xyz']) {
        $x = trim($_POST['xyz']);
    } else {
        $errors[] = '<small id="error">Error</small>';
    }
        
        
// Make the query:
$q = "SELECT CONCAT(first_name, ' ', last_name) AS name, (email) FROM users 
WHERE a OR b OR c OR d = '$x'";        
$r = @mysqli_query ($dbc, $q); // Run the query.
 
if ($r) { // If it ran OK, display the records.
 
    // Table header.
echo     'display data';
HTML form

Code: Select all

<select name="xyz" class="input"/>
    <option value="something1">XXX</option>   
    <option value="something2">XXX</option>
        </select>
My problem is that when i execute the form all records in the database are being retrieved regardless of whether the database field has the required data.

I've looked into what may be going wrong without success.

Any heads up on what may be going wrong would be greatly appreciated.

Many thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Retrieving data from database

Post by AbraCadaver »

Query looks bad. Try this:

Code: Select all

$q = "SELECT CONCAT(first_name, ' ', last_name) AS name, (email) FROM users WHERE a = '$x' OR b = '$x' OR c = '$x' OR d = '$x'";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Re: Retrieving data from database

Post by phpnoobyyy »

Hi

I actually just tried that literally before coming back on the forum to check if anyone had replied and was just going to post that i had solved the problem!

Thanks for confirming the solution.
Post Reply