Page 1 of 1

[SOLVED] PHP wont fetch data from MySQL database

Posted: Thu Apr 17, 2008 10:41 am
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

Re: PHP wont fetch data from MySQL database

Posted: Thu Apr 17, 2008 10:58 am
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.

Re: PHP wont fetch data from MySQL database

Posted: Thu Apr 17, 2008 11:04 am
by onion2k
Never use @ on code you're testing.

In fact, just never use @.

Re: PHP wont fetch data from MySQL database

Posted: Thu Apr 17, 2008 11:16 am
by RottNKorpse
onion2k wrote:Never use @ on code you're testing.

In fact, just never use @.
what would be better to use instead?

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

Posted: Thu Apr 17, 2008 12:05 pm
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;

Re: PHP wont fetch data from MySQL database

Posted: Thu Apr 17, 2008 1:41 pm
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.

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

Posted: Thu Apr 17, 2008 1:49 pm
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.

Re: PHP wont fetch data from MySQL database

Posted: Fri Apr 18, 2008 5:54 am
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.

Re: PHP wont fetch data from MySQL database

Posted: Fri Apr 18, 2008 6:01 am
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.

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

Posted: Fri Apr 18, 2008 7:46 am
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 @

Re: PHP wont fetch data from MySQL database

Posted: Fri Apr 18, 2008 9:33 am
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