How do I display the value of a row? - php

I have five rows setup in a MySQLi database:
$title, $subtitle, $owner, $contactemail, and $footer.
Instead of writing out my websites title on EVERY .php page, I would like to use a function that will display the value of $title, $subtitle, etc.
This is what I have done so far:
index.php
<?php include 'config.php'; ?>
<?echo $title;?>
config.php
<?php
$mysql_hostname = 'localhost';
$mysql_username = 'root';
$mysql_dbname = 'toplist';
$mysql_password = '';
$dbh= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$query = "SELECT * FROM toplist_settings";
$title=['title'];
$subtitle=['subtitle'];
$owner=['owner'];
$contactemail=['contactemail'];
$footer=['footer'];
?>
But this is not working. When I use , etc. nothing shows up.

try this
$query = "SELECT * FROM toplist_settings";
$stmt = $dbh->query($query);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$title=$row['title'];
$subtitle=$row['subtitle'];
$owner=$row['owner'];
$contactemail=$row['contactemail'];
$footer=$row['footer'];

execute your query to get records
$query = $dbh->prepare("SELECT * FROM toplist_settings");
print_r( $query->execute());

Related

mysql get data from outside of while

I want to get some data from database with this code
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $bd);
$result = mysql_query("SELECT author_id FROM user");
while($data = mysql_fetch_array($result)) {
$sn=$data['author_id'];
}
$lastresult = mysql_query("SELECT * FROM post WHERE id='".$sn."'");
This code only work fetches one author_id
$sn=$data['author_id'] and $lastresult works only with one author_id and doesn't fetches all the author_id.
How can I get data for all author_id ?
You are overwritting $sn each loop.
$result = mysql_query("SELECT author_id FROM user");
$sn = [];
while($data = mysql_fetch_array($result)) {
$sn[] = $data['author_id'];
}
$sn = implode(',',$sn);
$lastresult = mysql_query("SELECT * FROM post WHERE id IN (".$sn.")");
PS: mysql_* functions are deprecated and has security issues. Consider replace with mysqli_* functions or PDO.

PHP MySql delete query

I have a delete script on my dashboard that WAS working before moving domains.
( not sure if that is relevant )
The code for my 'deletejob.php' is below.
<?php
error_reporting(0);
$host = 'localhost';
$port = 3306;
$database = 'database';
$username = 'username';
$password = 'password';
$UID = $_POST["ID"];
// Connect to the database
$dsn = "mysql:host=$host;port=$port;dbname=$database";
$db = new PDO($dsn, $username, $password); // connect
$Query = "DELETE FROM joblist WHERE ID='$UID'";
// Do a query thingy whatever its called
$statement = $db->prepare($Query);
$statement->execute();
while ($result = $statement->fetchObject()) {}
?>
The script functions as if it is working and even gives me the alert
( ID has been successfully deleted. )
Does anyone have any idea as to why this script would return a false positive?
You must find the row you want to delete, using SELECT statement, like this:
$stmt= $conn->query("SELECT * FROM users WHERE id='".$_REQUEST['ids']."'");
Also You have to have the following sent when you click the delete button or it will not delete at all:
<input type="hidden" name="id" value="<?php echo $_REQUEST['ids'];?>">
After the above:
$stmt= $conn->query("DELETE FROM users Where id = '".$_REQUEST['id']."'");

What is wrong with my sql query? (PHP)

What is wrong with this code? Why can't I get the username from the members table in website database. Here's the screenshot of database information:
I am trying to make a function for it so that I can use it later. Here's the code:
<?php
function get_username($username){
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'website';
$con = mysqli_connect($host, $username, $password, $database);
$query = "SELECT * FROM members WHERE username = '{$username}'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
return $row['name'];
mysqli_close($con);
}
echo get_username('iamfaizahmed');
?>
Your username is overwritten in the function
$username = 'root'; // here
So no matter what you pass in the function argument it will always use
where username = 'root'
So use a different variable name
you are mixing between $username of the function and $username of your connect variable
change this
function get_username($username){
to
function get_username($user){
and do your query like that
$query = "SELECT * FROM members WHERE username = '$user'";
First thing, your $username variable.
You're passing it through the function and also using it for db connection.
Change your dbusername in the function as $dbusername
Second, the query. Keep it as:
$query = "SELECT * FROM members WHERE username = '$username'";
Now, once you're done with the basics, I recommend you to visit this link: Click Here
That should get your somewhere!

MySQL query doesn't work with sorcerer in Joomla

I've installed sorcerer in Joomla! 3.2 to execute some custom php.
The basic php code works fine, but the SQL-query gives no result. My code:
{source}
$servername = //my db-server;
$dbname = //my db-name;
$username = my db-username;
$password = my db-password;
$connection = mysql_connect($servername, $username, $password);
mysql_select_db($dbname);
$abfrage = "SELECT * FROM workshops";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)) {
echo $row->id;
}
{/source}
At a normal website without joomla! that code works perfect.
edit:
the correct query would look like this:
{source}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('id', 'workshop_title', 'workshop_time');
$query->from($db->quoteName('workshops'));
$query->order('ordering ASC');
$db->setQuery($query);
$results = $db->loadObjectList();
echo $results->id;
{/source}
Joomla has a class for that, read more about it at with really nice examples on how to set it up http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase

How can I use one connection for 2 or more functions?

I use PHP's PDO to connect to MySQL. I have this code for connection:
$dbh = new PDO ( $db_host.$db_database, $db_user, $db_pass );
$dbh->exec ( "set names utf8" );
I have a function in another file:
function Image()
{
include 'config/connect.php';
#connected
$sql = 'Select * from settings where name="X" ';
$stmt = $dbh->prepare($sql);
$stmt->execute();
$row = $stmt->fetchObject();
$Template = $row->web_site_template;
echo "Template";
}
I can use include connect.php file for that, but it's not true.
I want use one function like connection() for connect to the mysql on all other functions, like:
function Image()
{
connection();
$sql = 'Select * from settings where name="X" ';
$stmt = $dbh->prepare($sql);
$stmt->execute();
$row = $stmt->fetchObject();
$Template = $row->web_site_template;
echo "Template";
}
This is the function. Put it in any file you like to.
function connection() {
$db_host = "..."; $db_database = "..."; $db_user = "..."; $db_pass = "...";
$GLOBALS["dbh"] = new PDO ( $db_host.$db_database, $db_user, $db_pass );
$GLOBALS["dbh"]->exec ( "set names utf8" );
}
This is your main code. Include the file with the code above if you decided to put it in another file.
connection();
$sql = 'Select * from settings where name="X" ';
$stmt = $dbh->prepare($sql);
$stmt->execute();
$row = $stmt->fetchObject();
$Template = $row->web_site_template;
echo "Template";
I would consider it bad coding style though.
I find a best solution for my question (How you can use a MYSQL connection for 1 or more functions):
$db_database ="YourTableName";
$db_user ="YourUsername";
$db_pass ="YourPassword";
$db_host ="YourHostName";
$dbh = new PDO ( "mysql:host=$db_host;dbname=$db_database", $db_user, $db_pass );
$dbh->exec ( "set names utf8" );
$database = $dbh; // Here you can use $dbh too, but I use $database to understand the difference here .
#start function
function Template(){
global $database; //use $database as global
$sql = 'Select * from YourTableName where column="Record"';
$stmt = $database->prepare($sql); //use $database Instead $dbh
$stmt->execute();
$row = $stmt->fetchObject();
$Template = $row->web_site_template;
echo $Template;
}
Template(); // Here is your result.

Categories