vomitbomb Posted May 12, 2008 Share Posted May 12, 2008 The code below can query the database and get a result only as a row and only selects one row, what I want is to be able to search for a particular name in the fname row. Say there was an entry in fname called John, I'd like to be able to search for it in the row 'fname'. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php // Make a MySQL Connection mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("example") or die(mysql_error()); $search = "fname"; // Retrieve all the data from the "example" table $result = mysql_query("SELECT $search FROM example") or die(mysql_error()); // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry echo "Name: ".$row['fname']; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/105216-help-querying-the-database/ Share on other sites More sharing options...
beboo002 Posted May 12, 2008 Share Posted May 12, 2008 if u want fetch only john SELECT * FROM example where fname='john' if u pass value through form then SELECT * FROM example where fname='".$_POST[formfieldname]."' Quote Link to comment https://forums.phpfreaks.com/topic/105216-help-querying-the-database/#findComment-538748 Share on other sites More sharing options...
vomitbomb Posted May 12, 2008 Author Share Posted May 12, 2008 excellent thanks mate Quote Link to comment https://forums.phpfreaks.com/topic/105216-help-querying-the-database/#findComment-538751 Share on other sites More sharing options...
smc Posted May 12, 2008 Share Posted May 12, 2008 Assuming you want to search for the name in fname and fname is a column in your database... <?php //Make your MySQL connection $search = "fname"; $sql = mysql_query( "SELECT * FROM `example` WHERE fname = '$search'" ) or die( mysql_error() ); //Make sure you're doing a mysql_fetch_row if you're only getting one row from the Database $row = $mysql_fetch_row( $sql ); //OR - You probably will be getting more than one entry, many first names are similar.. so... instead we do: $myResults = mysql_fetch_array( $sql ); //To list results: print_r( $myResults ); //To print the one result from $row echo( "Name: " . $row['fname'] ); ?> Addendum on beboo002's post. I HIGHLY recommend you DO NOT use his type of code. That will query what is submitted in the form field and can leave your database open to serious SQL injection and hacking. Quote Link to comment https://forums.phpfreaks.com/topic/105216-help-querying-the-database/#findComment-538752 Share on other sites More sharing options...
Barand Posted May 12, 2008 Share Posted May 12, 2008 Assuming you want to search for the name in fname and fname is a column in your database... <?php //Make sure you're doing a mysql_fetch_row if you're only getting one row from the Database $row = $mysql_fetch_row( $sql ); //OR - You probably will be getting more than one entry, many first names are similar.. so... instead we do: $myResults = mysql_fetch_array( $sql ); ?> I highly recommend you read the manual regarding mysql_fetch_array(). All the mysql_fetch_xxx() functions retrieve fields from a single row. The only differences are in the way the data is stored when fetched. Quote Link to comment https://forums.phpfreaks.com/topic/105216-help-querying-the-database/#findComment-538886 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.