...

View Full Version : mysql_fetch_array - Question!



Jon W
01-04-2008, 07:14 PM
Alright, well I've been trying to figure this out and can't seem to find out why the info that is in the database isn't displaying onto the page. I have info that is being held in these tables, but when I call the mysql_fetch_array function to grab the info out of the database and then I try and echo it out onto the page, I get my die message. Any ideas? Heres my script.





<?php

$con = mysql_connect('localhost','username','pass','db');

if(!$con)
{
die('Database error while trying to connect');
}

mysql_select_db('db' . $con);

$query = mysql_query("SELECT id, fname, lname, password, email, age FROM register") or die('Database error.');

while($display = mysql_fetch_array($query))

{

echo "First Name: " .$display ['fname']. "<br />Last Name: " . $display ['lname']. "<br />Password: " .$display['password']. "<br />Email: " .$display ['email']. "<br />Age: " .$display['age']."";

}


?>

aedrin
01-04-2008, 07:16 PM
Try this:



$query = mysql_query("SELECT id, fname, lname, password, email, age FROM register") or die("Query error: ".mysql_error());

Jon W
01-04-2008, 07:22 PM
Ok, I did that. Its saying that no Database was selected. Doesn't make sense because I'm pretty sure when I say: 'FROM register' means that I'm selecting a database.

arnyinc
01-04-2008, 07:32 PM
Register is a table.

Let's say you wanted to work on a couple different projects. You are making a shopping cart application for yourself and your cousin asks you to create a message board.

You would create two different databases. One named shopping_cart and one named messageboard (or whatever).

Each of those databases would have their own tables. Register is a table that is part of your 'db' database.

EDIT: For that matter, it is dying when you are initially connecting. You need to fix this line:

$con = mysql_connect('localhost','username','pass','db');

I don't believe you pass the database at that point so it should be:
$con = mysql_connect('localhost','username','pass');

http://php.net/mysql_connect

Jon W
01-04-2008, 07:36 PM
Any ideas on what that could be. I know I have a table in the database called 'register'. The fields are id(primy key, Autoincrement), fname, lname, password, email, age, but yet its saying that I didn't select a database... Not making sense. I should also say that I have stuff in the field for the mysql_fetch_array to display onto the page.

anarchy3200
01-04-2008, 07:52 PM
If you start with arnyinc's suggestion of removing the db part from the initial connect then in your mysql_select_db you need to change the full stop to a comma i.e.:




$con = mysql_connect('localhost','username','pass');

if(!$con)
{
die('Database error while trying to connect');
}

mysql_select_db('db' ,$con);

Jon W
01-04-2008, 08:02 PM
Ahh.... Thank you very much for your help. I missed that complately. I actually noticed it before I came back to the thread to see if you had posted back any more replys because I went ahead and checked to see what I had for my other connection script that I had for when I submited the stuff to the database, and shortly after noticed what I my problem was. However I did remove the extra DB and it seems to work fine. Once again, thank you for your time.

Jon W
01-04-2008, 08:10 PM
Hey anarchy3200, do you talk on a IM services? If you do I was wondering if I could get your screen name for if I had some quick question I could ask you. I'm fairly new to PHP and to have someone I could ask a quick question really fast would be great. My Yahoo! screen name is 'manskater777@yahoo.com'.

Thanks
Jon W



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum