using joomla with k2 I am trying to call from my database the category that is assigned with my item title and I use this code.In order to achieve this. But something is wrong. I do get the title but I am not getting the category name. Below is the code that I am using to get it:
?php
$url= $_SERVER['REQUEST_URI'];
$data = parse_url($url);
$number = basename($data['path'], '.html');
$needle="-";
$number1=substr($number, 0, strpos($number, $needle));
?>
<?php
$db =& JFactory::getDBO();
$db->setQuery('SELECT title FROM #__k2_items WHERE id="'.$number1.'"');
$databaseTitle = $db->loadResult();
$db->setQuery('SELECT catid FROM #__k2_items WHERE id="'.$number1.'"');
$databaseCatid= $db->loadResult();
$db->setQuery('SELECT #__k2_categories.name FROM #__k2_categories INNER JOIN #__k2_items ON #__k2_categories.id = #__k2_items.catid WHERE #__k2_items.catid ="$databaseCatid" LIMIT 1');
$databaseNamecat= $db->loadResult();
?>
<h2>Submit a Charter Request for <?php echo $databaseTitle." from port ".$databaseNamecat; ?></h2>
because you are enclosing a variable between double quotes.
try this
$db->setQuery("SELECT #__k2_categories.name FROM #__k2_categories INNER JOIN #__k2_items ON #__k2_categories.id = #__k2_items.catid WHERE #__k2_items.catid ='".$databaseCatid."' LIMIT 1 ");
Related
How can I transform this MySQL query into Codeigniter query?
SELECT *
FROM categories
LEFT JOIN user_category_subscriptions ON
user_category_subscriptions.category_id = categories.category_id
and user_category_subscriptions.user_id =1
You need to place the AND part inside Codeigniter's join()
$query = $this->db ->select('t1.*')
->join('user_category_subscriptions t2', 't1.category_id =t2.category_id AND t2.user_id =1','left')
->get('categories t1');
return ($query->num_rows())?$query->result():false;
with this line after the query you can double-check if the query was generated correctly:
echo $this->db->last_query();die;
more about CI 2.x Active Record Class and alternatively CI 3.x Query Builder Class
According to the documentation here:
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
echo 'Total Results: ' . $query->num_rows();
ORM:
If you would like to use an ORM, you could follow the advice given on this StackOverflow Link
I have done this type of SELECT many times, but this time I can't get it to work. Any ideas, please?
$Name = "Dick";
$conn = mysqli_connect($server, $dbname, $dbpw, $dbuser);
$sql = "SELECT id FROM table WHERE $Name = table.first_name";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$customer_id = $row['id'];
Database::disconnect();
echo "customer id = " . $customer_id;
If you really DO have a table named table it would be more appropriate to use back ticks around the name since the word TABLE is a reserved word in MySQL. You should also use single quotes around your variable if it contains a string:
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
Other possible reasons if the query still doesn't work for you:
Make sure you have the connection parameters in the right order. It should be: mysqli_connect($server, $dbuser, $dbpw, $dbname).
You should be using fetch_array() instead of fetch_assoc() if you expect a one row result.
You are mixing PROCEDURAL STYLE with Object Oriented Style when using mysqli_connect() instead of mysqli(), at the same time using $result-> which is object oriented style. You should decide one style and stick with it.
This would be the procedural style of your query:
$Name = "Dick";
$conn = mysqli_connect($server, $dbuser, $dbpw, $dbname); // NOTE THE CHANGED ORDER OF CONNECTION PARAMETERS!
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
mysqli_free_result($result); // FREE RESULT SET
mysqli_close($conn); // CLOSE CONNECTION
And this would be the object oriented style:
$Name = "Dick";
$conn = new mysqli($server, $dbuser, $dbpw, $dbname);
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
$result->free(); // FREE RESULT SET
$conn->close(); // CLOSE CONNECTION
I would recommend naming your table something else than table since it's a reserved word and could get you into parsing problems. The same goes with field names. More reading: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
More about mysqli_fetch_array() and differences in procedural style and object oriented style use: http://php.net/manual/en/mysqli-result.fetch-array.php
$sql = "SELECT id FROM table WHERE '$Name' = table.first_name";
You simply need to concat the variable like this:
$sql = "SELECT id FROM table WHERE " . $Name . " = table.first_name";
I want to select a row from a database table, using where id= 'x'.
The thing is that I want to be able to change that 'x' dynamically, so if I need to select a row where id= 'y', I want to be able to just load y in another row and read it from that row.
Sorry for my bad english.
<?php
$id = a field from another table
$result = mysqli_query($con,"SELECT * FROM text where id= '$id'");
while($row = mysqli_fetch_array($result))
{
echo $row['text_area'];
}
?>
Here is an image that I hope will explain this better.
Try this code:
<?php
$result1 = mysqli_query($con,"SELECT * FROM id_val");
while($row1 = mysqli_fetch_assoc($result1)){
$result = mysqli_query($con,"SELECT * FROM text where id= '{$row1['value']}'");
while($row = mysqli_fetch_assoc($result))
{
echo $row['text_area'];
}
}
?>
I've been trying to display the number of rows using this code but it keep says
1 Rows which is wrong
<?php
$link = mysql_connect("localhost", "gamin1_forum", "password123");
mysql_select_db("gamin1_forumdb", $link);
$result = mysql_query("SELECT COUNT(*) FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows";
?>
Rows are approximately 1443 but it kept saying 1
The Count(*) returns you one row which contains the number of rows as a value.
By using mysql_num_rows($result) you are actually counting the amount of rows of the Count(*) result which really is one.
Change it to:
$result = mysql_query("SELECT * FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
Or just use the Count(*) value (which is probably better since it count in the DB and not retrieving the whole table for it) using mysql_fetch_array.
This is true, mysql_num_rows() is telling that there was one "row" returned. You need to check the value of the returned row to get your count. Try this
$link = mysql_connect("localhost", "gamin1_forum", "password123");
$result = mysql_query("SELECT COUNT(*) as cnt FROM smf_personal_messages", $link);
$row = mysql_fetch_array($result);
echo ($row['cnt']);
On a side note, you should not use mysql_* functions for security reasons. Try PDO/mysqli_* functions
I have the following query, and would like to list only the first match.
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
(?category=b)
So DISTINCT could do this right? This is what I tried, but did not work:
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
EDIT: Here is the full code:
function getCategory() {
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'") or die(mysql_error());
//$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
if(mysql_num_rows($query) == 0) {
echo "Geen resultaten gevonden.";
} else {
while ($row = mysql_fetch_assoc($query)) { ?>
<p><?= $row['author']; ?></p>
<?php }
}
}
(B% is just for testing)
If I run this following query in the database directly I get two results. If I run with the code above I just get an empty page (except for the html thats already there).
SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'
You should use LIMIT 1 to list only the first match.
If you have a a table "tbl_lyrics" with fields: author lyrics year and is filled for example as follows:
author_A lyrics_A year_A
author_A lyrics_A1 year_A1
author_A1 lyrics_A2 year_A
author_B lyrics_B1 year_B1
if you do
select distinct(author) from tbl_lyrics where author like '%author_A%'
you are going to get: author_A and author_A1. NOT the first one that matches.
If you want the first one that matches you can do:
select author from (select distinct(author) as author from tbl_lyrics where author like '%author_A%') where rownum <2;
this will return author_A only.
Limit is used with MySql but would not work with oracle databases