Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The following code is not working properly:
$query = $this->db->query("SELECT accountType FROM users WHERE id = $loggedID" );
It errors if the $loggedID is words like "justin", but if its only number like 201110523, it works. I don't know what is wrong. The datatype of the id in users is varchar.
public function account_type_student(){
$loggedID = $this->input->post('id');
$query = $this->db->query("SELECT accountType FROM users WHERE id = $loggedID" );
foreach ($query->result() as $row)
{
$query = $row->accountType;
}
if($query=="student"){
return true;
}
else{
return false;
}
}
When it is a word / string like "justin" then you have to escape your variable:
$query = $this->db->query("SELECT accountType FROM users WHERE id = '$loggedID' " );
Or use the active pattern syntax:
$this->db->select('accountType');
$this->db->where('id', $loggedID);
$query = $this->db->get('users');
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I new to oop and I have a image displaying when I had a single a array query (requesting the image url only) but when I added a second dimension to the query array (image id in the table) I just ended up getting a warning message 'Warning: Illegal string offset'. I have used mysqli_fetch_assoc in the database class and then running the result through a foreach loop.
This is the message I am getting.
Thanks in advance for any help given.
page code
$user = $_SESSION['user_name'];
$query = "SELECT gallery_image, 'gallery_id' FROM gallery WHERE user_name = '$user' ORDER BY gallery_id DESC";
$result = $obj_db->get_gallery($query);
foreach ($result as $image ) {
foreach ($image as $key => $value) {
echo '<div class="gallery_item"><img src="'.$value['gallery_image'].'"></div>';
}
}
Database class code
function get_gallery($sql){
$obj_res = mysqli_query($this->obj_db_conn, $sql);
if(mysqli_errno($this->obj_db_conn)){
die ("Failed query: $strSql".$this->obj_db_conn->error);
}
$arResults = array();
while($arRow = mysqli_fetch_assoc($obj_res)){
$arResults[] = $arRow;
}
return $arResults;
If your var_dump output on $result give this,
array(2) {
[0]=> array(2) {
["gallery_image"]=> ""
["gallery_id"]=> ""
}
[1]=> array(2) {
["gallery_image"]=> ""
["gallery_id"]=> ""
}
}
you don't need two nested foreach loops in your page code. The one foreach loop could be enough.
$query = "SELECT gallery_image, gallery_id FROM gallery WHERE user_name = '$user' ORDER BY gallery_id DESC";
$result = $obj_db->get_gallery($query);
foreach ($result as $image ) {
echo '<div class="gallery_item"><img src="'.$image['gallery_image'].'"></div>';
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've set up a Database class, a User class and a UserTools class to interact with a MySQL database. It seems that my UPDATE, DELETE and INSERT commands are working find but I can't figure out what's going on with my SELECT statements. I'm only using mysql right not until I get everything working as I'd like, then I'll switch over to mysqli or PDO. But anyway, here's what I'm looking at....
DB class....
public function select($table, $where) {
$sql = "SELECT * FROM $table WHERE $where";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 1)
return $this->processRowSet($result, true);
return $this->processRowSet($result);
}
public function processsRowSet($rowSet, $singleRow=false) {
$resultArray = array();
while ($row = mysql_fetch_assoc($rowSet)) {
array_push($resultArray, $row);
}
if ($single_row === true)
return $resultArray[0];
return $resultArray;
}
UserTools class
public function getUser($id) {
$db = new DB();
$result = $db->select('users', 'id = $id');
return new User($result);
}
There seems to be an issue with how I'm processing the rows or something. I've followed similar setups with UPDATE,DELETE,INSERT and they seem to work fine but I don't know whats going on here.
If I call an instance of new UserTools(), then try to run the getUser() function, it's not returning anything the way it's set up. If I keep the result from being pushed through the User class constructor, it just returns a Reference Value, meaning that I'm not processing the rows properly.
I have a feeling I'm overlooking something stupid here so any help would be greatly appreciated. Thanks!
For a start,
$result = $db->select('users', 'id = $id');
Should be
$result = $db->select('users', 'id = '.$id);
As Casimir mentioned, there's a typo in public function processsRowSet
I'd echo $sql; die; to check if the query is complete.
In UserTools class: 'id = $id' wouldn't parse in $id. Instead do "id = {$id}" or similar so that it can parse $id.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
There is an error in my code but the code works perfectly. I mean all the values are inserted in the database but there is an error like this on the screen:
Severity: Notice
Message: Array to string conversion
Filename: models/some_model.php
Line Number: 106
This is my code:
View:
<?php foreach($app as $row){
echo "<tr><td><input type=checkbox name=appname[] value='".$row->app_name."'/>".$row->id."</td><td>".$row->app_name."</td><tr>".
?>
Controller:
public function hide(){
$this->load->model('some_model');
$visi = $this->input->post('appname');
$success = $this->some_model->hideApp($visi);
foreach($visi as $key=>$value)
{
$success = $this->some_model->hideApp($visi[$key]);
}
if($success == TRUE)
$this->hideApp_page(TRUE);
else $this->hideApp_page(FALSE);
}
Model:
public function hideApp($visi){
$visi = $this->db->escape_str($visi);
$queryStr = "UPDATE appwarehouse.application_table SET visibility='hidden' where app_name='$visi';"; /* this is line 106*/
$query = $this->db->query($queryStr);
return $query;
}
$visi is array like [1,2,3,4]
when you put $visi in hideApp()
it will be display "array to string error"
so maybe you can remove this line $success = $this->some_model->hideApp($visi);
you have already do some_model->hideApp($visi[$key]) in the foreach loop
so i don't know why you write this$success = $this->some_model->hideApp($visi);
if you still want to run $success = $this->some_model->hideApp($visi);
you have to put $visi into a string
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Im having a strange issue with a select with PDO, so I came here to ask for your help.
I have this code below and Im getting this error:
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number:
parameter was not defined in `$verifyUser->execute();`
Somebody there have an ideia why this can be happening?
My Php Code:
if(!$_SESSION['result'])
{
header('Location: index.php');
}
else
{
$userId = $_SESSION['result']['id'];
$verifyUser = $pdo->prepare("SELECT * FROM aadmins where id = :userId");
$verifyUser->bindValue(":id", $userId);
$verifyUser->execute();
$num_rows = $verifyUser->rowCount();
$result = $verifyUser->fetch(PDO::FETCH_ASSOC);
}
You are using :userId in SQL query, while in bindValue you are using :id.
$verifyUser = $pdo->prepare("SELECT * FROM aadmins where id = :userId");
$verifyUser->bindValue(":id", $userId);
But it should be the same in query and bindvalue.
$verifyUser = $pdo->prepare("SELECT * FROM aadmins where id = :id");
$verifyUser->bindValue(":id", $userId);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I' am running into a problem where an array is not being able to shuffle. I want the array to randomized for each page loads. I followed the documentation but something still wrong over here
Error Message
Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\projects\snppets\sidebar.php on line 65
This is the code that gets the friends list, makes the sting into array, counts the array and shuffles it.
$friends = get_friends_IDS($profile_id);
$friends = make_string_to_ARRAY($friends);
$friends_count = count($friends);
$friends = shuffle($friends);
This is the code that is in line 65
<?php
foreach($friends as $id){
$sql = "SELECT profile_id, profile_photo, profile_username, profile_name FROM profile WHERE profile_id='$id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
if($db->NUM_ROWS() > 0){
foreach($rows as $row){
$friends_profile_id = $row->profile_id;
$friends_profile_photo = $row->profile_photo;
$friends_profile_username = $row->profile_username;
$friends_profile_name = $row->profile_name;
}
}
}
?>
This function gets the friends list from the database
function get_friends_IDS($profile_id){
global $db;
$sql = "SELECT profile_id, profile_friends FROM profile WHERE profile_id='$profile_id' LIMIT 1";
$query = $db->SELECT($sql);
if($db->NUM_ROWS()){
foreach($db->FETCH_OBJECT() as $row){
return $row->profile_friends;
}
}
}
This is the function makes a string into array
function make_string_to_ARRAY($array){
$array = explode(',', $array);
return $array;
}
When I remove the shuffle then it works fine...
shuffle() returns boolean value.
So $friends = shuffle($friends); makes $friends as a boolean variable.
just use shuffle($friends); to shuffle it