retreaving data from a mysql database - php

I have a mysql database which was created for a website using wordpress, when posting some text using wordpress it stores it on that database. When I try pulling this text using a php file, it returns null, but if I delete this text and write it in manually(no wordpress) it pulls it fine. is there anything specific i need to do in order to pull this text since it was created using wordpress? the structure of the table is as follows, field: post_content type: longtext. Thanks for the help
Code:
$query = "SELECT post_content FROM meiplay_posts WHERE post_type = 'portfolio'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
$output[] = $row;
print(json_encode($output));

Add some error handling:
$result = mysql_query($query) or die(mysql_error());
This will cause an error message to be printed if something goes wrong in mysql_query. Hopefully the error message will help.
Also, the documentation for mysql_query says:
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.
So I'd advise using the libraries that the PHP authors recommend.
Also:
$query = "SELECT post_content FROM meiplay_posts WHERE post_type = 'portfolio'";
Are your sure that columns with post_type = 'portfolio' exist? Try doing the select without the WHERE part and see if you get anything. Maybe it's slightly different, like Portfolio instead of portfolio?

Wordpress gives you a class that lets you run your queries on the database more easily.
Reference: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column
Try:
$result = $wpdb->get_col($wpdb->prepare("SELECT `post_content` FROM `meiplay_posts` WHERE post_type = 'portfolio'"));
if ($result)
{
echo $result->post_content;
echo json_encode($result);
};

Related

Displaying Datafield from mySQL in WP Shortcode

I am trying to display field data in the WP front end using shortcodes from a new table.
See Table
After coming across many sources and research, I do not seem to find a simple way to display data in text (not table), contained within a specific field selected in the SQL query by means of SELECT FROM WHERE.
So far I called wpdb, selected the field, created a loop and echoed. But no results are displayed.
I also tried using print_r and implode but both failed too.
<?php
function Initial_Brief(){ global $wpdb;
$results = $wpdb->prepare( "SELECT 'Initial_Brief'* FROM `Portal_100` WHERE Project_Title = 'Project 1'");
foreach ($results as $result)
echo $result['results'];
}
add_shortcode('Initial_Brief','Initial_Brief')
?>
Many thanks in advance,
To share the logic of this, which I find quite powerful, is to use shortcodes for displaying all text on the website, enabling text edit from the front-end by creating an HTML form which updates the specific field. I will create an edit icon displayed on hover to an editor's role, clicked to trigger a popup with an html form which calls a function to update the specific field in the database.
You are missing to call the get_results method like this:
<?php
global $wpdb;
$id = 23;
$sql = $wpdb->prepare( "SELECT * FROM tablename WHERE id= %d",$id);
$results = $wpdb->get_results( $sql , ARRAY_A );
?>
The use of the prepare method is a good practice for preparing a SQL query for safe execution (eg. preempt SQL injection), but it is no yet the execution, it returns a sanitized query string, if there is a query to prepare.
It is also good companion for the query method.
After some iterations I finally got it to work!
I understand mySQL does not accept input with a spacing?
How can I insert a WHERE condition for multiple words or a paragraph?
It worked for me using the following script but I had to change the WHERE value into an integer.
<?php
add_shortcode('Initial_Brief', function(){
global $wpdb;
$sql = $wpdb->prepare("Select Value from `P100` WHERE `P100`.`id` = 1 ");
$results = $wpdb->get_results( $sql , ARRAY_A );
foreach ($results as $result) {
$display = implode(", ", $result);
echo $display;
});?>

Display PHP code on web page

I am creating a library for PHP scripts and I want to be able to show php code on a html webpage.
I have looked at using highlight_file(); but this will show the whole page
For example, If I have a page called code.php which has an sql query on ( select code from table where sequence = $_GET["id"] ) - example then I use
Highlight_file('code.php?id=123');
This will work but will also show the select query which I do not want to show. I would just want to show the code from the database (code column)
How can I display just the code from the database with the correct colours and formatting etc
UPDATE:
<?php
$conn=mysql_connect("localhost","charlie_library","Pathfinder0287");
mysql_select_db("charlie_library",$conn);
function highlight_code_with_id($id, $conn)
{
$query = "select * from library_php where sequence = '$id' ";
$rs = mysql_query($query,$conn);
$code = mysql_fetch_array($rs);
echo highlight_string($code["code"]);
}
// and, use it like this:
highlight_code_with_id($_GET['id'], $conn);
?>
I have tried the above code, which is just displaying the code in plain text
use highlight_string function, like this:
<?php
highlight_string($code);
?>
where $code is the code you have obtained from your SQL query.
You can create a function around this (something along the following lines):
<?php
function highlight_code_with_id($id, $mysqli) {
$query = $mysqli->query("select code from table where sequence = '$id'");
$code = current($query->fetch_assoc());
return highlight_string($code);
}
// and, use it like this:
echo highlight_code_with_id($_GET['id'], $mysqli);
UPDATE:
Your code is a bit incorrect, you can use:
<?php
$conn=mysql_connect("localhost","charlie_library","Pathfinder0287");
mysql_select_db("charlie_library",$conn);
function highlight_code_with_id($id)
{
$query = "select * from library_php where sequence = '$id' ";
$rs = mysql_query($query);
$code = mysql_fetch_assoc($rs); // change is in this line
echo highlight_string($code["code"]);
}
// and, use it like this:
highlight_code_with_id($_GET['id']);
?>
Note that you do not need to include $conn in your function, it can be ommitted. Also, note that you should use mysqli->* family of functions, since mysql_* family has been deprecated.
Perhaps this would work for you.
This post is originally for HTML, but the answer linked above shows an example using PHP.

php get specific data from the same column in all row

i have a php question,
how can i get all the data from the same column from all row in a mysql table.
Table name : user
Here's my table structure:
name
url
How can i use php to get all the data in URL column from each row?
(P/S i have my connection to database established , just not sure the musql query for this)
Thanks and have a nice day.
This really is extremely basic stuff and you could have found this anywhere, it's even in the PHP manual. But alas, here you go.
$result = mysql_query("SELECT url FROM user");
while($row=mysql_fetch_assoc($result){
echo $row['url'].'\n';
}
Please read up on some basic stuff to avoid asking these kind of questions: http://www.freewebmasterhelp.com/tutorials/phpmysql
$query = "select url from user";
$result = mysql_query($query);
while( $row = mysql_fetch_assoc($result)){
echo $row['url'] . '<br>';
}

Joomla - Independent SQL query

I'm wondering if someone can give me a bit of a hand.
I'm trying to write a query inside a PHP file in Joomla and its not really working that well, nothing is being output.
I'm very new to this Joomla stuff, so if someone could let me know where I'm going wrong that would be great.
My code is as follows:
$db =& JFactory::getDBO();
$query = "SELECT fullname FROM jos_jxzine_authors WHERE published = '1'";
$db->setQuery($query);
$column = $db->loadResultArray();
echo JHTML::_('select.options', $column, 'value', 'text', $this->categoryMap);
Cheers,
Please use this query
$query = "SELECT fullname FROM `#__jxzine_authors` WHERE published = '1'";
joomla will itself add db prefix. So you must use #_ instead of jos

Problem with displaying information from DB

//conn to DB
echo $_POST['text'];
$filter = $_POST['text'];
$sql = "SELECT DISTINCT * FROM contents
WHERE
MATCH(content,title) AGAINST ('$filter')
";
$mksql=mysql_query($sql);
while($row = mysql_fetch_assoc($mksql)) {
echo $row['title']."<br />";
}
I send POST request to another page with the above code.
It echoes me what I wrote in the input field but it doesn't output any result.
When I run the query in phpmyadmin in works and outputs me 1 result.
Where's the problem?
Try this :
$sql = "SELECT DISTINCT * FROM contents
WHERE MATCH(content,title) AGAINST ('$filter' IN BOOLEAN MODE ) ";
Before query any fulltext search you must create mysql fulltext indexing.
Execute this query in phpmyadmin.
ALTER TABLE contents ADD FULLTEXT(content, title);
may be there is problem in the post data which is not matching with full text search. you can use mysql_escape_string(), htmlspecialchars() functions for the post data you are using for query. These functions will help avoiding html tags,quotes,etc. you can print the generated sql and run that query in phpmyadmin. and of course create mysql fulltext indexing for the field you are searching.

Categories