function getfield($get){
global $connection;
$query = "SELECT id, username, firstname, lastname FROM users WHERE username='".$_SESSION['user_id']."'";
if ($query_r = mysqli_query($connection, $query)) {
$num_rows = ($query_r -> num_rows);
if ($mysqli_result = mysqli_result($query_r, 0, $get)) {
return $mysqli_result;
}
I made a log in form and now all working fine but this function don't showing data. I think mysql_result not working in 7.1.
function getfield($get){
global $connection;
session_start();
$query = "SELECT id, username, firstname, lastname FROM users WHERE username='".$_SESSION['user_id']."'";
if ($query_r = mysqli_query($connection, $query)) {
$num_rows = ($query_r -> num_rows);
if ($mysqli_result = mysqli_fetch_assoc($query_r)) {
return $mysqli_result;
}
}
The function mysqli_result does not exist, so you would need to use something like mysqli_fetch_array(). Replace your innermost if-clause with this:
if ($mysqli_result = mysqli_fetch_array($query_r)) {
return $mysqli_result[$get];
}
Related
I am creating a function that is suppose to get the first name and last name from the database and return the query. Something is not right about it though.
The first and last names don't show up. I'm not getting any errors or warnings and I've tried all of the answers provided on this site and others(there is not much though).
Can someone tell me what is wrong with it ?
public function getFirstAndLastName() {
$username = $this->user['username'];
$query = $this->con->prepare("SELECT first_name, last_name FROM users WHERE username = ? ");
$query->bind_param("s", $username);
$query->execute();
$query_result = $query->get_result();
$query_result->fetch_array();
while ($row = $query_result->fetch_assoc()) {
$row['first_name'];
}
return $row;
}
First of all if you are trying to finding a better way you can use this one
public function getFirstAndLastName() {
$username = $this->user['username'];
$query = $this->con->prepare("SELECT first_name, last_name FROM users WHERE username = ? ");
$query->bind_param("s", $username);
$query->execute();
$query_result = $query->get_result();
$result = $query_result->fetch_all(MYSQLI_ASSOC);
Return $result;
}
mysqli has a handy function that instantly returns an array from the query result: mysqli_fetch_all(). So instead of this lines
while ($row = $query_result->fetch_assoc()) {
$row['first_name'];
}
it could be just a single line:
$result = $query_result->fetch_all(MYSQLI_ASSOC);
if you are looking to finding the answer why your function will return null i will explain for you :
There is some mistake in your codes
first of all when you execute this line $query_result->fetch_array();
actually you just make empty the mysqli buffer! so you don't have anything in buffer to catch it in this line -> while ($row = $query_result->fetch_assoc()) {
and in other hand even if you had something in buffer then you dont do nothing in this line ->
$row['first_name'];
if you are looking to correct your codes you should write the code like this ->
first of all make comment this line -> $query_result->fetch_array();
public function getFirstAndLastName() {
$username = $this->user['username'];
$query = $this->con->prepare("SELECT first_name, last_name FROM users WHERE username = ? ");
$query->bind_param("s", $username);
$query->execute();
$query_result = $query->get_result();
//$query_result->fetch_array();
while ($row = $query_result->fetch_assoc()) {
$result[] = $row['first_name'];
}
return $result;
}
Edit:
if you are looking to get both first name and last name you have to do like this ->
public function getFirstAndLastName() {
$username = $this->user['username'];
$query = $this->con->prepare("SELECT first_name, last_name FROM users WHERE username = ? ");
$query->bind_param("s", $username);
$query->execute();
$query_result = $query->get_result();
//$query_result->fetch_array();
while ($row = $query_result->fetch_assoc()) {
$result[] = $row;
}
return $result;
}
Try with $query->bind_param(":username", $username)
and change in the query the ? by :respuesta
"SELECT first_name, last_name FROM users WHERE username = :username
Looks the docs and apologies for my poor english
https://www.php.net/manual/es/pdostatement.bindparam.php
I'm trying to find out the id value which is the primary key of the table. In order to do so, I implemented a code like this.
public function addNewPost($userName, $content) {
// insert post information into the data.
$query = "INSERT INTO posts(uploader, content, uploaded_at) VALUES('$userName', '$content', NOW())";
$result = mysqli_query($this->db->connect(), $query);
$id = mysqli_insert_id($this->db->connect());
if ($result) {
// get the row data with the found primary key
$query_get_row_data = "SELECT * FROM posts WHERE id = '$id'";
$result_row_data = mysqli_query($this->db->connect(), $query_get_row_data);
return mysql_fetch_array($result_row_data);
} else {
return false;
}
}
Here I tried to get the id by running thhe $id = mysqli_insert_id($this->db->connect()); command, but it doesn't seem to work.
Any solutions?
You need to use a connection variable not a function call. A second call to connect() is returning a new connection.
$connection = $this->db->connect();
$result = mysqli_query($connection, $query);
$id = mysqli_insert_id($connection);
Or if you keep a variable in the db class which makes sense:
$result = mysqli_query($this->db->connection, $query);
$id = mysqli_insert_id($this->db->connection);
Save the connection
public function addNewPost($userName, $content) {
// insert post information into the data.
$query = "INSERT INTO posts(uploader, content, uploaded_at) VALUES('$userName', '$content', NOW())";
$con = $this->db->connect();
$result = mysqli_query($con, $query);
$id = mysqli_insert_id($con);
if ($result) {
// get the row data with the found primary key
$query_get_row_data = "SELECT * FROM posts WHERE id = '$id'";
$result_row_data = mysqli_query($this->db->connect(), $query_get_row_data);
return mysql_fetch_array($result_row_data);
} else {
return false;
}
}
I know this isn't good practice to have the queries within a controller, but for some reason they do not work within this controller class. I'm not too experienced with codeigniter, so any help is appreciated, as I have no idea on how else to set this up to work properly. I have an ajax script that gets the information.
Here is my code:
<?php
class Phonecall extends CI_Controller {
public function index() {
/*$con = mysqli_connect('localhost','root','root','MYDB');
if (!$con) {
die ('Could not connect: ' . mysqli_error($con));
}*/
//$operatorId = $_SESSION['Oper']['OperatorID']; //I also cannot figure out how to get $_SESSION information either
//$sql = "SELECT phone_number FROM incoming_calls WHERE OperatorID='${operatorId}'";
//$result = mysqli_query($con,$sql);
$query = $this->db->query("SELECT phone_number FROM MYDB.incoming_calls");// WHERE OperatorID='${operatorId}'");
while ($row = $query->row_array()) {
$number = $row['phone_number'];
}
/*$sql = "SELECT Username, UserID, Name
FROM tblUsers
WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'";
*/
$query = $this->db->query("SELECT Username, UserID, Name
FROM MYDB.tblUsers
WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'");
while ($row = $query->row_array()) {
$userArray[] = array("name" => $row['Name'], "username" => $row['Username'], "user_id" => $row['UserID']);
}
if (!empty($userArray)) {
echo json_encode($userArray);
}
if (isset($_POST["drop"])) {
$query = $this->db->query("DELETE FROM MYDB.incoming_calls
WHERE phone_number = $number
LIMIT 1");
if (!$result) {
die ('Could not drop row: ');
}
}
$this->db->close();
}
}
?>
Here is my JS:
var user = new Array();
var user_id = new Array();
var name = new Array();
$.get(baseURL + 'phonecall/index', function(data) {//where baseURL is defined
var loginInfo = jQuery.parseJSON(data);
for (var i = 0; i < loginInfo.length; ++i) {
name[i] = loginInfo[i].name;
user[i] = loginInfo[i].username;
user_id[i] = loginInfo[i].user_id;
}
}
I do not know much about frameworks and I'm trying to learn, I also do not know how to make this file access the $_SESSION data. Any help is greatly appreciated.
Always use model for DB queries.
For first query use the following code:
$this->db->select("phone_number");
$this->db->from("incoming_calls");
$query = $this->db->get();
return $query->result_array();
For the secound query use this:
$this->db->select("Username, UserID, Name");
$this->db->from("tblUsers");
$this->db->where("PhoneHome,'999-999-9999'");
$this->db->where("PhoneCell,'999-999-9999'");
$this->db->where("PhoneWork,'999-999-9999'");
$query = $this->db->get();
return $query->result_array();
Be sure to make 2 different functions for the queries in model.
Then at your controller side use the following code:
$data['first_record'] = $this->your_modal_name->your_first_function_name();
$data['secound_record'] = $this->your_modal_name->your_second_function_name();
Then run this data in foreach, and it will give you the record, which you want.
I am attempting to access user data from a single table in mysql in order to display it on the screen.
eg. "Logged in as 'username'."
I have followed the instructions from this tutorial.
This is my function:
//create function to fetch user data (eg. username, firstname, address, etc.)
function fetchUserData($field) {
$user_id = $_SESSION['user_id'];
$query = "SELECT '$field' FROM users WHERE id = '$user_id'";
if ($query_run = mysql_query($query)) {
if ($query_result = mysql_result($query_run, 0, $field)) {
return $query_result;
}
}
}
And I'm calling the function like so:
$firstname = fetchUserData('firstname');
$surname = fetchUserData('surname');
echo 'Logged in as '.$firstname.' '.$surname.'.';
The $_SESSION['user_id'] is working, I have tested it. But i am unable to collect the data (username, address, etc.) that correlates with that user's id.
Furthermore, $query_run is returning resource ID #7, whatever that means??
change your single quotes around $field
$query = "SELECT '$field' FROM users WHERE id = '$user_id'";
to backticks
$query = "SELECT `$field` FROM users WHERE id = '$user_id'";
^ ^
Also, you do not have mysql_connect() shown, so most likely you have a variable scope issue - http://php.net/manual/en/language.variables.scope.php - so your query is failing as there is no connection. You can verify this by using or die(mysql_error()) -
if ($query_run = mysql_query($query) or die(mysql_error()) {
or
if ($query_run = mysql_query($query)) {...}
else{die(mysql_error());}
if ($query_result = mysql_result($query_run, 0, $field)) {
return $query_result;
}
should be
if ($query_result = mysql_result($query_run, 0)) {
return $query_result;
}
or you may use
if ($query_result = mysql_result($query_run, 0, "'".$field."'")) {
return $query_result;
}
add single quote to the $field
Sorry about the last post I had. Here's my revision, please help me.
<?php
//connect database
$sql = "SELECT * FROM user where user_id = 8320 AND password = 'admin' ";
$query = pg_query($sql);
var_dump($row = pg_fetch_array($query)); //dumps correctly.
?>
BUT THE PROBLEM IS THIS..when I try to make it as a function LIKE:
function check($user_id, $password)
{
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($sql);
$row = pg_fetch_array($query);
return $row;
}
AND CALL IT HERE:
var_dump($data = check(8320, 'admin')); DUMPS NULL;
How come it ended up like this?
Its returning NULL because there is an error with your SQL query, and no results are being returned. You should do some error checking in your function, try this version:
function check($user_id, $password)
{
$dbconn = pg_connect("host=localhost dbname=test");
$sql = "SELECT * FROM user where user_id = $1 AND password = $2 ";
$result = pg_query_params($dbconn, $sql, array($user_id,$password));
$row = pg_fetch_array($result);
if (!$row) {
echo pg_last_error($dbconn);
} else {
return $row;
}
}
Try the code below. It should work fine for you.
$data = check(8320, 'admin');
var_dump($data);
Seems like your PostgreSQL resource is missing inside the function. You have two options.
Declare the connection resource inside the function using global.
Establish the connection inside the function.
This is the first option:
$conn = pg_connect('host','user','pass','db');
function check($user_id, $password)
{
global $conn;
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
And this is the second option:
function check($user_id, $password)
{
$conn = pg_connect('host','user','pass','db');
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
According to the PHP manual, You may omit connection resource, but it is not recommended, since it can be the cause of hard to find bugs in scripts.