John Robertson Nocos
John Robertson Nocos

Reputation: 1485

MySQL works on localhost but not online

My mysql query works perfect on my localhost but it does not work as I FTP to the server. The problem lies in the dynamic table select $tbl_prod.

$branch     = $_SESSION['branch'];    //value is shukey main_products(yes with space)
$tbl_prod   = $branch . "_products";  //for selecting dynamic table from database

$query      =   mysqli_query($db_connect, "SELECT * FROM `$tbl_prod` ORDER BY `prod_id` DESC LIMIT 5") or exit(mysqli_error());

However, when I try to use the name of the table directly it works but that's not the thing that I wanted since I want the table to be dynamically selected.

$query      =   mysqli_query($db_connect, "SELECT * FROM `shukey main_products` ORDER BY `prod_id` DESC LIMIT 5") or exit(mysqli_error());

Is there anyway that I can solve this? Is this about mysql versions or something?

Upvotes: 0

Views: 945

Answers (3)

GuyT
GuyT

Reputation: 4416

You problem is that you are developing on a Windows machine and your server is a Linux machine. Windows isn't case-sensitive, but Linux is.

When you try to execute a query and one table name has an uppercase and the other hasn't, the query will fail.

table != Table

Upvotes: 0

Raheel
Raheel

Reputation: 9024

Do the var_dump($tbl_prod) may be u are getting some additional characters / spaces. so you can trim the variable and then use in query.

Upvotes: 0

Ole Haugset
Ole Haugset

Reputation: 3797

Try to add this before you run the query to see what exactly the script is trying to call against the sql base:

echo "SELECT * FROM `$tbl_prod` ORDER BY `prod_id` DESC LIMIT 5"

EDIT:

also, remove the ` marks surrounding $tbl_prod as you do not need these there.

Upvotes: 1

Related Questions