beaux1 Posted February 16, 2007 Share Posted February 16, 2007 Hey, This is what I want to do: In my SQL DB, I have a row called number, so each column has a number. I have a form, and in that form I enter a number and hit submit. The form finds the number entered, and echos all the information in the colimn with that number, so it would echo all the other rows in that column. I know how to do the form, but I just don't get what SQL/PHP code I would use to go about making this. Do you understand me or am I talking nonsense? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2007 Share Posted February 16, 2007 SELECT * FROM table WHERE col1 = '$col1'; Read some of the tutorials on here. Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 Sorry but, I don't really understand. Is col1 to me 'number', as that's the column? And also, how do I go about echoing the other rows from that column? So it could echo first name, last name..etc Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2007 Share Posted February 16, 2007 http://www.phpfreaks.com/tutorials/142/0.php Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 Thanks, I just read that there. But there's one thing I'm not so sure of: This is my code: <?php include 'config.php'; include 'opendb.php'; $number = $_POST['number']; $sql = "SELECT * FROM information WHERE number = '$number'"; $result = mysql_query($sql) or die(mysql_error()); include 'closedb.php'; ?> Where as I know how to display the column, I don't know how to display the OTHER columns on that line, if you get me? Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 16, 2007 Share Posted February 16, 2007 Do you want to display the rest of the info in that row? Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 Yep! Example: form = number person entered in the form. it finds the number in the DB. it echos the rest of the rows in the line. |number|first|last form.......bob..smith So it would echo the first and last name. Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 16, 2007 Share Posted February 16, 2007 When you do your mysql query call for the rest of the information in that row at the same time. $sql = "SELECT * FROM information `number`, `first`, `last`, WHERE number = '$number'"; $result = mysql_query($sql) or die(mysql_error()); include 'closedb.php'; while ($row = mysql_fetch_array($result)) { $list = ".$row["number"]." ; $list = ".$row["first"]."; $list = ".$row["last"]."; echo( $list ); Quote Link to comment Share on other sites More sharing options...
Clinger Posted February 16, 2007 Share Posted February 16, 2007 If you wanted to print out absolutely every value: <?php //Put all your database connection stuff here. $sql="SELECT * FROM `TABLENAME` WHERE `COLUMN` = 'CONDITION'"; if ($query=@mysql_query($sql)) { if (mysql_num_rows($query) > 0) { while ($req=mysql_fetch_array($query)) { $string=""; foreach ($req as $key=>$value) { $string.="{$req[$key]} = {$value},"; } //Do whatever you need with your string here. } } } //Close your database connection ?> ?> Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 <?php include 'config.php'; include 'opendb.php'; $number = $_POST['number']; $sql = "SELECT * FROM information `number`, `first`, `last` WHERE number = '$number'"; $result = mysql_query($sql) or die(mysql_error()); include 'closedb.php'; while ($row = mysql_fetch_array($result)) { $list = ".$row["number"]." ; $list = ".$row["first"]."; $list = ".$row["last"]."; echo( $list ); ?> And I get: Parse error: parse error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\number.php on line 10 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 16, 2007 Share Posted February 16, 2007 $sql = "SELECT * FROM information `number`, `first`, `last` WHERE number = '$number'"; Unless I'm totally missing something, that query is all kinds of wrong. $sql = "SELECT `number`, `first`, `last` FROM information WHERE number = '$number'"; Also, the fine point that you're missing here is that $row will be an associative array of the values returned by the DB. Each key in the array will correlate to one of your DB rows. In the while loop where $row is set, try this: echo "<pre style=\"text-align: left;\">" . print_r($row, true) . "</pre>"; Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 16, 2007 Share Posted February 16, 2007 That's how I used it on what I'm developing and it worked fine. Although I also used $list to create a table. Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 Thanks for all your help so far, but, this is my code, and I'm getting error: Parse error: parse error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\number.php on line 12 But I probably added it wrong or something: <?php include 'config.php'; include 'opendb.php'; $number = $_POST['number']; //Put all your database connection stuff here. $sql = "SELECT `number`, `first`, `last` FROM information WHERE number = '$number'"; $result = mysql_query($sql) or die(mysql_error()); include 'closedb.php'; while ($row = mysql_fetch_array($result)) echo "<pre style=\"text-align: left;\">" . print_r($row, true) . "</pre>" ; { $list = ".$row["number"]." ; $list = ".$row["first"]."; $list = ".$row["last"]."; echo( $list ); ?> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 16, 2007 Share Posted February 16, 2007 $list = ".$row["number"]." ; $list = ".$row["first"]."; $list = ".$row["last"]."; echo( $list ); Should be: $list = ".$row['number']." ; $list .= ".$row['first']."; $list .= ".$row['last']."; echo( $list ); Take a good look at the use of .= instead of = on the 2nd and 3rd lines. Also take a good look at my use of the single quotes within the double-quoted strings. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 16, 2007 Share Posted February 16, 2007 Also, WHERE number = '$number'"; If number is defined as INT, you're comparing it with a string by enclosing $number in single quotes. Use: WHERE number = $number"; Quote Link to comment 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.