473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem displaying one column of one row from a database with PHP.

I have a small script with PHP that queries a MySQL database to pull
out one row, where I want to be able to access each of the columns
separately. I have tried several different variations and am able to
get the entire row to print, but when I attempt to access the
individual columns I get an error. Here is what I have so far:

if (isset($_POST['memberNo'])):
$link = mysql_connect('...','...','...');
mysql_select_db("...");

//Perform a test query
$query = "SELECT * FROM users WHERE 'id' = " . $_POST['memberNo'];
$result = mysql_query($query) or die(mysql_error());

$line = mysql_fetch_assoc($result);
print $line;

//Close connection
mysql_close($link);

The point of this code is to retrieve the user information to validate
the login information sent in the previous form. I do not get an
error with this code, but it also does not print anything. I know
there is an entry in the database that matches data sent in the
$_POST['memberNo'] variable, it is the only entry in the database.

Changing the 'print $line;' to 'print $line["id"];' does not display
anyting either.

Does anyone know where I am making the mistake? There should only be
one row returned and I want to be able to access the columns in that
row as an array.

Thanks for any information,

Wayne
Jul 17 '05 #1
2 3475

On 30-Oct-2003, wa***@mishre.com (Wayne Pierce) wrote:
I have a small script with PHP that queries a MySQL database to pull
out one row, where I want to be able to access each of the columns
separately. I have tried several different variations and am able to
get the entire row to print, but when I attempt to access the
individual columns I get an error. Here is what I have so far:

if (isset($_POST['memberNo'])):
$link = mysql_connect('...','...','...');
mysql_select_db("...");

//Perform a test query
$query = "SELECT * FROM users WHERE 'id' = " . $_POST['memberNo'];
$result = mysql_query($query) or die(mysql_error());

$line = mysql_fetch_assoc($result);
print $line;

//Close connection
mysql_close($link);

The point of this code is to retrieve the user information to validate
the login information sent in the previous form. I do not get an
error with this code, but it also does not print anything. I know
there is an entry in the database that matches data sent in the
$_POST['memberNo'] variable, it is the only entry in the database.

Changing the 'print $line;' to 'print $line["id"];' does not display
anyting either.

Does anyone know where I am making the mistake? There should only be
one row returned and I want to be able to access the columns in that
row as an array.


I'm surprised your code doesn't die with on the query. You need to enclose
the field name id in back ticks not apostrophies.

You should be testing $line to see if it's null indicating no row was
returned.

print $line['id']; should work if a row was actually found.

If memberNo is not all digits and id is not defined as an int, you need to
enclose the value in apostrophies (e.g. '$_POST[memberNo]')

It's really a bad idea to insert user supplied data into a query without at
least addslashes().
--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2
On 30 Oct 2003 09:01:04 -0800, wa***@mishre.com (Wayne Pierce) wrote:
I have a small script with PHP that queries a MySQL database to pull
out one row, where I want to be able to access each of the columns
separately. I have tried several different variations and am able to
get the entire row to print, but when I attempt to access the
individual columns I get an error. Here is what I have so far:

if (isset($_POST['memberNo'])):
$link = mysql_connect('...','...','...');
Never ignore the return value of mysql_query (or any other MySQL function), as
queries can fail, returning 'false'. The function mysql_error() will tell you
why.

For debugging, you can use something like:

$result = mysql_query($query)
or die("Query failed: $query<br />Error: ".mysql_error()."<br />");

For a production system, you should never display raw error messages to the end
user, and should use some sort of graceful error handling to inform the user
that the function is currently unavailable.
mysql_select_db("...");
As above.
//Perform a test query
$query = "SELECT * FROM users WHERE 'id' = " . $_POST['memberNo'];
That will return no rows. You're comparing the string 'id' with what's in
$_POST['memberNo']. If you want to compare with the id column, leave it
unquoted.

Also you've got a security problem, as you're open to SQL injection attacks
(unless magic_quotes_gpc is on, which is generally more trouble than it's
worth).

Use addslashes() on the data; for example:

$query = sprintf("SELECT * FROM users WHERE id = '%s'",
addslashes($_POST['memberNo']);
$result = mysql_query($query) or die(mysql_error());
OK - error checking here!
$line = mysql_fetch_assoc($result);
print $line;

The point of this code is to retrieve the user information to validate
the login information sent in the previous form. I do not get an
error with this code, but it also does not print anything. I know
there is an entry in the database that matches data sent in the
$_POST['memberNo'] variable, it is the only entry in the database.

Changing the 'print $line;' to 'print $line["id"];' does not display
anyting either.

Does anyone know where I am making the mistake? There should only be
one row returned and I want to be able to access the columns in that
row as an array.


Your query matched no rows, so you get Boolean false back. When you try and
print that, you get nothing.

If you had got a row, you'd get Array(), since you're trying to print an
array. But you got the right idea to try $line['id'].

if ($line = mysql_fetch_assoc($result))
print $line['id']
else
print 'No rows returned.';

If all you want is the id column, then only select that column, rather than
select * - only select what you're going to use.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: euang | last post by:
Hi, I have been using access 2000 for two years on WINDOWS NT to display dynamic aweb page using ASP My ISP has now changed to Windows 2003, and I am having major problems displaying...
6
by: Omar | last post by:
When I try to databind my comboBox (specifically field "emplcode") to a filled dataset , the contents of the comboBox displays a bunch of "System.Data.DataRowView". I assume the amount of times...
4
by: Ankit Aneja | last post by:
code of my aspx page <asp:RadioButtonList id="RadioButtonList1" runat="server"></asp:RadioButtonList> now i want to bind data from database Dim rsComm As SqlCommand Dim rsReader As...
2
by: Wayne | last post by:
This is a copy of a message I previously posted in a Microsoft Access Newsgroup, but it was suggested to me that my problem is ASP related and not Access, and hence I'm posting in this newsgroup now...
6
by: yoshitha | last post by:
hi db : sql server 2000 lan : C#.net(ASp.Net) in my database table there are 2 fileds of data type datatime. in field 1 i'm storing date in field 2 i'm storing time.
1
by: speralta | last post by:
For some reason the text in h2 tag is displaying as white in IE. http://www.salperalta.com/ <td class="sidebar" id="sidebar-right"> <div class="block block-listing" id="block-listing-0">...
2
by: biganthony via AccessMonster.com | last post by:
Hi, I decided to install Office 2003 Service Pack 3 on my home computer to test (in full knowledge that there may be some issues with it). After installation, I have noticed that with a small...
4
by: cheltboy | last post by:
At one of my client sites we noticed a few weeks ago that Listboxes which previously showed "Yes" or "No" are now showing "-1" or "0". The underlying tables and queries are displaying True/False...
2
by: BobLewiston | last post by:
Most databases have multiple users. For these databases it is a good idea to auto-increment the identity column. I understand that the identity column is not incremented until the newly-created...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.