I have a mySQL database table setup called site.
Rather than qet the column headings to produce a PHP object of values I want to produce data from the rows.
TABLE: site
:::field:::::value:::::
url www.example.com
name example site
etc Example Etcetera
So I want to be able to get this information from the server by calling the column name I want and the row I am after. I want to do this for all fields in site as I don't want to do multiple calls for the various different rows in site; I'd rather store all the information from the beginning in the object:
eg. <? echo $site['url']; ?>
I created this put it appears to be causing an error:.
$sql = "SELECT * FROM `site`";
$site[];
foreach($sodh->query($sql) as $sitefield){
$site[$sitefield['field']] = $sitefield['value'];
}
Obviously I've missed something. ¿Any idea as to what?
$site[];
should be
$site = array();
Related
I have a database with a table called translations. It has several columns, but I am trying to create a session array to house a variale:translations named array.
I've tried the below code, but I must be missing something... my end goal is to populate much of the static verbiage of a website using $_SESSION['TRANSLATIONS']['Userboards'] for example to populate the names of otherwise static content into any language supported based on the database.
$querylang = "SELECT variable,translation FROM translations left outer join languages on languages.abbrev = translations.fkabbrev WHERE languages.abbrev = 'EN'";
$sqllang = mysql_query($querylang);
while($reslang = mysql_fetch_array($sqllang)){
$_SESSION['TRANSLATIONS'][$reslang['variable']] = $reslang['translation'];
};
The above code is actually correct. The reason it didn't work was a separate issue. Thanks to #Epodax for reminding me to check the errorlog!
I'm developping a website where there are ajax request to get data from a mySQL database.
To simplify the problem, let's say I have the following database structure :
Table OBJECT
ID_OBJECT (int)
NAME_OBJECT (varchar)
Table IMAGE
ID_OBJECT (int) -> foreign key from OBJECT
URL (varchar)
So every object can have between 0 and n images attached to it.
I'm stuck at writing the php script that request the data.
To simplify again, let's say I want to get all objects with all the images they have (but in the future I'll want to get any specific object with all its picture too)
I have written this PHP script :
$sql = "select
ID_OBJECT,
NAME_OBJECT,
URL
from
OBJECT,
IMAGE
where
OBJECT.ID_OBJECT = IMAGE.ID_OBJECT";
$statement=$db->prepare($sql);
$statement->execute();
$result=$statement->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);
The problem I have is as soon as I add "URL", "IMAGE" and the where statement in the sql query, it doen"t echo anything.(I have several objects and images in my databases with the propers ids). I don't understand where I'm making a mistake here.
ID_OBJECT is in both tables, so MySQl doesn't know which one to show. Try specifying like this:
select
OBJECT.ID_OBJECT,
OBJECT.NAME_OBJECT,
IMAGE.URL
from
OBJECT,
IMAGE
where
OBJECT.ID_OBJECT = IMAGE.ID_OBJECT
Obviously you have an error in your query. You should set what ID_OBJECT field you want - from first or second table.
Rewrite your query like:
select
o.ID_OBJECT,
o,NAME_OBJECT,
i.URL
from
OBJECT o,
IMAGE i
where
o.ID_OBJECT = i.ID_OBJECT
In FileMaker I have (a.o.) two related tables, where Betreuer_id of table "studentpaper" refers to id of table "Betreuer".
With help of FileMaker's PHP API, I want to access a record in "studentpaper" including the releated fields. Howerver, the later poses a problem. Consider the following PHP code:
$findCommand =& $fm->newFindAllCommand('studentpaper');
$result = $findCommand->execute();
$records = $result->getRecords();
$record = $records[0];
echo $record->getField('Titel'); // okay
echo $record->getField('Betreuer_id'); // okay
echo $record->getField('Betreuer::Name');
// ERROR: get empty string, even related record has a non-empty field
I have expected for "Betreuer::Name" the correct related result, as usually in FileMaker (and as I get in studentpaper's layout). However, I get only an empty string.
What am I doing wrong? Does the relation in FileMaker's PHP-API differs from the "usual" FileMaker approach?
In case anybody is interested, I found the solution on my own.
The problem was that the field wasn't defined (in the layout) as normal text input, but as selection field resticted by a value list.
Thus, one needs to access the value not via the usual getField function but by one of the functions related to value lists. In my case, getValueListTwoFields did the job.
I'm trying to use the Joomla framework to make a create in the main content table.[http://docs.joomla.org/How_to_use_the_JTable_class] This works fine except that some data comes from posted variables and some from logic that happens when a file is uploaded moments before (store the random image name of a jpg)
$data=&JRequest::get('post');
this takes a ref to the posted values and I want to add to this Array or Object my field. The code I have makes the new record but the column images, doesnt get my string inserted.
I am trying to do something like$data=&JRequest::get('post');
$newdata=(array)$data;
array_push($newdata,"images"=>"Dog");
i make newdata as data is a ref to the posted variables and i suspect wont there fore allow me to add values to $data.
I'm a flash guy normally not a php and my knowledge is letting me down here.
Thanks for any help
Right, first thing:
$data=&JRequest::get('post');
$data is an array, you do not have to cast it. To add another element to the array as described in the comments do this:
$data['images'] = 'cats';
If you are using normal SQL to do the insert then you would do something like this to get the last inserted id e.g. the id of the row you just inserted:
$db = $this->getDBO();
$query = 'Some sql';
$db->setQuery($query);
if (!$db->query()) {
JError::raiseWarning(100, 'Insert failed - '.$db->getErrorMsg());
}
$id = $db->insertid();
If you are developing in Joomla I suggest you use the db functions provided to you rather than mysql_insert_id();
[EDIT]
If you want to use store then you can get the last inserted id like so:
$row->bind($data);
$row->check();
$row->store();
$lastId = $row->id;
I'm trying to display the data here in order of:
Author Name
Book Name
Url
NOTE: There are many results for each piece of data. Im not sure how they are stored in the array when they are fetched.
The database schema is relational as you will see and connects these bits of information from different areas of the database.
Im new to programming as you may have figured.
Im at a loss here.
Here is my code:
<?php
//Starting session
session_start();
//Includes mass includes containing all the files needed to execute the full script
//Also shows homepage elements without customs
include ('includes/mass.php');
//Set the session variable
$username = $_SESSION['username'];
//Check to see if logged in
if (isset($username))
{
//Check all databases assoc with the notes for username submissions
$sql_for_username_submission = "SELECT notes.authname, notes.bookname, notes.url, notes.note_id, notes.user, smallnotes.chapter_name FROM notes INNER JOIN small_notes ON notes.note_id = small_notes.notes_id AND notes.user = small_notes.user ORDER BY notes.note_id";
$get_data = mysql_query($sql_for_username_submission);
while ($data_row = mysql_fetch_assoc($get_data))
{
$authnames = $data_row['authname'];
Stopped here. not sure how to progress
}
}
?>
I would imagine you need some UI controls to which you would bind the data in data_row. In other words, you need to have some placeholders on the screen.
Best Regards
You are fetching my assosciative array so it does not matter. You can just reference the array item by key and use it whenever and where ever you want.
You do not have to worry about how the array is sorted.