PHP - Getting Data From Mysql And Then Converting It To Json With Php
Hi,
I have a problem with some code not working. I need it to get the data from a mysql database and then export it as a json array in the format of id , title, author, date, imageUrl, text. (Please not that these are all variables ) PHP CODE: Code: [Select] <?php $link = mysql_connect('localhost', 'root', ''); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $arr = array(); $rs = ("SELECT `id`, `title`, `author`, `date`, `imageUrl`, `text` FROM `items`"); while($obj = mysql_query($rs)) { $arr[0] = $obj->id; $arr[1] = $obj->title; $arr[2] = $obj->author; $arr[3] = $obj->date; $arr[4] = $obj->imageUrl; $arr[5] = $obj->text; } echo '{"items":'.json_encode($arr).'}'; ?> Thanks For Your Help borden0108 Similar TutorialsI can obtain either one or a collection of objects by executing the following. $product = $entityManager->find('Product', $id); $products = $entityManager->getRepository('Product')->findAll(); All products have many common properties, however, based on the type of product there are some differences. To implement this, I am using class table inheritance (side question - is there any difference between CTI and supertype/subtype modeling?). I will need to convert either a single product to JSON as well as a collection of products to JSON. When converting a collection, only a subset of the product's properties is desired, however, again most of these properties are common to each product type but there are some differences. One solution is to utilize the JsonSerializable interface. A shortcoming, however, is how to deal with both the detail view with all the properties and the collection view with only a subset of properties. To overcome this, I can potentially use jsonSerialize() to provide the detail view, and add another function index() to provide the subset results for the collection. Since this index() function will not recursively incorporate sub-objects, I will need to manually loop over the collection, however, it should not be too big of issue. Or should the entity not be responsible to convert to JSON? I am kind of mixed on this issue, but do agree using a separate class has merit. So, if a separate class is used, how does it deal with each subclass of Product which might have unique properties and getters? I suppose I can have the entities implement an interface which requires them to return their applicable properties, but doing so isn't very flexible and isn't this (almost) the same as having the entity responsible to provide the JSON? I "could" make this other class perform some switch operation based on the class name, but doing so goes against all good OOP principles. What is the "right" way to do this? Thanks Hi, I'm new to PHP and have dabbled a little but need some help or someone to point me in the right direction if possible. I have an ajax web-form that sends the inputed data to a php file and then emails the results to the designated address (all working fine). However, what I am looking to do is have the php file (in addition to the current function) convert the data into XML and then attach the XML file to the email that is sent out. As mentioned I'm new to php and have a small amount of knowledge so any help on this would be very much appreciated. Cheers Hi, Anybody can help me to extract data from son using php
{"location":{"woeid":2293962,"city":"Kasaragod","region":" KL","country":"India","lat":12.5174,"long":74.990662,"timezone_id":"Asia/Kolkata"},"current_observation":{"wind":{"chill":80,"direction":338,"speed":11.81},"atmosphere":{"humidity":70,"visibility":10.0,"pressure":29.8,"rising":0},"astronomy":{"sunrise":"6:54 am","sunset":"6:35 pm"},"condition":{"text":"Clear","code":31,"temperature":80},"pubDate":1549638000},"forecasts":[{"day":"Fri","date":1549564200,"low":76,"high":85,"text":"Sunny","code":32},{"day":"Sat","date":1549650600,"low":76,"high":85,"text":"Sunny","code":32},{"day":"Sun","date":1549737000,"low":75,"high":85,"text":"Sunny","code":32},{"day":"Mon","date":1549823400,"low":76,"high":87,"text":"Mostly Sunny","code":34},{"day":"Tue","date":1549909800,"low":76,"high":89,"text":"Sunny","code":32},{"day":"Wed","date":1549996200,"low":77,"high":88,"text":"Partly Cloudy","code":30},{"day":"Thu","date":1550082600,"low":76,"high":89,"text":"Mostly Sunny","code":34},{"day":"Fri","date":1550169000,"low":76,"high":86,"text":"Sunny","code":32},{"day":"Sat","date":1550255400,"low":76,"high":85,"text":"Sunny","code":32},{"day":"Sun","date":1550341800,"low":74,"high":85,"text":"Sunny","code":32}]}
How to extract data of "current_observation"
Thanks I am also trying to extract the "address_components" from the JSON data, but the array index changes if you do not submit an address line for example. I am trying to get the "sublocality" and "administrative_area_level_1" from the "address_components" below. The API sample is as follows: { "results" : [ { "access_points" : [], "address_components" : [ { "long_name" : "10", "short_name" : "10", "types" : [ "street_number" ] }, { "long_name" : "Gillespie Street", "short_name" : "Gillespie St", "types" : [ "route" ] }, { "long_name" : "South Beach", "short_name" : "South Beach", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Durban", "short_name" : "Durban", "types" : [ "locality", "political" ] }, { "long_name" : "Durban Metro", "short_name" : "Durban Metro", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "KwaZulu-Natal", "short_name" : "KZN", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "South Africa", "short_name" : "ZA", "types" : [ "country", "political" ] }, { "long_name" : "4001", "short_name" : "4001", "types" : [ "postal_code" ] } ], "formatted_address" : "10 Gillespie St, South Beach, Durban, 4001, South Africa", "geometry" : { "location" : { "lat" : -29.859728, "lng" : 31.039773 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : -29.8583790197085, "lng" : 31.0411219802915 }, "southwest" : { "lat" : -29.8610769802915, "lng" : 31.0384240197085 } } }, "place_id" : "ChIJGRCdW0mo9x4RcIwU_7S1xa8", "plus_code" : { "compound_code" : "42RQ+4W Durban, South Africa", "global_code" : "5G2H42RQ+4W" }, "types" : [ "street_address" ] } ], "status" : "OK" } If you submit a full address to the API, you can use the hard coded index like below: [7] $json_a['results'][0]['address_components'][7]['long_name'] But it is not reliable to hard code the index [7] in this case. So I tested some sample code from above post, it is working, but in some cases it does not return the "long_name" of the "sublocality" or "administrative_admin_level_1": /* if no address, then the array index change */ // $json_a['results'][0]['address_components'][7]['long_name'] $area = array(); foreach ($json_a['results'][0]['address_components'] as $addrObj) { foreach($addrObj['types'] as $type) { if (substr_count($type, 'sublocality')>0) { $area['short_name'] = $addrObj['short_name']; $area['long_name'] = $addObj['long_name']; // var_dump($addrObj); } if (substr_count($type, 'administrative_area_level_1')>0) { $area['short_admin'] = $addrObj['short_name']; $area['long_admin'] = $addObj['long_name']; var_dump($addrObj); } } } In the var_dump() I do get the long_name value though: In this sample it must be "Kwazulu-Natal", but from the code above, it returns a NULL value. array(3) { ["long_name"]=> string(13) "KwaZulu-Natal" ["short_name"]=> string(3) "KZN" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } Below is the full script I am trying to get to work. The lon, lng and format name is working 100%, it is just this array index problem. /* get google result in JSON*/ $googleString = file_get_contents($googleUrl); // get json content $json_a = json_decode($googleString, true); //json decoder // response status will be 'OK', if able to geocode given address if($json_a['status']=='OK'){ // get the important data $lati = isset($json_a['results'][0]['geometry']['location']['lat']) ? $json_a['results'][0]['geometry']['location']['lat'] : ""; $longi = isset($json_a['results'][0]['geometry']['location']['lng']) ? $json_a['results'][0]['geometry']['location']['lng'] : ""; $formatted_address = isset($json_a['results'][0]['formatted_address']) ? $json_a['results'][0]['formatted_address'] : ""; /* if no address, then the array index change */ // $json_a['results'][0]['address_components'][7]['long_name'] $area = array(); foreach ($json_a['results'][0]['address_components'] as $addrObj) { foreach($addrObj['types'] as $type) { if (substr_count($type, 'sublocality')>0) { $area['short_name'] = $addrObj['short_name']; $area['long_name'] = $addObj['long_name']; // var_dump($addrObj); } if (substr_count($type, 'administrative_area_level_1')>0) { $area['short_admin'] = $addrObj['short_name']; $area['long_admin'] = $addObj['long_name']; var_dump($addrObj); } } } Is there a better way to get the "sublocality" and "administrative_area_level_1" from the array $area array above. Any pointers will be much appreciated. So when I load my paginator it returns No Results Found. 1) My query is only returning 1 array entry, should be hundreds, maybe I'm not builing it correctly, 2) There is at least 1 array set but its still saying data not found. Heres the template array: Code: [Select] array('id'=>'0', 'name'=>'xmlqoyzgmykrphvyiz', 'date'=>'13-Sep-2002', 'price'=>'8370', 'number'=>'8056', 'address'=>'qdfbc', 'company'=>'taufrid', 'desc'=>'pppzhfhcdqcvbirw', 'age'=>'5512', 'title'=>'zticbcd', 'phone'=>'hvdkltabshgakjqmfrvxo', 'email'=>'eodnqepua', 'zip'=>'eodnqepua', 'country'=>'pdibxicpqipbsgnxyjumsza'), array('id'=>'1', 'name'=>'rbdmbabficcre', 'date'=>'10-Sep-2004', 'price'=>'3075', 'number'=>'3627', 'address'=>'oxcm', 'company'=>'xyzwzv', 'desc'=>'rwndyoedxh', 'age'=>'2134', 'title'=>'lxxyfgdtdffjce', 'phone'=>'zeejvbwy', 'email'=>'ldcikhxwfuulaxeedkogpxftb', 'zip'=>'ldcikhxwfuulaxeedkogpxftb', 'country'=>'pcmobxrdfclcyrx'), array('id'=>'2', 'name'=>'yr', 'date'=>'04-Mar-2007', 'price'=>'7129', 'number'=>'6614', 'address'=>'i', 'company'=>'gcpvrshftfxxlz', 'desc'=>'nyalrdjjl', 'age'=>'4728', 'title'=>'ddfl', 'phone'=>'mnhifzqltvirgiaug', 'email'=>'f', 'zip'=>'f', 'country'=>'epipbmtfsfxetenyedjxzsog'), etc Here is my arry: Code: [Select] $echoarray = array(); $resultsql = mysql_query("SELECT * FROM clients")or die(mysql_error()); while($row = mysql_fetch_array($resultsql)){ $echoarray['id'] = $row['ID']; $echoarray['name'] = $row['First_Name'] . " " . $row['Last_Name']; $echoarray['price'] = $row['Status']; $echoarray['number'] = $row['Sex']; $echoarray['address'] = $row['Phys_Street']; $echoarray['company'] = $row['Agency']; $echoarray['desc'] = $row['Notes']; $echoarray['age'] = $row['Date_Birth']; $echoarray['title'] = $row['Occupation']; $echoarray['phone'] = $row['Phone']; $echoarray['email'] = $row['Email']; $echoarray['zip'] = $row['Phys_Zip']; $echoarray['country'] = $row['Phys_City']; } Here is the template return: Code: [Select] function initArray() { return array( array('id'=>'0', 'name'=>'xmlqoyzgmykrphvyiz', 'date'=>'13-Sep-2002', 'price'=>'8370', 'number'=>'8056', 'address'=>'qdfbc', 'company'=>'taufrid', 'desc'=>'pppzhfhcdqcvbirw', 'age'=>'5512', 'title'=>'zticbcd', 'phone'=>'hvdkltabshgakjqmfrvxo', 'email'=>'eodnqepua', 'zip'=>'eodnqepua', 'country'=>'pdibxicpqipbsgnxyjumsza'), array('id'=>'1', 'name'=>'rbdmbabficcre', 'date'=>'10-Sep-2004', 'price'=>'3075', 'number'=>'3627', 'address'=>'oxcm', 'company'=>'xyzwzv', 'desc'=>'rwndyoedxh', 'age'=>'2134', 'title'=>'lxxyfgdtdffjce', 'phone'=>'zeejvbwy', 'email'=>'ldcikhxwfuulaxeedkogpxftb', 'zip'=>'ldcikhxwfuulaxeedkogpxftb', 'country'=>'pcmobxrdfclcyrx'), array('id'=>'2', 'name'=>'yr', 'date'=>'04-Mar-2007', 'price'=>'7129', 'number'=>'6614', 'address'=>'i', 'company'=>'gcpvrshftfxxlz', 'desc'=>'nyalrdjjl', 'age'=>'4728', 'title'=>'ddfl', 'phone'=>'mnhifzqltvirgiaug', 'email'=>'f', 'zip'=>'f', 'country'=>'epipbmtfsfxetenyedjxzsog'), array('id'=>'3', 'name'=>'bhqggvwolybfdtk', 'date'=>'26-Dec-2000', 'price'=>'1867', 'number'=>'4288', 'address'=>'jo', 'company'=>'goevufkvmbct', 'desc'=>'zhixinabyazbfleozrvovr', 'age'=>'3423', 'title'=>'b', 'phone'=>'odhh', 'email'=>'g', 'zip'=>'g', 'country'=>'idxvdztezvkkaz')); } And Mine: Code: [Select] function initArray() { return $echoarray; } Where am I going wrong here? <?php ini_set('display_errors', 0); function escapeArray($array) { foreach ($array as $key => $val) { if(is_array($val)){ $array[$key]=escapeArray($val); } else{ $array[$key]=addslashes($val); } } return $array; } $request_type=$_SERVER['REQUEST_METHOD']; $api_key=$_SERVER['HTTP_X_API_KEY']; $res=array(); if($api_key!=="643256432"){ $res['msg']="Failu Invalid API KEY"; echo json_encode($res); die; } // Connects to the orcl service (i.e. database) on the "localhost" machine //$conn = oci_connect('SCOTT', 'admin123', 'localhost/orcl'); $conn = oci_connect('test', 'test', '192.168.10.43/test.test.com'); if (!$conn) { $e = oci_error(); trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); } $request=file_get_contents("php://input"); $request=escapeArray(json_decode($request,true)); print_r($request); // die; if($request_type=="POST"){//for creation of invoice echo $CONTACT_ID=isset($request['CONTACT_ID'])?$request['CONTACT_ID']:""; $INV_SERIAL_NO=isset($request['INV_SERIAL_NO'])?$request['INV_SERIAL_NO']:""; $NAME=isset($request['NAME'])?$request['NAME']:""; $INV_DATE=isset($request['INV_DATE'])?$request['INV_DATE']:""; $DUE_DATE=isset($request['DUE_DATE'])?$request['DUE_DATE']:""; $CURRENCY=isset($request['CURRENCY'])?$request['CURRENCY']:""; $SUBTOTAL=isset($request['SUBTOTAL'])?$request['SUBTOTAL']:""; $TAX_TOTAL=isset($request['TAX_TOTAL'])?$request['TAX_TOTAL']:""; echo $SHIP_SERIAL_NO=isset($request['SHIP_SERIAL_NO'])?$request['SHIP_SERIAL_NO']:""; $MASTER_NO=isset($request['MASTER_NO'])?$request['MASTER_NO']:""; $HOUSE_NO=isset($request['HOUSE_NO'])?$request['HOUSE_NO']:""; $shipment_data=isset($request['shipment_data'])?$request['shipment_data']:""; if($CONTACT_ID==""){ $res['msg']="CONTACT_ID is required"; } else if($INV_SERIAL_NO==""){ $res['msg']="INV_SERIAL_NO is required"; } else if($NAME==""){ $res['msg']="NAME is required"; } else if($INV_DATE==""){ $res['msg']="INV_DATE is required"; } else if($DUE_DATE==""){ $res['msg']="DUE_DATE is required"; } else if($CURRENCY==""){ $res['msg']="CURRENCY is required"; } else if($SUBTOTAL==""){ $res['msg']="SUBTOTAL is required"; } else if($TAX_TOTAL==""){ $res['msg']="TAX_TOTAL is required"; } else if($MASTER_NO==""){ $res['msg']="MASTER_NO is required"; } else if($HOUSE_NO==""){ $res['msg']="HOUSE_NO is required"; } else if($SHIP_SERIAL_NO==""){ $res['msg']="SHIP_SERIAL_NO is required"; } else{ $stid = oci_parse($conn, "Select * from FL_HDR_INVOICE where CONTACT_ID='$CONTACT_ID'"); (oci_execute($stid)); oci_fetch_all($stid, $out); if(count($out['CONTACT_ID'])==0){ $stid = oci_parse($conn, "Insert into FL_HDR_INVOICE (CONTACT_ID,INV_SERIAL_NO,NAME,INV_DATE,DUE_DATE,CURRENCY,SUBTOTAL,TAX_TOTAL) Values ('$CONTACT_ID','$INV_SERIAL_NO','$NAME',TO_DATE('$INV_DATE','YYYY-MM-DD'),TO_DATE('$DUE_DATE','YYYY-MM-DD'),'$CURRENCY','$SUBTOTAL','$TAX_TOTAL')"); oci_execute($stid); $stid_2 = oci_parse($conn, "Insert into FL_SHIPMENT_DATA (CONTACT_ID,INV_SERIAL_NO,SHIP_SERIAL_NO,MASTER_NO,HOUSE_NO) Values ('$CONTACT_ID','$INV_SERIAL_NO','$SHIP_SERIAL_NO','$MASTER_NO','$HOUSE_NO')"); oci_execute($stid_2); if(oci_num_rows($stid)>0){ $res['msg']="Invoice created successfully against this contact_id:".$CONTACT_ID; } else{ $res['msg']="Something going wrong please try again later"; } } else{ $res['msg']="contact_id must be unique"; } } echo json_encode($res); die; } I need to read json data inside an array. Please help correct my code.. i am trying to do an API. Hi- I have to post to a URL, grab one field our of the JSON output/response and format nicely to the user. I've tried a few things with some help but none seem to be working. So any help or direction would be greatly appreciate it!! Attempt 1 (works but not formatted nicely) if I post using the browser, it works. the data is sent to the URL (web service) and I can see in the web service logs that the entry has been recorded. the problem is that since i have the URL in "action", i'm taken to that page and the user gets to see the JSON output Code: [Select] //here's the HTML form in offer.php <form action="http://somewebservice.com/<?php echo $PrizeID;?>" method="post" name="myform"> <input name="account" id="account" type="hidden" value="<?php echo $account;?>"/> <input name="dob" id="dob" type="hidden" value="<?php echo $dob;?>"/> <input name="site" id="site" type="hidden" value="<?php echo $site;?>"/> <input id="submit" type="submit" value="submit"/> </form> //here's the output (JSON) after clicking submit {"Prize":"XXXXXX","ConfirmationCode":"######","Error":false,"ErrorMsg":null} Attempt 2 (doens't work) i tried using php but after submit, the page refreshes and no entry is recorded in the web service log Code: [Select] //here's the HTML form in offer.php <form name="myform"> <input name="account" id="account" type="hidden" value="<?php echo $account;?>"/> <input name="dob" id="dob" type="hidden" value="<?php echo $dob;?>"/> <input name="site" id="site" type="hidden" value="<?php echo $site;?>"/> <input id="submit" type="submit" value="submit"/> //here's the php in offer.php <?php if(isset($_POST['submit'])){ $options = array( 'http'=>array( 'method'=>"POST", 'content'=>http_build_query(array( 'account' => $account, 'dob' => $dob, 'site' => $site )) )); $context = stream_context_create($options); $result = file_get_contents("http://somewebservice.com/{$PrizeID}",NULL,$context); var_dump($result); } ?> Last attempt (doesn't work) i tried using php & curl so that i can process in the back and show only the ConfirmationCode to the user in a nice format but it doesn't work. after submit, i'm taken to process.php where i see nothing and no entry is recorded in the web service log Code: [Select] //here's the HTML form in offer.php <form action="process.php" method="post" name="myform"> <input name="account" id="account" type="hidden" value="<?php echo $account;?>"/> <input name="dob" id="dob" type="hidden" value="<?php echo $dob;?>"/> <input name="site" id="site" type="hidden" value="<?php echo $site;?>"/> <input id="submit" type="submit" value="submit"/> </form> //here's the PHP in process.php <?php $postvars=file_get_contents("php://input"); $curl=curl_init("http://somewebservice.com/{$PrizeID}"); curl_setopt($curl,CURLOPT_POSTFIELDS,$postvars); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result=curl_exec($curl); $decodedresult=json_decode($result,true); ?> i haven't gotten to the point where i grab the ConfirmationCode I want because i want to get this fixed first and also because i can't get that to work but will focus on that once i fix this. Thanks. I am retrieving Google Books info in JSON format and displaying it inside a div. I would like to send the contents of this div (name, title, description) to my database using Ajax.
Currently only the ISBN field sends because I have it declared as a variable. However my question is, how do I send the other fields (name, title, author). How do I declare these also, I'm not sure what format they need to be in etc.
My JS
$(document).ready(function() { $('#submit').click(function(ev) { ev.preventDefault(); var isbn = $('#isbn_search').val(); //get isbn direct from input var url='https://www.googleapis.com/books/v1/volumes?q='+isbn; $.getJSON(url,function(data){ $.each(data.items, function(entryIndex, entry){ var html = '<div class="results well">'; html += '<h3>' + entry.volumeInfo.title + '</h3>'; html += '<div class="author">' + entry.volumeInfo.authors + '</div>'; html += '<div class="description">' + entry.volumeInfo.description + '</div>'; }); }); }); });My Ajax; $.ajax({ type: 'POST', url: 'addIsbnScript.php', data: { 'isbn' : isbn, 'title' : title 'subtitle' : subtitle, 'authors' : authors, 'description' : description }, success: function () { $.growl({ message: " Record added" }); } });Note, if i manually set the vars like below they all do successfully send to my database, so I know my query is working ok var title = "some text", var author = "some text", Var description = "some text"Thanks in advance for any help, newbie here (incase it wasn't obvious!). J I am retrieving data from a third party's API using AJAX method. I would like to do two things. 1. Display the data records on a page. 2. Create a pagination on the page to display records more efficiently. I am expecting to retrieve possibly hundreds of records. I can normally do this using PHP but since I am retrieving the records using AJAX, the pagination is gonna be a challenge. First things first is to display the records. Here is my AJAX code. It displays the log data fine. But it doesn't display any data in the "output" div. And it gives this error in the console. Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) at Object.success <div class="output"></div> <script> $.ajax ({ type: 'GET', url: "https://3rdpartywebsite.com/api/GetCustomers", dataType: 'json', processData: false, contentType: false, success: function(data) { console.log(data) $newData = JSON.parse(data); $.each($newData, function(i, v) { $('.output').append(v.LastName); $('.output').append(v.FirstName); }); } }); </script>
What am I doing wrong? Edited November 14, 2020 by imgroootI want to print data from response json? I was debug with firebug and show this :
{"umumu":"","levelu":1,"nameu":""}How to echo data array just levelu. But i don't know how to make it? thanks Hello (I think I could have posted this in the wrong PHP area! - so posting it here),
I'm pretty new at building websites using php (and mysql) and was most recently given the task to create a database image gallery, which was to be accessed through a php website.
I made a full site which allowed me to upload said images & it worked perfectly. However after doing my last checks I have been told that mysql is deprecated and that I need to use mysqli.
I've had a look at some tutorials on websites to help direct me but it's simply confusing me more and more each time I look at it.
Is it possible I am over thinking this and there is an easier way to approach it?
Thank you kindly.
This is my php code:
help required for noobie(no hair left) all i want to be able to do is use the individual elements of the array in javascript eg companyname or companyimage but just getting my head in my hand. mysql_select_db("herefordinfo") or die(mysql_error()); echo "Connected to Database"; $dbQuery = 'SELECT * FROM companies'; $dbResult = mysql_query($dbQuery) or die ("Could not read data because " . mysql_error()); $RowCount = mysql_numrows($dbResult); while ($pubs = mysql_fetch_assoc($dbResult)) { $array[] = "{ {$pubs['id']}, {$pubs['companyname']}, {$pubs['companyinfo']}, {$pubs['companyimage']} }"; } echo 'var pub = [' . implode(', ', $array) . '];'; // this ?> <html> <head> <script type="text/javascript"> var jsArray = <?php echo json_encode($array); ?>; document.write(jsArray); //this </script> </head> <boby></body> </html> both php and javescritp output the results below { 1, lichfield vaults, 10 church street hereford , lich.jpg },{ 2, Black Lion, bridge street, blacklion.jpg },{ 3, Saracens Head, st martins street, saracens.jpg } This is more of a database architect question.
I am building a CMS for website development. I am ready to use it for the first time on one of my clients. Now, my issue is is how to set up some or one of my table(s) for some areas that my client wants to be able to change in the admin section.
In their website on the front page, I put together this nice block slider. Its 6 blocks with text, a background image and nice effects. At first it was always going to be static, which was no issue at all, but later they came to me and asked if they could be able to change words in the boxes themselves in the admin section..
Of course this can be easily done by creating a table that is associated with this home page block thing and each row in the table will correspond to one of the blocks on the home page.
But I believe there might be an easier way to store this type of data without having to create another table for every feature the client wants to edit, for example they also want to be able to edit a slider on another page.
The WordPress database then came into my mind and how they store most of their data as JSON. So I was then thinking of creating a table named 'modules' for examples with three columns (id, title, data). Then inside the data column store the editable fields as a JSON string object.
For example a row could like like :
1, "Front Page Box Slider", {"slider-home" : [ {"data" : "value", "img_1", "value1"}, {"data" : "value", "img_2", "value2"} ]}
I've been doing a bit of research on this and people have just been saying, never do this if you do want your data to be searched through, which I don't.
Can anyone tell me why or why not I should do this, or maybe a better solution to my problem?
Thank you
Edited by carlosmoreeira, 17 September 2014 - 07:37 PM. I'm a beginner here and i am learning the basic in converting from MySQL to MySQLi. I am currently working on this registration page which I would want to convert to MySQLi. Please advise me how to modify this script, I would prefer the procedural style.
The MySQLi coding is not working because it would notg insert into the database like the MySQL coding would, would appreciate if your can help me.
MYSQL
<?php error_reporting(1); $submit = $_POST['submit']; //form data $name = mysql_real_escape_string($_POST['name']); $name2 = mysql_real_escape_string($_POST['name2']); $email = mysql_real_escape_string($_POST['email']); $password = mysql_real_escape_string($_POST['password']); $password2 = mysql_real_escape_string($_POST['password2']); $email2 = mysql_real_escape_string($_POST['email2']); $address = mysql_real_escape_string($_POST['address']); $address2 = mysql_real_escape_string($_POST['address2']); $address3 = mysql_real_escape_string($_POST['address3']); $address4 = mysql_real_escape_string($_POST['address4']); $error = array(); if ($submit) { //open database $connect = mysql_connect("localhost", "root", "Passw0rd") or die("Connection Error"); //select database mysql_select_db("logindb") or die("Selection Error"); //namecheck $namecheck = mysql_query("SELECT * FROM users WHERE email='{$email}'"); $count = mysql_num_rows($namecheck); if($count==0) { } else { if($count==1) { $error[] = "<p><b>User ID taken. Try another?</b></p>"; } } //check for existance if($name&&$name2&&$email&&$password&&$password2&&$email2&&$address&&$address2&&$address3&&$address4) { if(strlen($password)<8) { $error[] = "<p><b>Password must be least 8 characters</b></p>"; } if(!preg_match("#[A-Z]+#",$password)) { $error[] = "<p><b>Password must have at least 1 upper case characters</b></p>"; } if(!preg_match("#[0-9]+#",$password)) { $error[] = "<p><b>Password must have at least 1 number</b></p>"; } if(!preg_match("#[\W]+#",$password)) { $error[] = "<p><b>Password must have at least 1 symbol</b></p>"; } //encrypt password $password = sha1($password); $password2 = sha1($password2); if($_POST['password'] != $_POST['password2']) { $error[] = "<p><b>Password does not match</b></p>"; } //rescue email match check if($_POST['email2'] == $_POST['email']) { $error[] = "<p><b>Rescue Email must not be the same as User ID</b></p>"; } //generate random code $random = rand(11111111,99999999); //check for error messages if(isset($error)&&!empty($error)) { implode($error); } else { //Registering to database $queryreg = mysql_query("INSERT INTO users VALUES ('','$name','$name2','$email','$password','$password2','$email2','$address','$address2','$address3','$address4','$random','0')"); $lastid = mysql_insert_id(); echo "<meta http-equiv='refresh' content='0; url=Activate.php?id=$lastid&code=$random'>"; die (); } } } ?>MYSQLi (NOT WORKING AFTER CONVERTING) <?php error_reporting(1); $submit = $_POST['submit']; //form data $name = mysqli_real_escape_string($connect, $_POST['name']); $name2 = mysqli_real_escape_string($connect, $_POST['name2']); $email = mysqli_real_escape_string($connect, $_POST['email']); $password = mysqli_real_escape_string($connect, $_POST['password']); $password2 = mysqli_real_escape_string($connect, $_POST['password2']); $email2 = mysqli_real_escape_string($connect, $_POST['email2']); $address = mysqli_real_escape_string($connect, $_POST['address']); $address2 = mysqli_real_escape_string($connect, $_POST['address2']); $address3 = mysqli_real_escape_string($connect, $_POST['address3']); $address4 = mysqli_real_escape_string($connect, $_POST['address4']); $error = array(); if ($submit) { //open database $connect = mysqli_connect("localhost", "root", "Passw0rd", "logindb") or die("Connection Error"); //namecheck $namecheck = mysqli_query($connect, "SELECT * FROM users WHERE email='{$email}'"); $count = mysqli_num_rows($namecheck); if($count==0) { } else { if($count==1) { $error[] = "<p><b>User ID taken. Try another?</b></p>"; } } //check for existance if($name&&$name2&&$email&&$password&&$password2&&$email2&&$address&&$address2&&$address3&&$address4) { if(strlen($password)<8) { $error[] = "<p><b>Password must be least 8 characters</b></p>"; } if(!preg_match("#[A-Z]+#",$password)) { $error[] = "<p><b>Password must have at least 1 upper case characters</b></p>"; } if(!preg_match("#[0-9]+#",$password)) { $error[] = "<p><b>Password must have at least 1 number</b></p>"; } if(!preg_match("#[\W]+#",$password)) { $error[] = "<p><b>Password must have at least 1 symbol</b></p>"; } //encrypt password $password = sha1($password); $password2 = sha1($password2); if($_POST['password'] != $_POST['password2']) { $error[] = "<p><b>Password does not match</b></p>"; } //rescue email match check if($_POST['email2'] == $_POST['email']) { $error[] = "<p><b>Rescue Email must not be the same as User ID</b></p>"; } //generate random code $random = rand(11111111,99999999); //check for error messages if(isset($error)&&!empty($error)) { implode($error); } else { //Registering to database $queryreg = mysqli_query($connect, "INSERT INTO users VALUES ('','$name','$name2','$email','$password','$password2','$email2','$address','$address2','$address3','$address4','$random','0')"); $lastid = mysqli_insert_id(); echo "<meta http-equiv='refresh' content='0; url=Activate.php?id=$lastid&code=$random'>"; die (); } } } ?> hi all i have some json that i want to decode and add to mysql could you give me some help? i need to add each of these as a row here is the json Code: [Select] [ { "DayNumber": 1, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 1, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 1, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 1, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 1, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 2, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 2, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 2, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 2, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 2, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 2, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 83, "Room": "G3" }, { "DayNumber": 3, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:30:00 AM", "DefinitionTimeTo": "9:25:00 AM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 1455, "Room": "G3" }, { "DayNumber": 3, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:25:00 AM", "DefinitionTimeTo": "10:15:00 AM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 3, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:35:00 AM", "DefinitionTimeTo": "11:30:00 AM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 3, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:30:00 AM", "DefinitionTimeTo": "12:15:00 PM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 3, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10C9H1", "ClassDescription": "Chapel", "StaffID": 1071, "Room": "C1" }, { "DayNumber": 3, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "A3" }, { "DayNumber": 4, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 4, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 4, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 4, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 4, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 5, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 5, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 5, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 5, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 5, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 6, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 6, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 6, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10C9H1", "ClassDescription": "Chapel", "StaffID": 1071, "Room": "C1" }, { "DayNumber": 6, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 6, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 6, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 7, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 7, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 7, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 1455, "Room": "G3" }, { "DayNumber": 7, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 8, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:30:00 AM", "DefinitionTimeTo": "9:25:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "S21" }, { "DayNumber": 8, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:25:00 AM", "DefinitionTimeTo": "10:15:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "S21" }, { "DayNumber": 8, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:35:00 AM", "DefinitionTimeTo": "11:30:00 AM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 8, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:30:00 AM", "DefinitionTimeTo": "12:15:00 PM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 8, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 8, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 9, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 83, "Room": "G3" }, { "DayNumber": 9, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 9, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 9, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 9, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 9, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "S23" }, { "DayNumber": 10, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 10, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10F9M1", "ClassDescription": "Form Period", "StaffID": 615792, "Room": "T11" }, { "DayNumber": 10, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 10, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 10, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 10, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" } ] thanks Matt good evening folks, i have a question regarding the foreach syntax when needing to access something say 3 levels deep into the json architecture here's the example I'm trying to accomplish but when echoing it's not returning the value i need from 3 levels deep. I believe i just don't understand the correct syntax so here's the example any help would he sincerely appreciated.
https://pastebin.com/mEBiMUW5 Hi, I have the following code that takes a list from a mySQL db and puts it into an array that shows 'gig date, venue, city' per entry: - Code: [Select] while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $array[] = ($row['date'].', '.$row['gl_venue'].', '.$row['gl_city']); } $output = json_encode($array); this outputs JSON like so: Code: [Select] ["06-05-2011, O'neills, Southend","07-05-2011, Power League, Peterborough","14-05-2011, Queen Victoria hall, Oundle",.......] how do I add keys into the array so I get something like: Code: [Select] {"gigs":[ { "gig": "date, venue, city",} ]} at least I think so anyway!?! What I want to do is eventually be able to parse a JSON listing into Objective-C and be able to call key/pairs into various different parts of an app. I'm pretty new to this so any advice would be great. I have a simple ajax that passes data to a php script using jquery serialize. This is working good but where I am getting stuck is trying to pass json array into mysql. The json is outputting the correct data, but when I insert into db, it just says Array in the field. How can I convert(if that is the right term) json array to use in mysql. In the code I have posted, $box is an array from jquery which I normally use foreach to loop through array. What I need to do is convert $box after is has been encoded. Thanks Code: [Select] $box = $_POST['BRVbrtrv_boxnumber']; $list = array("activity" => $activity, "mobile" => $mobile, "company" => $company, "authorised" => $authorised, "service" => $service, "department" => $department, "address" => $address, "boxcount" => $boxcount, "box" => $box); $c = json_encode($list); echo $c; I implemented the Content-Security-Policy (CSP). I also included the report-uri so it sends a POST request to myserver.com/csp-report.php test.php <?php header("Content-Security-Policy: default-src 'self'; report-uri http://127.0.0.1/csp-report.php"); ?>I attempted to run these two inline scripts in my test.php file. <img src="http://evil.example.com/image.png"> <script>alert('XSS');</script>nothing is saved in the report-uri file! However, using Firebug, the policy is working as it should. Kindly advise if I have the report-uri synthax wrongly. Thank you Edited by terungwa, 20 October 2014 - 09:06 AM. Hello - I am connecting to MySql and running a query. If I use a foreach loop, I can iterate over the results and have them print to screen. However, when I try the below everything is null!
How can I add the results of my MySql query to a php array?
<?php $con=mysqli_connect("site", "user", "psasswr=ord", "db"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="SELECT * FROM test LIMIT 10"; $result = mysqli_query($con,$sql); echo json_encode($result); mysqli_free_result($result); mysqli_close($con); ?>
but what this prints is: {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null} |