472,995 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 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 3449

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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.