How come html_entity_decode is not working? - php

I am having a lot of trouble trying to decode the html in my database columns. Each column in my table has the potential to include something that needs to be decoded. For example / to / is one of the many that needs to be decoded. I am pretty bad at php. My code does not do the decoding. Please help.
<?php
$connection = mysql_connect($host, $user, $pass);
if(!$connection)
{
die("Database server connection failed.");
}
else
{
$dbconnect = mysql_select_db($db, $connection);
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM Table WHERE area='ON'";
$resultset = mysql_query($query, $connection);
$records = array();
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
mysqli_close($connection);
json_encode($records);
echo html_entity_decode($records);
}
}
?>

Related

Select SQL query is not working in PHP

I am having trouble with an SQL query that I have inserted into a piece of PHP code to retrieve some data. The query itself works perfectly within SQL. I am using the following PHP script.
I have the following objectives:
Connect to the existing database. This part works well.
Get data from the column 'Brand' of the table 'Transport' in $sql. This part is not working at this stage. echo ($sql) returns SELECT Brand FROM Transport WHERE Type = 'car'
Could you please let me know if you see the solution to this issue and if the remaining part of the code is correct. This is my f_sqlConnect()
function f_sqlConnect() {
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!link) {
die('Could not connect: '.mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can not use'.DB_NAME.
': '.mysql_error());
}
}
/*This function cleans up the array to protect against injection attacks */
function f_clean($array) {
return array_map('mysql_real_escape_string', $array);
}
<?php
// Create connection
$link = f_sqlConnect();
// Getting data from the column Brand of the table Transport
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"]. "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>
Here is the code, without seeing your f_sqlConnect(); mothod. This method should return connection string for DB in your case. But you can use following code this must work.
<?php
$servername = "Your_db_host";
$username = "your_db_username";
$password = "your_db_password";
$dbname = "your_DB_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"];
}
} else {
echo "0 results";
}
$conn->close();
?>
NOTE: Object oriented way of mysqli, You can use procedural way too to connect and get data.

mysql not showing all databases

I'm trying to list all my databases. But I only return information schema and one other table.. I checked my user settings/privileges in mysql and I have access to everything.. How can I return all databases
here is the code i used:
$set = mysql_query('SHOW DATABASES;');
$dbs = array();
while($db = mysql_fetch_row($set)) $dbs[] = $db[0]; echo implode('<br/>', $dbs);
As pointed out in the comments, you really should start using mysqli instead of mysql.
This should solve your problem though:
<?php
$link = mysqli_connect("localhost", "mysql_username", "mysql_password");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$res = mysqli_query($link, "SHOW DATABASES");
while ($row = mysqli_fetch_assoc($res)) {
var_dump($row['Database']);
}

Is it possible to connect to two different mysql databases?

I have 2 databases that I need to connect with but I don't know how to put the query in my code.
I have 2 pages of code. 1 for the form and the other to display the data after we have filled out the form. Will I need to put a new query on both pages or neither?
When I try to put a second connection on both pages, the whole page isn't working.
The connection I have made looks like this for both pages:
<?php
session_start();
include('conn.php');
include('connection2.php');
?>
For second database:
<?php
define('DB_HOST2','localhost');
define('DB_USER2','root');
define('DB_PASSWORD2','');
define('DB_NAME2','smile2');
$dbc = #mysql_connect(DB_HOST2,DB_USER2,DB_PASSWORD2) OR Die ('Could not connect to MySQL: '. mysql_error());
#mysql_select_db (DB_NAME2) OR Die ('Could not select database: '. mysql_error());
?>
For first database:
<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_NAME','smile');
$dbc = #mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) OR Die ('Could not connect to MySQL: '. mysql_error());
#mysql_select_db (DB_NAME) OR Die ('Could not select database: '. mysql_error());
?>
How can I make #mysql_* change to:
<?php
//connect
mysql_connect("localhost", "user", "password");
//select first database
mysql_select_db("database_1");
//do query
mysql_query("SELECT * FROM `table` WHERE `field` = 1");
//select second database
mysql_select_db("database_2");
//do query on other database
mysql_query("SELECT * FROM `other_table` WHERE `field` = 2");
//you might wanna switch back to the first database
mysql_select_db("database_1");
?>
You can use the PHP function mysql_select_db to switch between databases. Like so:
<?php
//The preferred way:
//connect
mysqli_connect("localhost", "user", "password");
//select first database
mysqli_select_db("database_1");
//do query
mysqli_query("SELECT * FROM `table` WHERE `field` = 1");
//select second database
mysqli_select_db("database_2");
//do query on other database
mysqli_query("SELECT * FROM `other_table` WHERE `field` = 2");
//you might wanna switch back to the first database
mysqli_select_db("database_1");
//The old way
//connect
mysql_connect("localhost", "user", "password");
//select first database
mysql_select_db("database_1");
//do query
mysql_query("SELECT * FROM `table` WHERE `field` = 1");
//select second database
mysql_select_db("database_2");
//do query on other database
mysql_query("SELECT * FROM `other_table` WHERE `field` = 2");
//you might wanna switch back to the first database
mysql_select_db("database_1");
?>
<?php
class manageDB{
private $dbStatus = false;
private $mode = 'debug';
private $curCon = 'db1';
private $db = '';
private $con = '';
private $details = array(
'db1'=>array(
'server'=> '',
'user' => '',
'pass' => '',
'db' => ''
),
'db2'=>array(
'server'=> '',
'user' => '',
'pass' => '',
'db' => ''
)
);
//Init object
function __construct() {
$this->curCon = ($GLOBALS['isDebug']) ? 'db1' : 'db2';
}
public function defineDB($options, $type){
if(empty($type)){
$this->curCon = 'db1';
$this->details[$this->curCon]['server'] = $options['server'];
$this->details[$this->curCon]['user'] = $options['user'];
$this->details[$this->curCon]['pass'] = $options['pass'];
$this->details[$this->curCon]['db'] = $options['db'];
$this->con = null;
} else {
$this->curCon = $type;
}
$this->returnDB = FALSE;
if($this->dbStatus == TRUE) $this->open();
}
public function open(){
$this->con = mysql_connect($this->details[$this->curCon]['server'], $this->details[$this->curCon]['user'], $this->details[$this->curCon]['pass']);
if (!$this->con) { die('Could not connect: ' . mysql_error()); };
mysql_select_db($this->details[$this->curCon]['db'], $this->con);
$this->dbStatus = true;
}
public function close(){
mysql_close($this->con);
unset($this->connectStatus);
$this->dbStatus = false;
}
public function changeDB($db){
if($this->details[$this->curCon]['db'] != $db) mysql_select_db($db, $this->con);
$this->returnDB = TRUE;
return $this;
}
public function qry($query) {
$args = func_get_args();
if(count($args) > 1){
$query = array_shift($args);
$query = str_replace("?", "%s", $query);
$args = array_map('mysql_real_escape_string', $args);
array_unshift($args,$query);
$query = call_user_func_array('sprintf',$args);
}
$result = mysql_query($query);
//Check if we need to return to default table
if(isset($this->returnDB)){
mysql_select_db($this->details[$this->curCon]['db'], $this->con);
$this->returnDB = FALSE;
}
if($result){
return $result;
}else{
$error = "Error";
return $result;
}
}
public function checkStatus(){ echo $this->dbStatus; }
};
$db = new manageDB();
?>
Usage:
for normal operations use
$select = $db->query(...);
to switch database use
$select = $db->changeDB('db_name')->query(...);
easily done one step at a time, like here:
<?php
$link = mysql_connect("localhost", "mysql_user","mysql_password")
or die("Could not connect to MYSQL");
$selected_db = mysql_select_db('Database_Name', $link)
or die ('Sorry, could not connect to Database');
echo 'Connected successfully';
mysql_close($link);
?>
then the second one as:
<?php
$link = mysql_connect("localhost", "mysql_user","mysql_password")
or die("Could not connect to MYSQL");
$selected_db = mysql_select_db('Database_Name', $link)
or die ('Sorry, could not connect to Database');
echo 'Connected successfully';
mysql_close($link);
?>

mysql to pdo JSON for ios

I'm trying to convert this mysql code into PDO code, yet I can only return one of my rows in JSON whereas the mysql code allows me all the rows.
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM questions ORDER BY RAND() LIMIT 40";
$resultset = mysql_query($query, $connection);
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
And here is the PDO code I've got to so far
$db = new PDO('mysql:host=***;dbname=***', $user, $pass);
$query = "SELECT * FROM questions ORDER BY RAND() LIMIT 40";
$stmt = $db->prepare($query);
$stmt->execute();
$records = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$records = $row;
}
echo json_encode($records);
It looks like I have to fill some more of this post out with random gobldygook as it seems I haven't already gotten to the point.
forgot to push each row into records, therefore
$records[] = $row;
or use fetchAll()
$db = new PDO('mysql:host=***;dbname=***', $user, $pass);
$query = "SELECT * FROM questions ORDER BY RAND() LIMIT 40";
$stmt = $db->prepare($query);
$stmt->execute();
$records = $stmt->fetchAll(PDO::FETCH_ASSOC); // to get all records at once
echo json_encode($records);

How to get rows of a specific type matching a specific value?

I want to get data from a table in my MySQL database.
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$search='flu';
$query = mysql_query("SELECT * From ProviderDxCptCodes WHERE CodeType='CPT'");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
echo json_encode($rows);
I want this query to show me all CPT codes matching the search string flu. How can I do that?
Do you mean something like that?
SELECT * From ProviderDxCptCodes WHERE CodeType='CPT' AND name LIKE '%flu%';
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$flu = "CPT";
$query = mysql_query("SELECT * From ProviderDxCptCodes WHERE CodeType='%".mysql_real_escape_string($flu)."%'");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
echo json_encode($rows);

Categories