MySQL query doesn't work with sorcerer in Joomla - php

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

Related

MySQL in PHP - WHERE clause - not working

I have this code:
<?php
$user = $_COOKIE["user"];
$password = $_COOKIE["password"];
$localhost = "localhost";
$userdb = "xxxxx";
$passworddb = "xxxxx";
$database = "xxxxx";
$conn = mysqli_connect($localhost, $userdb, $passworddb, $database);
$vyber = "SELECT PASSWORD FROM Login WHERE User=".$user;
$result = mysqli_query($conn, $vyber);
echo $result;
?>
Cookie are set and if I use $vyber in database so everything is good. But there PHP write nothing. Can anybody tell, what I doing wrong? (Without comand $vyber every thing running perfect)
instead of,
echo $result
try to do that :
while ($row = mysqli_fetch_row($result)){
echo $row[0];
}
It is query error change query to :
$vyber = "SELECT PASSWORD FROM Login WHERE User='$user'";
if did not work use die function to dispaly error message :
mysqli_query($conn, $vyber) or die(mysqli_error($conn));
To fetch records :
while ($row = mysqli_fetch_array($result)){
echo $row[0];
}

PHP mysqli query doesn't return any results

I'm using this code here
<?php
error_reporting(1);
$servername = '127.0.0.1';
$username = '';
$password = '';
$dbname = 'splafpoo_users';
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){
printf("<b>Connection failed:</b> %s\n", mysqli_connect_error());
exit;
}
$key = '';
if(isset($_POST['key'])){
$key = $_POST['key'];
}
$query = "SELECT * FROM users WHERE serial='$key'";
echo $query;
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row;
?>
Running the query SELECT * FROM users WHERE serial='test' in phpMyAdmin returns the desired result however when trying to display the result using the code above nothing is displayed and I cannot figure out how. How do I display the result?
You're gonna need a good old fashion while loop
while($row = $result->fetch_assoc()) {
echo $row['WHATEVERCOLUMNITISYOUWANT'];
}
also this is most definitely a duplicate.
Use var_dump($row) instead of echo $row or you use echo with a key:e.g. echo $row["user"]

mysql can't return a row result

I am connected to the database, a page has lots of content so I'll only share the part that doesnt return a value in php, but it returns a value in MySQL
Here is the code;
$query = "SELECT firstname FROM users WHERE id = '17'";
$query_run = mysql_query($query);
$row = mysql_fetch_row($query_run);
echo $row[0];
Changing the code I shared first to this, solved the problem. Thanks to anyone who tried to help.
$query = "SELECT firstname FROM users WHERE id = '17'";
$query_run = mysqli_query($conn, $query);
$row = mysqli_fetch_row($query_run);
echo $row[0];
And made bit changes to the connect.inc.php which I also shared in comment.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "notsitesi";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
die("Connection failed");
}
?>

How do I display the value of a row?

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());

Error calling member function "FETCH_ASSOC" - what is wrong with my code?

I am trying to connect to a PHP PDO and run a simple query on the data but I run into an error about the object not being created? I'm trying to make this as simple as possible and have been advised not to make a singleton database class and instead define the Database PDO whenever I need it, which is for this initial (simple) query.
Here is the error I am getting. What do I need to do to fix the code?
Fatal error: Call to a member function fetch() on a non-object in/home/...index.php on line 11
<?php
$dbhost = "localhost";
$dbname = "x";
$dbuser = "y";
$dbpass = "z";
$db = new PDO("mysql:host=$dbhost;db_name=$dbname", $dbuser, $dbpass);
$query = $db->query("SELECT * FROM tablename");
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
echo $row['field_name'],'<br>';
}
?>
And please let me know if you have a better way to select data from a mysql table using php. Trying to learn PDO and not mysqli. Thanks
You have to choose between ->query or ->fetch. You can't mix both method.
->query():
$sql = 'SELECT * FROM tablename';
foreach ($db->query($sql) as $row) {
echo $row['field_name'],'<br>';
}
->fetch():
$sth = $db->prepare("SELECT * FROM tablename");
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $row['field_name'],'<br>';
}
Probably there is a problem with the query, like a typo or the table does not exist.
Then $query is not a PDOStatement but false and you can't call false->fetch().
There's your typo:
$db = new PDO("mysql:host=$dbhost;db_name=$dbname", $dbuser, $dbpass);
Should be:
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
After fixing the typo this code works perfectly:
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$query = $db->query("SELECT * FROM wp_blogs");
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
var_dump($row).'<br>';
}

Categories