Jump to content

Calling column PHP MySQL


beaux1

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 );

Link to comment
Share on other sites

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
?>


?>

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

$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>";

Link to comment
Share on other sites

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 );
?>

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.