[SOLVED] PHP wont fetch data from MySQL database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

[SOLVED] PHP wont fetch data from MySQL database

Post by RottNKorpse »

I have been staring at just a few lines of code for over an hour and I don't see a single thing wrong with it so I wanted to get some new eyes looking at this to possibly shine some light on the issue.

What I am wanting to do is simply fetch a single value from a table in my database, a table that only has 1 row and 4 columns. Then to insert that data into a variable and call the variable later in the file.

The problem is for some reason when I call for the data it doesn't retrieve anything.

Code: Select all

// Database Connection
$dbcnx = @mysql_connect("$dbhost","$dbuname","$dbpass");
$dbselect = @mysql_select_db("$dbname");
if ((!$dbcnx) || (!$dbselect)) { echo "Can't connect to database"; }
 
// Fetching data from MySQL
$order_sql  = @mysql_fetch_row(mysql_query("SELECT * FROM ".$prefix."_table") or die('Query failed: ' . mysql_error()));
$order      = $order_sql['order'];
 
echo $order;
All of the variables for connecting to the database and the prefix are stored in a separate file and they are pulled from that file with no issue. I've even tested to see if it is even looking for the table correctly and it is because I deleted the table which caused it to tell me it didn't exist so I recreated it and still the same empty result.

Here is my MySQL structure
4 Columns & 1 Row
order - field2 - field3 - field4
manual - 0 - 0 - 0
That's it, nothing special in either the code or the table yet it still refuses to pull the data...any idea what it could be?

Thanks in advance
Last edited by RottNKorpse on Thu Apr 17, 2008 12:02 pm, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: PHP wont fetch data from MySQL database

Post by aceconcepts »

I would write the query like this:

Code: Select all

 
$table=$prefix."_table";
$order_sql  = @mysql_fetch_row(mysql_query("SELECT * FROM '$table' ") or die('Query failed: ' . mysql_error()));
 
Also, I find it easier to implement and look at, if you seperate the mysql functions.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP wont fetch data from MySQL database

Post by onion2k »

Never use @ on code you're testing.

In fact, just never use @.
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

Re: PHP wont fetch data from MySQL database

Post by RottNKorpse »

onion2k wrote:Never use @ on code you're testing.

In fact, just never use @.
what would be better to use instead?
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

Re: [SOLVED] PHP wont fetch data from MySQL database

Post by RottNKorpse »

ok I've got it solved...thanks to everyone for their help.

Here is what fixed it just in case someone has the same issue in the future:

I changed the fetch from mysql_fetch_row to mysql_fetch_assoc and that did the trick. :)

Code: Select all

$sql             = "SELECT * FROM ".$prefix."_table";
$result          = mysql_query($sql) or die('Query failed: ' . mysql_error());
$sorder_sql  = mysql_fetch_row($result);
$sorder      = $song_order_sql['sorder'];
echo $sorder;
to

Code: Select all

$sql             = "SELECT * FROM ".$prefix."_table";
$result          = mysql_query($sql) or die('Query failed: ' . mysql_error());
$sorder_sql  = mysql_fetch_assoc($result);
$sorder      = $song_order_sql['sorder'];
echo $sorder;
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP wont fetch data from MySQL database

Post by onion2k »

RottNKorpse wrote:
onion2k wrote:Never use @ on code you're testing.

In fact, just never use @.
what would be better to use instead?
It's better to see the error and fix it.
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

Re: [SOLVED] PHP wont fetch data from MySQL database

Post by RottNKorpse »

I'd agree but there wasn't an error...I just wasn'ting using the correct fetch call.

Thanks for your help though.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: PHP wont fetch data from MySQL database

Post by markusn00b »

onion2k wrote:
RottNKorpse wrote:
onion2k wrote:Never use @ on code you're testing.

In fact, just never use @.
what would be better to use instead?
It's better to see the error and fix it.
What if you want to prevent php from showing the 'undefined variable' statement?

I think it's perfectly fine to use the @ symbol to surpress errors you know are there.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP wont fetch data from MySQL database

Post by onion2k »

markusn00b wrote:What if you want to prevent php from showing the 'undefined variable' statement?
Define your variables. Problem solved!
markusn00b wrote:I think it's perfectly fine to use the @ symbol to surpress errors you know are there.
Well I don't. If you know there are errors there's no excuse for not fixing them.

The @ symbol is the coding equivalent of sticking your fingers in your ears and singing LA LA LA at the top of your voice to make a problem go away.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: [SOLVED] PHP wont fetch data from MySQL database

Post by aceconcepts »

If you're going to have a system or site etc... that is going to function correctly then these errors need to be sorted...surely!

So basically don't use @
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

Re: PHP wont fetch data from MySQL database

Post by RottNKorpse »

onion2k wrote:The @ symbol is the coding equivalent of sticking your fingers in your ears and singing LA LA LA at the top of your voice to make a problem go away.
lol, buce
Post Reply