Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP wont fetch data from MySQL database

Status
Not open for further replies.

RottNKorpse

Technical User
Aug 8, 2006
13
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:
// 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
 
If you had removed all the drop-dead signs (@), you would certainly have got a warning that mysql_fetch_row was not called with a query resource. While you don't want to have to verbose errors showing on the pages of a production site, remove error suppression when debugging.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
I agree. If your developing don't put error suppression in, it makes finding errors very difficult.

so remove all your "@" and try again.

Also I would do the query separate form the row fetching if only to find possible errors.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I dont care about the errors really so I've removed the @ symbols from the database and the fetch yet nothing has changed.

it isnt that the query isnt looking into the table, it is. It isnt that the connection isnt working, it is. It is that when looking into the row and trying to pull the data from it, it isnt pulling anything and just resulting in an empty variable.

Code:
$sql             = "SELECT * FROM ".$prefix."_table";
$result          = mysql_query($sql) or die('Query failed: ' . mysql_error());
$song_order_sql  = mysql_fetch_row($result);
$song_order      = $song_order_sql['song_order'];
echo $song_order;
 
I would start to question your $prefix variable then. Is it properly populated?. Does it have the value you expect?

Is your query actually returning a row?

You can use mysql_num_rows to find out if you are getting a row back.

Also try adding a die statement to your connection.
Code:
$dbcnx = mysql_connect("$dbhost","$dbuname","$dbpass") or die(mysql_error());

Just to make sure.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
yes the prefix was fine...I tested all the basics of it in every way I could so I know it wasn't any variable issues but anyway I've got it solved now.

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:
$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:
$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;
 
Right yes, I missed that, mysql_fetch_row returns a numerical array, so you can't use the name of your field but instead use a number to point to it. Such as $song_order_sql[[red]x[/red]]

Where x is the number of the column you want to access.

in your case probably $song_order_sql[[red]0[/red]]





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
yea it would have been [0] and yea I didn't even notice that either because I use fetch_row all the time on my CMS's programming I forgot that the default one doesn't work anything like the CMS's
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top