Jump to content

[SOLVED] PHP wont fetch data from MySQL database


RottNKorpse

Recommended Posts

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.

 

// 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

Link to comment
Share on other sites

ahh...ok well I changed the variable and now I have

 

$song_order_sql  = @mysql_fetch_row(mysql_query("SELECT * FROM ".$prefix."_table") or die('Query failed: ' . mysql_error()));
$song_order      = $song_order_sql['song_order'];

echo $song_order;

 

yet it still wont work. :(

Link to comment
Share on other sites

modified to work:

 

<?php
// Database Connection
$dbcnx = @mysql_connect("$dbhost","$dbuname","$dbpass");
$dbselect = @mysql_select_db("$dbname");
if ((!$dbcnx) || (!$dbselect)) { echo "Can't connect to database"; }
/* ##################################################### */
/* NEED TO DEFINE $prefix                                */
/* ##################################################### */

// Fetching data from MySQL
/* EDITED to clean it up and make it just easier to read*/
$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['order'];
echo $song_order;

Link to comment
Share on other sites

modified to work:

 

<?php
// Database Connection
$dbcnx = @mysql_connect("$dbhost","$dbuname","$dbpass");
$dbselect = @mysql_select_db("$dbname");
if ((!$dbcnx) || (!$dbselect)) { echo "Can't connect to database"; }
/* ##################################################### */
/* NEED TO DEFINE $prefix                                */
/* ##################################################### */

// Fetching data from MySQL
/* EDITED to clean it up and make it just easier to read*/
$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['order'];
echo $song_order;

 

thanks for cleaning it up but it still doesnt give me any data...I've tested the sql in phpmyadmin and it is fine...the prefix variable is defined, on a separate file as I stated in the first post.

Link to comment
Share on other sites

lets make sure it's building the query right:

<?php
// Database Connection
$dbcnx = @mysql_connect("$dbhost","$dbuname","$dbpass");
$dbselect = @mysql_select_db("$dbname");
if ((!$dbcnx) || (!$dbselect)) { echo "Can't connect to database"; }
/* ##################################################### */
/* NEED TO DEFINE $prefix                                */
/* ##################################################### */

// Fetching data from MySQL
/* EDITED to clean it up and make it just easier to read*/
$sql = "SELECT * FROM ".$prefix."_table;";
print $sql;
/*
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
$song_order_sql  = @mysql_fetch_row($result);
$song_order      = $song_order_sql['order'];
echo $song_order; */

if

$prefix = "cheese";

you should see:

SELECT * FROM cheese_table;

if you just see

SELECT * FROM _table;

your problem is $prefix

 

Link to comment
Share on other sites

the only reason I mention it is everything else appears fine, and it should be echoing the result. try this then:

<?php
// Database Connection
$dbcnx = @mysql_connect("$dbhost","$dbuname","$dbpass");
$dbselect = @mysql_select_db("$dbname");
if ((!$dbcnx) || (!$dbselect)) { echo "Can't connect to database"; }
/* ##################################################### */
/* NEED TO DEFINE $prefix                                */
/* ##################################################### */

// Fetching data from MySQL
/* EDITED to clean it up and make it just easier to read*/
$sql = "SELECT * FROM ".$prefix."_table;";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while ($song_order_sql  = @mysql_fetch_assoc($result)){
$song_order  = $song_order_sql['order'];
echo $song_order;
echo "<br />";
}

this should print out a list of all items. lets see if you have data now.

Link to comment
Share on other sites

that worked!!!...thanks dude...

 

I used your suggestion of using mysql_fetch_assoc instead of fetch_row and it worked.

 

now looks like

 

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

 

so thanks again dude.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.