How to get single element from $row[id] - php

I'm working on a project for PHPBB. I have a problem that I get some $row types. And they have some "id"s from MySQL, But I want theese "id"s by one by.
It's SQL query ;
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'u.*, z.friend, z.foe, p.*',
'FROM' => array(
USERS_TABLE => 'u',
POSTS_TABLE => 'p',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(ZEBRA_TABLE => 'z'),
'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
)
),
'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
AND u.user_id = p.poster_id'
));
$result = $db->sql_query($sql);
And then for user_id's
$rowset[$row['post_id']] = array(
'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
'user_id' => $row['user_id'],
.
.
I logged $row[user_id] for in a topic and it return ;
97777
97778
97779
97783
But I want just first id i mean -> "97777" . So how can i pass just "97777" to a variable?
Dont suggest about sql level because I can't change any SQL command.

Dont suggest about sql level because I can't change any SQL command.
This is the problem though, your PHP is clearly selecting all 9 rows;
Using WHERE user_id = '97777' you will only select the record you want.
Why can you not edit the SQL? aren't you familiar with SQL or is the code your are using your own?

Without any code it is hard to say what you want to do:
reset($row);
$first_key = key($row);
or
reset($rows);
$first_key = key($rows);
Hard to guess without seeing any code.

You can limit your records using LIMIT clause.
SELECT column_name FROM table_name LIMIT 1;
It will return the first record of that table.

Related

Sql operator "and" and "or" not working correctly

When I run this code there seems to be a problem with the Boolean and and or operations:
$ne = News::find(['conditions' => 'title_md = "' . htmlspecialchars($_post['title_md']) . '" OR alias = "'.$item->alias.'" AND id != ' . $id])->toArray();
The condition inside if(count($ne) < 1) returns true, but I need to get false because id is not currently taken.
Here is the correct query using Phalcon's ORM full potential.
$ne = News::find([
'conditions' => '(title_md = :title: OR alias = :alias:) AND id != :id:',
'bind' => [
'title' => $_POST['title_md'],
'alias' => $item->alias,
'id' => $id,
]
])->toArray();
As the guys above mentioned you have to be more careful when binding parameters to avoid SQL injection. More examples and howto's in the docs: https://docs.phalconphp.com/zh/3.2/db-models#binding-parameters

How to use value from 'join' clause in 'where' clause?

There is join, and I need to include value from it in 'where'. Is there any way to do this? In other words, I want to apply 'where' clause only to that values, which being joined. I'm really got confused with it, and may be this task can be solved more easily.
$this->table = "CoolTable";
$sql = new Sql($this->getAdapter());
$select = $sql->select()
->columns(array(/*some expressions...*/))
->from($this->table)
->join(
array('ct' => 'CoolTable'),
new Expression('
ct.a = CoolTable.a AND
ct.b = CoolTable.b
'),
array()
)
/*another joins...*/
->where(array(
'CoolTable.a' => $someExternalVar,
'CoolTable.b = ?' => '$ct.a',
))
What about my tries:
'CoolTable.b = ?' => '$ct.a'
'CoolTable.b' => 'ct.a'
'CoolTable.b' => new Expression('ct.a') /*it's a really pity, yes*/
new Predicate\PredicateSet(
array(
new Operator(
'CoolTable.b',
Operator::OPERATOR_EQUAL_TO,
'ct.a'
),
),
Predicate\PredicateSet::TYPE_VALUE
),

PHP. How to take data from 2 mysql tables instead of 1

Learning php and I am losing my mind trying to solve this for days now. Please help.
This is a code which goes thought a table COUPON, take data with a condition met, and download it afterwards. In this table COUPON I have USER_ID as number but I want to have a user name also, which is kept in another table USER.
How can I go to another table (USER) and take names (REALNAME) by this USER_ID which is the same in both tables?
if ( $_POST ) {
$team_id = abs(intval($_POST['team_id']));
$consume = $_POST['consume'];
if (!$team_id || !$consume) die('-ERR ERR_NO_DATA');
$condition = array(
'team_id' => $team_id,
'consume' => $consume,
);
$coupons = DB::LimitQuery('coupon', array(
'condition' => $condition,
));
if (!$coupons) die('-ERR ERR_NO_DATA');
$team = Table::Fetch('team', $team_id);
$name = 'coupon_'.date('Ymd');
$kn = array(
'id' => 'ID',
'secret' => 'Password',
'date' => 'Valid',
'consume' => 'Status',
);
$consume = array(
'Y' => 'Used',
'N' => 'Unused',
);
$ecoupons = array();
foreach( $coupons AS $one ) {
$one['id'] = "#{$one['id']}";
$one['consume'] = $consume[$one['consume']];
$one['date'] = date('Y-m-d', $one['expire_time']);
$ecoupons[] = $one;
}
down_xls($ecoupons, $kn, $name);
After this, I want to try to do the same thing using only SQL queries.
You would need to JOIN the tables in the SQL query
SELECT something FROM coupons as coupons JOIN user as user ON coupons.id=user.id
You should use join when you want to retrieve details from two tables.
Join table COUPON and table USER based on user_id . This should yield results you want.

MySQL returns null values for certain rows

I am having a problem with MySQL and PHP. I'm trying to create something that gets values from a database, encode it in JSON and then print it. I have that down, but there's something that is keeping from one certain row in the database from displaying. It always returns NULL except for the id value I set. Here's my code, am I doing something wrong?
$srv = mysql_query("SELECT * FROM `players` WHERE `name` LIKE '%" . mysql_real_escape_string($_GET['q']) . "%'");
while ($record = mysql_fetch_array($srv)) {
$playerInfo = array('id' => $playerarray['id'], 'name' => $playerarray['name'], 'server' => $playerarray['server']);
echo(json_encode($playerInfo));
}
If you want to take a look at it, it's hosted here. The funny thing is, this page uses the exact same code, but doesn't return null. Any ideas?
Edit:
Here's what is in $playerInfo (when I use geekygamer14)
array (size=3)
'id' => null
'name' => null
'server' => null
It seems that whatever rows that have verified set to 1 (integer), it gives NULL.
You're using variable $record in your loop, though this doesn't show in your generated array. Instead you're using a variable named $playerarray. I assume the code should be:
while ($record = mysql_fetch_array($srv)) {
$playerInfo = array('id' => $record['id'], 'name' => $record['name'], 'server' => $record['server']);
echo(json_encode($playerInfo));
}
Note: Consider using an alternative to mysql-functions, for example mysqli. Mysql-functions are deprecated.
Change this
$srv = mysql_query("SELECT * FROM `players` WHERE `name` LIKE '%" . mysql_real_escape_string($_GET['q']) . "%'");
while ($record = mysql_fetch_array($srv)) {
$playerInfo = array('id' => $playerarray['id'], 'name' => $playerarray['name'], 'server' => $playerarray['server']);
echo(json_encode($playerInfo));
}
to this
$srv = mysql_query("SELECT * FROM `players` WHERE `name` LIKE '%" . mysql_real_escape_string($_GET['q']) . "%'");
while ($record = mysql_fetch_array($srv)) {
$playerInfo = array('id' => $record['id'], 'name' => $record['name'], 'server' => $record['server']);
echo(json_encode($playerInfo));
}
Your variables name $playerarray['id'] is wrong this is write $record['id']. Your data is in $record because of loop so you should use $record instead of $playerarray.

CakePHP: How to retrieve data by multiple condition

Please help me to retrieve data from a table by multiple condition in Cakephp
I have one table name: article; I have tried to retrieve data with the code below
I want to get specific id as given in the parameter; article_price > 0 and article_status > 1
public function getArticle($artID = ''){
return $this->find('all', array(
'condition' => array(
'article_id =' => $artID,
'article_price' => '> 0',
'article_status = ' => '1'),
'order' => 'article_id DESC'
));
}
// the out put was selected all data without condition that I want.
What was the problem with my code?
What I found out is I print: echo $this->element ('sql_dump'); and I got the following sql statement:
SELECT `article`.`article_id`, `article`.`name`, `article`.`article_price`, `article`.`article_status` FROM `db_1stcakephp`.`article` AS `article` WHERE 1 = 1 ORDER BY `article_id` DESC
Please help me.
Thank!
If your model name is Article:
public function getArticle($art_id) {
return $this->find('first', array(
'conditions' => array(
'Article.article_id' => $art_id,
'Article.article_price >' => 0,
'Article.article_status >' => 1,
),
));
}
Using 'Model.field' syntax is optional, until your models have relationship and have the same names - for example Article.status and Author.status.
Moving comparison sign into array's key part allows you to do:
'Article.price >' => $minPrice,
'Article.price <=' => $maxPrice,
And I didn't really notice typo in 'conditions'.

Categories