PHP echo database result - php

Im trying to store and get HTML data made with tiny mce into database using PHP.
Its giving me this error: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource
Here is what ive tried:
save.php
<?php
include "functions.php";
$data = file_get_contents("php://input");
update_field($data);
show.php
<?php
include "functions.php";
search_field();
functions.php
<?php
function connect(){
return $yhteys = new mysqli("localhost", "root", "", "harjoitus20");
}
function search_field(){
$result = connect()->query('SELECT sisalto FROM Taulu WHERE nimi="harkka"');
while($row = mysql_fetch_object($result)) {
echo $row;
}
}
function update_field($data){
connect()->query('UPDATE Taulu SET sisalto="$data" WHERE nimi="harkka"');
}
Been trying to figure this out for a while now so any help would be appreciated.
Edit: In database it says $data in sisalto but shouldnt there be the content of my textarea?.

try to use echo before connect() , like this
echo connect()->query('UPDATE Taulu SET sisalto="$data" WHERE nimi="harkka"');
it might solve your issue

Hi have you tried:
while($row = mysqli_fetch_assoc($result)) {
echo $row['sisalto'];
}

Related

How to get information from database file, and retrieve that information in the website file?

I'm in the process of doing a "mock" website for a class and I'm having issues getting the information from my database file, to the actual page in my site. I should mention that I'm a total php newb. Also there isn't any security yet.
I'm calling the connection from another file, so I didn't include that.
database function:
function GetProductsByCategory($categoryID) {
global $conn, $productsResult;
echo $categoryID;
$sql = "SELECT * FROM products WHERE prodCategory = '$categoryID'";
$productsResult = $conn->query($sql);
}
Website File:
//In the head of website file
<?php
$categoryID = "87";
if (isset($_GET['category'])) {
GetProductsByCategory($categoryID);
} else {
// Fallback behaviour goes here
}
?>
//In the body
<?php while ($row = $productsResult->fetch_assoc()) {
//Just trying to get information from the database file
echo '<div>'.$row["prodName"].'</div>';
}
?>
I should mention that nothing throws an error, It echos out the category ID at the top, but inside the <div> it just doesn't show anything.
Call the function and save the result in a variable:
$result = GetProductsByCategory($categoryID);
then inside the function:
$productsResult = $conn->query($sql);
return $productsResult;
then your while loop should use this returned result (i.e., $result):
while ($row = $result->fetch_assoc()) { ... }

Call to undefined function sccp_get_model_data()

Hoping someone can help me with a Call to undefined function error I am getting in the following code:
$query = \FreePBX::Database()->query('SELECT model, dns, buttons, loadimage
FROM sccpdevmodel
WHERE dns > 0
ORDER BY model');
$res = $query->fetchAll();
foreach ($res as $row) {
$modelData['model'][] = $row[0];
$modelData['dns'][] = $row[1];
$modelData['buttons'][] = $row[2];
$modelData['loadimage'][] = $row[3];
}
return $modelData;
This first part seems to be ok then I get the error $modelData = sccp_get_model_data(); in this line.
<?php
$modelData = sccp_get_model_data();
$numModels = count($modelData['model']);
$addonData = sccp_get_addon_data();
$numAddons = count($addonData['model']);
?>
Any advice?
Here is a link to the source file if anyone can help please?
https://github.com/Cynjut/SCCP_Manager/tree/master
Make sure you are using an include or require statement to load your functions. I found what seems to be the full code base for this on github and didn't see where it loads in the functions you use.
Not sure if you want them conditionally loaded, but if not, you can include 'functions.inc.php'; at the top of the file that needs to use them.

PHP Notice: Trying to get property of non-object error

I'm using Factory in AngularJS, The Script is
app.factory('GetCountryService', function ($http, $q) {
return {
getCountry: function(str) {
// the $http API is based on the deferred/promise APIs exposed by the $q service
// so it returns a promise for us by default
var url = "https://www.bbminfo.com/sample.php?token="+str;
return $http.get(url)
.then(function(response) {
if (typeof response.data.records === 'object') {
return response.data.records;
} else {
// invalid response
return $q.reject(response.data.records);
}
}, function(response) {
// something went wrong
return $q.reject(response.data.records);
});
}
};
});
My Output Response Screen Shot:
My PHP Script:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
session_start();
$ip = $_SERVER['REMOTE_ADDR'];
$dbname = "xyzData";
$link = mysql_connect("localhost", "Super", "Super") or die("Couldn't make connection.");
$db = mysql_select_db($dbname, $link) or die("Couldn't select database");
$uid = "";
$txt = 0;
$outp = "";
$data = file_get_contents("php://input");
$objData = json_decode($data);
if (isset($objData->token))
$uid = mysql_real_escape_string($objData[0]->token, $link);
else if(isset($_GET['uid']))
$uid = mysql_real_escape_string($_GET['uid']);
else
$txt += 1;
$outp ='{"records":[{"ID":"' . $objData->token . '"}]}';
echo($outp);
?>
I got error_log message is
[18-Mar-2016 21:40:40 America/Denver] PHP Notice: Trying to get
property of non-object in /home/sample.php
I tried both $objData->token and $objData->token[0]. But I got the same error notice. Kindly assist me...
I tried the Solution provided in the Post Notice: Trying to get property of non-object error, But it fails so, I raised 50 Bounty Points for this post. I tried to update my requirement in that post Question https://stackoverflow.com/review/suggested-edits/11690848, But the Edit was Rejected so I posted my requirement as a new Question. Kindly assist me...
The AngularJS is not sending any Object instead it passes the GET element.
Just access the Value by simply using $_GET['uid']
$objData doesn't seem to be an object.
try $objData['token'].
Or echo the object $objData and try to figure out what is its structure.

PHP function not browsable

I'm trying to build an API with a JSON output, the second function is producing the desired output however I can't call it from the web. Whereas the first function returns as expected a large mass of text. Am I using something that is not compatible? I'm using version PHP 5.5.9 on an Ubuntu 14.04 server.
I can view the result of this function in both the terminal and the browser;
<?php
class ArticlesAPI {
function top() {
$db = new mysqli("mysql-host.rds.amazonaws.com", "user", "password", "db_name");
$results = $db->query("SELECT article_id, title, summary FROM top_articles");
while ($row = $results->fetch_assoc()) {
echo $row['article_id'];
echo $row['title'];
echo $row['summary'];
}
$results->close();
}
}
$api = new ArticlesAPI;
$api->top();
?>
This function only returns a result in the terminal;
<?php
class ArticleAPI {
function top() {
$db = new mysqli("mysql-host.rds.amazonaws.com", "user", "password", "db_name");
$results = $db->query("SELECT article_id, title, summary FROM top_articles");
$articles = array();
while($article = $results->fetch_assoc()){
$article_id = $article['article_id'];
$articles[$article_id][] = $article['title'];
$articles[$article_id][] = $article['summary'];
}
$results->close();
$db->close();
$json = json_encode($articles);
echo $json;
}
}
$api = new ArticleAPI;
$api->top();
?>
There are 2 separate configuration files for CLI and for WEB check them and check server configuration(if http server parse php files etc.) you can do simple <?php echo 'hello world'; and see if output is correct. If you open it via browser and you see anything more than hello world then PHP parser is not enabled.
Also when you output JSON you should set proper header for browser application/json
Check your output buffering settings. Maybe you use ob_* functions and don't flush output to browswer
Try putting exit after echo and check script after.
Set error_reporting(E_ALL); and ini_set('display_errors', 1); in first line of your app to check if there are errors.

Use ISSET from some of the examples here on stackoverflow Error on the Code

I Dont think I wrote the code correctly. Can someone look at this? I am new to php!
<?php
// Create Connection
$connect = mysqli_connect('localhost','root','test123','joomla');
// Check Connection
if(mysqli_connect_errno($connect)) {
echo 'Failed to connect to DataBase| '. mysqli_connect_error();
}
?>
I added if(issets($_GET['rp_id'])) to the if rp_id does not exist in the url parameter - it just closes the database
<?php
if(issets($_GET['rp_id]')) {
$rp_id = htmlspecialchars($_GET["rp_id"]);
// open record with evdet_id =$rp_id
$result = mysqli_query($connect,"SELECT * FROM n0dap_jevents_vevdetail WHERE evdet_id =".$rp_id);
while($row = mysqli_fetch_array($result)):
$value = $row['google_map'];
echo $value;
endwhile;
}
$connect->close();
//if the rp_id does exist the go ahead and run the top otherwise close it.
} else {
$connect->close();
}
?>
if(issets($_GET['rp_id]))
Looks like you made a typo on isset().
Call to undefined function issets()
This means you're trying to call issets() as a function, which it is not.
The function that you are looking for is isset(). If you replace issets() with isset(), you will not longer get that error message.

Categories