473,670 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($qu ery) or die(mysql_error ());

$line = mysql_fetch_ass oc($result);
print $line;

//Close connection
mysql_close($li nk);

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 3490

On 30-Oct-2003, wa***@mishre.co m (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($qu ery) or die(mysql_error ());

$line = mysql_fetch_ass oc($result);
print $line;

//Close connection
mysql_close($li nk);

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*********@wil lglen.net (it's reserved for spammers)
Jul 17 '05 #2
On 30 Oct 2003 09:01:04 -0800, wa***@mishre.co m (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($qu ery)
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_gp c 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($_PO ST['memberNo']);
$result = mysql_query($qu ery) or die(mysql_error ());
OK - error checking here!
$line = mysql_fetch_ass oc($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_ass oc($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
5787
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 information from MEMO fields within the Access 2000 database. I have not had any problems before displaying MEMO fields on NT and have tried various tips to resolve this issue but no luck so far. I was wondering if anyone had come accross a similar...
6
7477
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 "System.Data.DataR..." is displayed inside the combobox is the amount of records in the dataset. On the other hand, if my query is "select emplcode from payemployee", the databind will work fine (but I don't want to limit the dataset to one field)....
4
1309
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 SqlDataReader
2
1350
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 instead. Hi everyone, I've got quite a specific query that I'm trying to resolve with Microsoft Access and I'm hopeful someone out there can offer a solution to my problem. I have records that I'm displaying on a web page from an Access...
6
4435
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
1756
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"> <h2>Easy MLS Search for Oregon and Southwest Washington:</h2> <div class="content"> </div> </div>
2
7045
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 database I wrote for home, the combo boxes and listboxes no longer display the bound column. For example, on a form I have a combo box based on a table called 'names'. The two columns in the combo box are ID and Surname. The combo box and list box...
4
3879
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 fields correctly and I suspect this problem is related to Service Pack 3. I haven't been able to find any other posts on the Net about this problem? Has anyone else noticed this and more importantly, is there a solution?
2
1691
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 record is inserted. I guess this means the identity column is incremented when you actually reconnect to the database and insert the new record there, rather than just add the record to the locally-resident DataSet, correct? And I guess this also...
0
8388
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8817
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.