954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying mysql table's fieldname in php

This is a very newbie post. I think I'm just missing something.
Anyway.
I'm trying to display the contents of one column's field (not column name) specified by the contents of another filed in the same row.

Here I show you my table and what I need:

Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 71
Server version: 5.5.8-log MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use interntv;
Database changed
mysql> SELECT * FROM members2;
+----+-------+-------+----------------+-------------------+
| id | user  | pass  | name           | email             |
+----+-------+-------+----------------+-------------------+
| 21 | ADMIN | 7aeb0 | Jakob Neumaier | Jakob@gmail.com   |
| 54 | user1 | da8e0 | Adam Portman   | user1@hotmail.com |
| 13 | user2 | 0f1d3 | Peter Lupica   | user2@live.com    |
+----+-------+-------+----------------+-------------------+
3 rows in set (0.00 sec)

mysql> SELECT name FROM members2 WHERE user = 'admin';
+----------------+
| name           |
+----------------+
| Jakob Neumaier |
+----------------+
1 row in set (0.00 sec)

mysql>


I have tried:

<?php

	define('DB_NAME','interntv');
	define('DB_USER','root');
	define('DB_PASSWORD',''); 
	define('DB_HOST','localhost');

	$mysqllink = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

	if (!$mysqllink) {
		die('Could not connect: ' . mysql_error());
	}

	$db_selected = mysql_select_db(DB_NAME, $mysqllink);

	if (!$db_selected) {
		die ('Can\'t use ' . DB_NAME . ': ' . mysql_error());
	}

	echo 'Connected successfully';

	echo mysql_query("SELECT name FROM members2 WHERE user = 'admin'");
	
?>

The only output I get is:

Connected successfully

Resource id #4

Can someone help me?

Jaklins
Newbie Poster
21 posts since May 2011
Reputation Points: 10
Solved Threads: 1
 

http://www.w3schools.com/php/php_mysql_select.asp

echo mysql_query("SELECT name FROM members2 WHERE user = 'admin'");


to

$result = mysql_query("SELECT name FROM members2 WHERE user = 'admin'");
while($row = mysql_fetch_array($result))
  {
  echo $row['user'] . " " . $row['pass'];
  echo "";
  }

mysql_close($con);
?>


ect

G&G Designing
Junior Poster in Training
90 posts since Aug 2011
Reputation Points: 17
Solved Threads: 11
 

http://www.w3schools.com/php/php_mysql_select.asp

echo mysql_query("SELECT name FROM members2 WHERE user = 'admin'");

to

$result = mysql_query("SELECT name FROM members2 WHERE user = 'admin'");
while($row = mysql_fetch_array($result))
  {
  echo $row['user'] . " " . $row['pass'];
  echo "";
  }

mysql_close($con);
?>

ect

This doesn't work.
I get these errors:

( ! ) Notice: Undefined index: user in C:\wamp\www\test.php on line 28
Call Stack
# Time Memory Function Location
1 0.0039 372456 {main}( ) ..\test.php:0

( ! ) Notice: Undefined index: pass in C:\wamp\www\test.php on line 28
Call Stack
# Time Memory Function Location
1 0.0039 372456 {main}( ) ..\test.php:0


( ! ) Notice: Undefined variable: con in C:\wamp\www\test.php on line 32
Call Stack
# Time Memory Function Location
1 0.0039 372456 {main}( ) ..\test.php:0

( ! ) Warning: mysql_close() expects parameter 1 to be resource, null given in C:\wamp\www\test.php on line 32
Call Stack
# Time Memory Function Location
1 0.0039 372456 {main}( ) ..\test.php:0
2 0.0286 380232 mysql_close ( ) ..\test.php:32

I erased two last ones by deleting $con.
Then I echoed $row inside the while and it only displayed:
Connected successfully

Array

Jaklins
Newbie Poster
21 posts since May 2011
Reputation Points: 10
Solved Threads: 1
 

http://www.w3schools.com/php/php_mysql_select.asp

echo mysql_query("SELECT name FROM members2 WHERE user = 'admin'");

to

$result = mysql_query("SELECT name FROM members2 WHERE user = 'admin'");
while($row = mysql_fetch_array($result))
  {
  echo $row['user'] . " " . $row['pass'];
  echo "";
  }

mysql_close($con);
?>

ect


2nd reply.
I've taken a closer look now.
You practically copied the code from the example and pasted my syntax into it.
It was almost working. I just had to erase the $con, and replace "name" with "*" in the mysql query. That little star was the key. That and fetching the array.
But you found the example, which was what I needed, so you get +1 to rep ;-)
Thanks.

Jaklins
Newbie Poster
21 posts since May 2011
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: