I am having a pretty weird problem with my PHP MySQL Query, I am trying to return rows that have the correct rname, rcity, and rstate values.
$sql = "SELECT * FROM `images` WHERE rname = '" . $rname . "' AND rcity = '" . $rcity . "' AND rstate = '" . $rstate . "'";
When I run that query, it only returns 0 results. However after some playing around with it, If I only use rname and rstate in the WHERE Clause it returns results.
$sql = "SELECT * FROM `images` WHERE rname = '" . $rname . "' AND rstate = '" . $rstate . "';
That works perfect. So when i tried just useing rcity in the WHERE Clause.
$sql = "SELECT * FROM `images` WHERE rcity = '" . $rcity . "'";
0 results return. So something is wrong with the rcity portion of the query. If I hard write the value into the query instead of the $rcity variable, it pulls up results. I doubled checked to make sure $rcity was declared, it has the correct value, etc.
I also created another test table in the database to check to see if it was a problem on the db side. Which the problem still existed.
Here is the full code of the getQuery() Function
private function getQuery($data){
// Takes raw data and creats image(s) query to search for listing resort...
$listing = $data['listing'];
$rname = $data['rname'];
$rcity = $data['rcity'];
$rstate = $data['rstate'];
$query = "SELECT * FROM `test` WHERE rname = '" . $rname . "' AND rcity = '" . $rcity . "' AND rstate = '" . $rstate ."'";
return $query;
}
And Here is my database class
class db {
public function __construct(){
$this->server = DB_SERVER;
$this->user = DB_USER;
$this->Pass = DB_PASS;
$this->Database = DB_Database;
}
protected function connect(){
return mysqli_connect($this->server, $this->user, $this->Pass, $this->Database);
}
public function query($sql){
$conn = $this->connect();
$query = $conn->query($sql);
if($query == false) {
throw new Exception("Query failed:".PHP_EOL.$conn->error.PHP_EOL.$sql);
}
if($query->num_rows == 0) {
// need E_NOTICE errors enabled to see this,
// on screen if display_errors is on, else in PHP error log
trigger_error("Query returned 0 rows:".PHP_EOL.$sql);
}
$result = array();
while ($row = $query->fetch_assoc()){
$result[] = $row;
}
return $result;
}
}
I call the query in a class __construct function like so
$con = new db;
$sql = $this->getQuery($data);
$result = $con->query($sql);
I think problem can be with syntax or mysql screening. Try to use PDO with bindParam method
$sql = "SELECT * FROM `images` WHERE rname = '$rname' AND rcity = '$rcity' AND rstate = '$rstate'";
Try to implement this i.e. using variable directly between ' (apostrophe), It should work perfect
Related
i open a sql connection and try to send it to another function and it doesnt work .
its probbably something with sintax ,
i search all over the internet and couldnt find it .
my code :
$mainDataBase = mysqli_connect(/...../);
if ($mainDataBase->connect_errno) {
printf("Connect failed: %s\n", $mainDataBase->connect_error);
die;
}
/**
* 1 -> logIn && getInformation
*
* */
switch ($whichOperation){
case 1:
$gettingPlayerNumber = checkIfPlayerExist($mainDataBase, $playerName, $playerPass);
if($gettingPlayerNumber == 0){
echo "F";
die;
}
... keep going ...
function ::
function checkIfPlayerExist($dataBase ,$playerName ,$pass ){
$result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = " . $playerPass .")");
$row = $result->fetch_assoc();
if($row != null)
return $row['playerNumber'];
return 0;
}
its says:
Call to a member function fetch_assoc() on a non-object.
thank you for your time.
after some of the answer i checked and $result is null , but i checked the query before and now and it works!.
pass is int so it doesnt need any '' around it in the query.*
its connected succsesfully to the database - i checked this too
there seems to be a bug in your query,near password,missing quotes:
change,
$result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = " . $playerPass .")");
to
$result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = '" . $playerPass ."')");
Fatal error: Call to a member function super_query() on a non-object in /mp3.is-great.org/htdocs/modules/profile.php on line 7
This is my profile.php:
<?php
if(isset($_REQUEST['username'])) $username = $_REQUEST['username'];
if(!$username) die("User not found!");
$user = $db->super_query("SELECT username, user_id, lastdate, reg_date, banned, info, foto, fullname, playlist, song FROM tan_users WHERE username = '$username'");
if(!$user) die("User not found!");
$row = $db->super_query("SELECT COUNT(*) AS count FROM tan_favorites WHERE user_id = '" . $user['user_id'] . "'");
$user['favorites'] = $row['count'];
$row = $db->super_query("SELECT COUNT(*) AS count FROM tan_playlists WHERE user_id = '" . $user['user_id'] . "'");
$user['playlists'] = $row['count'];
$smarty->assign("User", $user);
$action = $_REQUEST['action'];
$smarty->assign("Action", $action);
if($action == 'favorites'){
$db->query("SELECT source, hash, name FROM tan_favorites WHERE user_id = '" . $user['user_id'] . "'");
while ($row = $db->get_row($sql_result)){
$row['play_url'] = play_url($row['hash'], $row['source'], $row['name']);
$favorites[] = $row;
}
$smarty->assign('Favorites', $favorites);
}elseif($action == 'playlists'){
$db->query("SELECT tan_playlists.id, tan_playlists.foto, tan_playlists.name, tan_users.username, tan_users.fullname FROM tan_playlists LEFT JOIN tan_users ON tan_playlists.user_id = tan_users.user_id WHERE tan_playlists.user_id = '" . $user['user_id'] . "'");
while ($row = $db->get_row($sql_result)){
$playlists[] = $row;
}
$smarty->assign('Playlists', $playlists);
}else{
$db->query("SELECT source, hash, name FROM tan_favorites WHERE user_id = '" . $user['user_id'] . "' LIMIT 0,10");
while ($row = $db->get_row($sql_result)){
$row['play_url'] = play_url($row['hash'], $row['source'], $row['name']);
$favorites[] = $row;
}
$smarty->assign('Favorites', $favorites);
$db->query("SELECT tan_playlists.id, tan_playlists.foto, tan_playlists.name, tan_users.username, tan_users.fullname FROM tan_playlists LEFT JOIN tan_users ON tan_playlists.user_id = tan_users.user_id WHERE tan_playlists.user_id = '" . $user['user_id'] . "' ORDER by id DESC LIMIT 0,10");
while ($row = $db->get_row($sql_result)){
$playlists[] = $row;
}
$smarty->assign('Playlists', $playlists);
}
$smarty->assign("Title", $username);
if($member_id['username'] == $username) $smarty->assign("MEMBER", $member_id);
?>
Any ideas why i'd be getting this error?
Safe Mode is off on my hosting, and it's a free host so I can't change php.ini
I'm kinda a PHP noob, any help and link to solve my problem would be appreciated.
It seems like you haven't created the $db object.
First you have to initialize your DB-connection like
$db = new PDO('host'; 'table', 'user', 'pass');
use query for access the $current_rank this value want to access in different query but this value can not access any where in different query so how to access $current_rank......
$query = "select * from menu_master where menu_id =
$row_id and hotel_id='" . $_REQUEST['hotel_id'] . "'";
$result = mysql_query($query)."<br/>";
while($row=mysql_fetch_array($result))
{
$rank = $row['set_rank'];
}
$current_rank = $rank;
//echo $current_id = $row_id."<br/>";
//echo $new_rank =$_REQUEST['set_rank']."<br/>";
$sql = "select * from menu_master where set_rank = '$new_rank ' and hotel_id='".$_REQUEST['hotel_id']."'" ;
// echo $sql."<br/>";
$rs = mysql_query($sql)."<br/>";
while($row = mysql_fetch_array($rs))
{
$menu_id = $row['menu_id'];
$sql="update menu_master
set set_rank=$current_rank where menu_id= $menu_id and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
mysql_query($sql)."<br/>";
}
$sql="update menu_master set menu_name = '" . mysql_real_escape_string($_REQUEST['menu_name']) . "',
menu_name_ar = '" . mysql_real_escape_string($_REQUEST['menu_name_ar']) . "',
is_active = '" . $is_active . "',
set_rank = $new_rank where menu_id = '$current_id' and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
//exit;
mysql_query($sql);
Your current_rank seems to be an array. If you have single value in current_rank, then do not use while loop for it.
Just use $row=mysql_fetch_array($result);
$current_rank = $row['set_rank'];
Also you have commented out this line.
//echo $new_rank =$_REQUEST['set_rank']."";
So you have no value for $new_rank
function get_user_id()
{
global $db;
$userid = NULL;
if (!empty($_COOKIE['PHPSESSID']))
{
$result = $db->execute("
SELECT profile_id
FROM " . TABLE_PREFIX . "profile_online
WHERE hash = '" . $db->escape_string($_COOKIE['PHPSESSID']) . "'
");
if ($row = $db->fetch_array($result))
{
$userid = $row[0];
}
}
return $userid;
}
function get_membership_id($userid)
{
global $db;
$result = $db->execute("
SELECT * FROM date_profile WHERE profile_id = '" . $db->escape_string($userid) . "'");
$mem = $db->fetch_array($result)
$membership = $mem[17];
return $membership;
}
the get_user_id is functioning fine... but the membership part i just can't get it to work..
I am trying to take the membership ids.. and make it so certain code will not run for them.
with an : if ($membership != 18 )
so it shows all everyone except the membership 18 people...
also tried this:
function get_membership_id($userid)
{
global $db;
$membership = $db->execute("SELECT `membership_type_id` FROM `date_profile` WHERE `profile_id`= '" . $db->escape_string($userid) . "'");
return $membership;
}
any help would be appreciated greatly.
missed ; here
$mem = $db->fetch_array($result);
^
You're missing semicolon after $mem = $db->fetch_array($result).
forgot to use TABLE_PREFIX constant.
SELECT * FROM " . TABLE_PREFIX . "date_profile WHERE profile_id ...
OK I know this should be easy but i'm going round in circles. I have two tables, and two functions each running a query, the first function gets a product, the second one gets images for a product
I want to get an array which is the product, and it's images...
here's my code...
/**
* Gets the requested product from the DB
*
* #param string $productUrl
* #param string $productID
*/
private function _db_get_product($productUrl = null, $productID = null) {
if (empty($productUrl) && empty($productID))
return;
$db = $this->getConnection();
$q = "SELECT " . $this->_leaf_sql_fields() .
" FROM content_products_items pr WHERE pr.productStatus >= "
. menuMachine::getMinimumStatus() . " ";
if (!empty($productUrl))
$q .= " AND productUrl = '" . $productUrl . "'";
if (!empty($productID))
$q .= " AND productID = '" . $productID . "'";
if ($res = $db->recordsetSingle($q))
$this->_product = $res;
return $res;
}
/**
* Get the images for the product
* #return array
*/
private function _db_get_product_images($productID) {
$db = $this->getConnection();
$q = "SELECT * FROM content_products_images WHERE productID = '" . $productID . "'";
$this->_productImages = $db->recordset($q);
}
Are you just looking for a query to combine the two within the same function?
//Basic query, improve it according to your needs
SELECT
*
FROM
content_products_items as p,
content_products_images as i
WHERE
p.productID = $productId AND
i.productID = p.productID;
Or for a way to call both functions and combine the results in a array?
$myProduct = array(
'productData' => $this->_db_get_product($productUrl, $productID),
'productImages' => $this->_db_get_product_images($productID),
);
Both should guide you into a working direction.
My first attempt at answering someone here on StackOverflow, so bear with me ... but I think the below is what you are looking for?
$product = array('product' => _db_get_product($URL, $ID), 'images' => _db_get_product_images($ID));
Alternately, if you want it all in one go and don't need the two separate methods for anything else, you could rewrite your _db_get_product method as follows:
private function _db_get_product($productUrl = null, $productID = null) {
if (empty($productUrl) && empty($productID))
return;
$output = array();
$db = $this->getConnection();
$q = "SELECT " . $this->_leaf_sql_fields() .
" FROM content_products_items pr WHERE pr.productStatus >= "
. menuMachine::getMinimumStatus() . " ";
if (!empty($productUrl))
$q .= " AND productUrl = '" . $productUrl . "'";
if (!empty($productID))
$q .= " AND productID = '" . $productID . "'";
if ($res = $db->recordsetSingle($q))
array_push($output, $res);
$q2 = "SELECT * FROM content_products_images WHERE productID = '" . $productID . "'";
array_push($output, $db->recordset($q2));
return $output;
}