How to use MySql Query in CI Bonfire - mysql

I have this code on my PHP test file. Now I need to do it in my CI-Bonfire application. I used find_by() and find_all() but, I didn't get any result that I want.
<?php
$result2 = mysql_query('SELECT * FROM its1_dms_users LEFT JOIN its1_dms_roles ON its1_dms_users.role_id=its1_dms_roles.role_id WHERE its1_dms_users.deleted=0 AND its1_dms_users.banned=0 AND its1_dms_users.active=1');
print "<select name='Notice' multiple size='10'>";
while ($roles = mysql_fetch_assoc($result2)){
print "<optgroup label='" . $roles['role_name'] . "'>";
print "<option value='" . $roles['id'] . "'>" . $roles['display_name'] . "</option>";
print "</optgroup>";
}
print "</select>";
?>
I just need to know. How I run that Query in CI-Bonfire in My Modules Controller.

As the Ci Bonfire works on the codeignitor frame work so you can run the mysql Query using the codeignitor class.
Please follow the below URL.
http://ellislab.com/codeigniter/user-guide/database/queries.html

Related

Create a div on my website that shows database information with php

I need to create a div that only shows the information of the database, how should i connect the div to mysql so it only shows my database information?
I'm not sure I'm understanding your question... but I'll give it a try. The code below this will select whatever you tell it to from the database and echo it into a table.
<?php
$db = mysqli_connect('host', 'username', 'password', 'database');
$sql = "SELECT * FROM database"
mysqli_stmt_bind_param($db, "params here", your info here);
$res = mysqli_stmt_execute($sql);
mysqli_stmt_close($sql);
mysqli_close($db);
echo "<table>";
while($row = mysqli_fetch_array($res)){ //Creates a loop to loop through results
echo "<tr><td>" . $row[''] . "</td><td>" . $row[''] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
?>

how to display images whose names are stored in database

I had created a table in mysql database. I am using php One column was for images, but it only stored images name. I wanted to print the whole table so did the coding but got got only the name of the images in the table.
Please explain how can I display images on the webpage that are stored in my PC. I don't want to save images in the database.
I used this code to print the table :
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$query="SELECT * FROM images LIMIT 0,5";
$result = #mysql_query($query);
echo "<table border='1'>
<tr>
<th>S.No.</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['image_id'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Hope you got some answer....
All your code is doing is sending the filename to the screen as a string. You need to the use image tags.
You may find this page useful, http://www.htmlcodetutorial.com/images/_IMG.html

How to repeat different pic?

I wan to always display the different pic. So there's a problem with looping the different pic. How do i do that?
index.php:
<?php
$result = mysql_query("SELECT * FROM table2", $connection );
echo '<ul>';
while($row = mysql_fetch_array($result)) {
if ($row)
echo "<li>";
echo "<img src='code.php?id=3'>";
echo "&nbsp";
echo "</p>";
echo "</li>";
echo "<br/>" ;
}
echo '</ul>';
mysql_close($connection);
?>
You need to pull the id value from the database. If you have a column called, maybe id you would want to put:
while($row = mysql_fetch_array($result)){
if ($row){
echo "<li>";
echo "<img src='code.php?id=".$row['id']."'/>"; // see what I did there?
echo "&nbsp";
echo "</p>"; // take this out
echo "</li>";
echo "<br/>"; // take this out
}
}
P.S. - don't write new code using the deprecated mysql extension. Use PDO or mysqli at least. And don't put <br />s between your <li>s, or close unopened <p>s. And, generally speaking, don't store your images in your database - just store the path to the image and put the images themselves in a folder on the server where they belong.
And please format your code - it is hard to read with no indentation or separation of files (your if statement was not properly enclosing what should have been conditioned statements). And mysql_real_escape_string is not as cool as you think it is.
Hope this helps.

Error: Column count doesn't match value count at row 1 -MySQL

I am a beginner programmer and I have just started using MySQL - I am using a program called PHP MyAdmin to Create a table in a database. I want to enter in values and then have those values appear in a table. I have made a table called table 1 in a database called sweetshop. The two rows I put in the PHP MyAdmin program are called SweetID and SweetName.
I hope someone will be able to help. Sorry if I don't come across clear - I haven't been coding long!
The code I'm using for this particular section is:
<?php
$con = mysqli_connect("localhost","root","","sweetshop");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to Connect to MySQL: ".mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table>
<tr>
<th>Sweet ID</th>
<th>Name</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo " <td>" . $row['SweetID'] . "</td>";
echo " <td>" . $row['SweetName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I assume you mean that the columns you have are named SweetID and SweetName
If so, change your select statement from
$result = mysqli_query($con,"SELECT * FROM table1");
to
$result = mysqli_query($con,"SELECT SweetID, SweetName FROM sweetshop.table1");
and you should be good (db name in your select isn't required but is a great habit to develop now).
Try running that exact select through phpMyadmin to see what your result set looks like as well, if you get results there then you should also get them from php
It would also be helpful for you to post the full structure of the table just in case

Getting mysql field data when a link is clicked?

I'm trying to get data from a database if a link is clicked.
Basically i have a main.php that contains this:
$sql2="SELECT projectsid, projectname, description,
SUBSTRING(description,0,80) FROM projects";
$result2=mysql_query($sql2);
while($row2 = mysql_fetch_assoc($result2)) {
echo "<div id=\"links\">";
echo "<ul>";
echo "<li> <a href=\"fullproject.php\">" . $row2['projectname'];
$_SESSION['projectname']= $row2['projectname'];
echo "<em>" . $row2['description'] . "</em></a></li>";
echo "</ul>";
echo "</div>";
}
This displays a list of names and a brief description.
Proj1
description
Proj2
description
What i'd like to do is to display the full contents of a project if that one is clicked.
fullproject.php
<?php
session_start();
$projectname= $_SESSION['projectname'];
// Connect to server.
require ("connect.php");
$sql1="SELECT projectsid, projectname, programme, difficult, requirements,
resources, description, contact, gsize, size
FROM projects WHERE projectname = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['projectname']. "<br />";
echo "Programme : " . $row1['programme'] . "<br />";
echo "Difficult : " . $row1['difficult'] . "<br />";
echo "Requirements : " . $row1['requirements'] . "<br />";
echo "Resources : " . $row1['resources'] . "<br />";
echo "Description : " . $row1['description'] . "<br />";
echo "Contact : " . $row1['contact'] . "<br />";
echo "Group size : " . $row1['gsize'] . " " . $row1['size'] . "<br />";
echo "<br /> ";
}
Whenever I click any of the project list items, it only displays the last itemin the list.
I believe this is happening because when the while loop ends the session variable is set to the last one.
Can someone tell me how to fix this?
Thanks
Try using GET variable instead of session
in main.php use
echo "<li> <a href='fullproject.php?projectname=$row2[projectname]'>";
in fullproject.php declare a variable and initialize it to the get variable
$projectname= $_GET['projectname'];
There is a lot wrong with this code.
1) You are trying to send the primary key of the table via 2 different methods in main.php: in the session and via the path of the URL. This is just bad practice - the reason you have access to different conduits for passing data is that they all have different semantics, but even then, sending the same thing twice then there's always going to be a risk that the receiving end may see different values and must terefore have a way of reconciling the discrepancy.
2) You are using the session to pass a transactional data item. This is a bad idea - the data item relates to a specific transition - not to the session. $_SESSION should only be used for storing data relating to the sesion - not to navigation or transactions.
3) The other conduit you provided for passing the data item is by appending it to the path within the URL. While in some circumstances this approach has benefits it also has complications specific to the webserver the code is implemented on - you've not provided any explanation of why you are using this apporach instead of a conventional $_GET or $_POST var.
The reason your code is failing is because you can only store a single value in $_SESSION['projectname']. Even if you stored an array of values in the session, there is no mechanism for the webserver to know which of these values was selected when the code was delivered to the browser.
Your HTML is badly structured and poorly formatted too.
The code you've shown does not match the description you've given (it does not display the project name).
Your code is also wide open to SQL injection attacks.
Change it so that in main.php:
echo "<div id=\"links\">\n";
echo "<ul>\n";
while($row2 = mysql_fetch_assoc($result2)){
echo "<li> <a href=\"fullproject.php?project=\""
. urlencode($row2['projectname']) . "\">"
. htmlentities($row2['projectname']) . "</a>\n";
echo "<br /><em>" . $row2['description'] . "</em></li>";
}
echo "</ul>";
echo "</div>";
And in fullproject.php
<?php
session_start();
require ("connect.php");
$projectname= mysql_real_escape_string($_GET['projectname']);
echo"<a href='fullproject.php?project_id='".$row2['project_id'].
"> $row2['projectName']</a>";
in fullproject.php
$project_id=$_GET['project_id'];
then you can use this id to display contents of that project id from database