My code:
$db1 = new PDO ( 'mysql:host=localhost;dbname=db;charset=utf8', 'root', '');
$db1->setAttribute ( PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING )
$qry = $db1->prepare('SELECT user_name FROM user_data WHERE user_id = $userid LIMIT 1');
$qry -> execute(array($userid));
$row = $qry -> fetch();
echo $qry -> user_name;
And it not eching nothing
And I want to find by $userid, and echo the user_name column(like user_id = 1 and it echo Name)
$db1 = new PDO ( 'mysql:host=localhost;dbname=db;charset=utf8', 'root', '');
$db1->setAttribute ( PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING );
$qry = $db1->prepare("SELECT user_name FROM user_data WHERE user_id = ? LIMIT 1");
$qry -> execute(array($userid));
$row = $qry->fetch();
echo $row->user_name;
Change:
$qry = $db1->prepare('SELECT user_name FROM user_data WHERE user_id = $userid LIMIT 1');
For:
$qry = $db1->prepare('SELECT user_name FROM user_data WHERE user_id = ? LIMIT 1');
You will assign the value to user_id through execute statement
EDIT:
Add semicolon:
$db1->setAttribute ( PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING ); <<<-- Missed semicolon
Related
I'm trying to do a php select statement like this:
$select = mysql_query("select * from message where receiver_id = '$receiver_id'and frm_id = '$frm_id' ORDER BY id DESC");
while($row = mysql_fetch_array($select))
{
$id = urlencode(encryptor('encrypt', $result['user_id']));
$query = mysql_query($sql);
$result = mysql_fetch_assoc($query);
}
But its not working instead its select from table where frm_id = $frm_id only.
Using PDO you can do something like:
$id = "Id wanted here";
$conn = new PDO("server", "username", "password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $conn->prepare("SELECT * from `tablename` WHERE `receiver_id` = :id");
$sth->bindParam(':name', $id);
$sth->execute();
I think there are two errors.
1: you are using $result['user_id'] before defining $result.
2: $sql isn't defined.
also i am not sure about encryptor method.
You can try:
$select= mysql_query("select * from message where receiver_id = $receiver_id
and frm_id = $frm_id ORDER BY id DESC");
or
$select= mysql_query("select * from message where receiver_id = $receiver_id
or frm_id = $frm_id ORDER BY id DESC");
I am writing a query that gets an ID from the database where it's last digit can be 1-5. This works but seems messy. Is there a cleaner way of doing this?
$id1 = $id.'1';
$id2 = $id.'2';
$id3 = $id.'3';
$id4 = $id.'4';
$id5 = $id.'5';
$sql = "SELECT col1
FROM table1
WHERE id = :id1 OR id = :id2 OR id = :id3 OR id = :id4 OR id = :id5
LIMIT 1";
$core = Connect::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->bindParam(':id1', $id1, PDO::PARAM_STR);
$stmt->bindParam(':id2', $id2, PDO::PARAM_STR);
$stmt->bindParam(':id3', $id3, PDO::PARAM_STR);
$stmt->bindParam(':id4', $id4, PDO::PARAM_STR);
$stmt->bindParam(':id5', $id5, PDO::PARAM_STR);
$stmt->execute();
$data = $stmt->fetchAll();
Try This
$sql = "SELECT col1
FROM table1
WHERE id = :id1 OR id = :id2 OR id = :id3 OR id = :id4 OR id = :id5
LIMIT 1";
$core = Connect::getInstance();
$stmt = $core->dbh->prepare($sql);
for($i; $i<=5; $i++){
$stmt->bindParam(':id'.$i, $id.$i, PDO::PARAM_STR);
}
$stmt->execute();
$data = $stmt->fetchAll();
Another alternative would be to use question mark placeholders. All the values, just load em all up inside the ->execute method:
Question mark placeholders:
$sql = "SELECT col1
FROM table1
WHERE id = ? OR id = ? OR id = ? OR id = ? OR id = ?
LIMIT 1";
Inside execute:
$stmt->execute(array($id1, $id2, $id3, $id4, $id5));
How to loop php using ref data from mysql ?
work flow :
Loop If column 'xxx' == ''
{
$sql = "SELECT * FROM table WHERE xxx == '' order by id desc";
$result = mysql_query($sql);
$datas=mysql_fetch_array($result);{
$id = stripslashes(str_replace('\r\n', '<br>',($datas['id'])));
}
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
ypur query is confusing. why would you store ids with html in them? regardless, this should work
$sql = "SELECT * FROM table WHERE xxx != '' "; //ORDER BY will not work since ids are not int
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result){
$id = stripslashes(str_replace('\r\n', '<br>',($row['id'])));
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
I have a function in php that does retrieval and display work from a MySql database.
It's like this:
function thisFunction($id,$country,$year){
global $conn;
$conn = connect();
$stmt = $conn->prepare("select * from table where id = :id and countryCode = :country and YEAR(addedDate) = :year and status = 0");
$stmt->execute(array(
':id' => $id,
':country' => $location,
':year' => $year
));
}
The problem is, sometimes $id has a value, sometimes it doesn't. When it does have a value, I'd like to select records with that value, when it doesn't I'd like to select all.
How do I write the sql in there, or do this thing, so that when there is a value it'll select only records with that value, and when there isn't a value, it'll select all. It's the part where when no value is selected - then select all where I'm stuck.
I call the function like anyone would. Nothing unique there.
select * from table where id = 9 -- works fine - displays all records where id = 9
select * from table where id = no value supplies - should display all value. How do I do this?
Can you please help?
select * from table where id = * //Does not work
Just remove the id part if it's empty:
function thisFunction($id,$country,$year){
global $conn;
$conn = connect();
if (!isset($id) || empty($id))
{
$stmt = $conn->prepare("select * from table where countryCode = :country and YEAR(addedDate) = :year and status = 0");
$stmt->execute(array(
':country' => $location,
':year' => $year
));
}
else
{
$stmt = $conn->prepare("select * from table where id = :id and countryCode = :country and YEAR(addedDate) = :year and status = 0");
$stmt->execute(array(
':id' => $id,
':country' => $location,
':year' => $year
));
}
}
select * from table where id = id;
Sample fiddle
You could use something like this:
function thisFunction($id,$country,$year){
global $conn;
$sql_query = "select * from table where status = 0";
$where_data = array();
if(isset($id) && $id != '*'){ // Only add this if ID is set to something other than *
$where_data[':id'] = $id;
$sql_query .= " AND id = :id";
}
if(isset($location)){
$where_data[':country'] = $location;
$sql_query .= " AND countryCode = :country";
}
if(isset($year)){
$where_data[':year'] = $year;
$sql_query .= " AND YEAR(addedDate) = :year";
}
$conn = connect();
$stmt = $conn->prepare($sql_query);
$stmt->execute($where_data);
}
you could potentially select them if the column is not null.
select * from table where id is not null
My Database
id username
1 xxx
2 bbb
3 vvv
4 bbbbbb
i need output as
array('xxx','bbb','vvv','bbbbb');
Using PDO i need to fetch username from db & make that to array, is to for checking username exists
If you're only checking if a username exist, it would be optimal to modify your query to do just that, instead of returning excess data:
$query = $pdo->prepare("SELECT `id` FROM `users` WHERE `username` = ? LIMIT 1");
$query->execute(array($username_to_test));
if ($query->rowCount() == 1)
{
// user exists in table
}
If you do happen to need an array of usernames, do something like the following:
$usernames = array();
$query = $pdo->query("SELECT `username` FROM `users`");
while(($row = $query->fetchObject()) != null)
$usernames[] = $row->username;
$pdo = new PDO($dsn,$user,$pass);
$pdos = $pdo->prepare('select username from mytablename');
$pdos ->execute();
$results = array();
while ($row = $pdos->fetch(PDO::FETCH_ASSOC)) {
$results[] = $row['username'];
}
var_dump($results);
Set Your database $dsn, $user and $pass before according to doc.
To do exactly what you've asked:
$query = $dbh -> prepare("SELECT username from users");
if ($query -> execute()){
$user_array = $query -> fetch(PDO::FETCH_ASSOC);
}
Tim Cooper's answer is probably a better solution though.