Query not being executed inside php class function - php

Hi I am learning how to make a simple mvc with php, but seem to be running into a little trouble with executing the queries inside the class function.
When I take the codes out of the class function they work fine. But they dont work when the code is inside. Any ideas would be appreciated thanks.
<?php
include_once("model/book.php");
class Model {
public function getBookList()
{
$conn=mysqli_connect("localhost","Ory4n","","test");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Title, Author, Description FROM books";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<h2 class="word">'.$row["Title"].'</h2>';
echo '<h4 class="word">'.$row["Author"].'</h4>';
echo '<p class="word">'.$row["Description"].'</p>';
}
} else {
echo "0 results";
}
} // close function
public function getBook($title)
{
getBookList();
}
}
?>

Summarizing the comments...
<?php
class Model {
/**
* #return array Empty array in case of no results
*/
public function getBookList() {
$conn=mysqli_connect("localhost","Ory4n","","test");
if($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Title, Author, Description FROM books";
$result = $conn->query($sql);
$books = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$book = new stdClass();
$book->title = $row['Title'];
$book->author = $row['Author'];
$book->description = $row['Description'];
$books[] = $book;
}
}
return $books;
}
}
//Demo - how to use
$model = new Model();
$books = $model->getBookList();
foreach($books as $book) {
echo '<h2 class="word">'.$book->title.'</h2>';
echo '<h4 class="word">'.$book->author.'</h4>';
echo '<p class="word">'.$book->description.'</p>';
}
?>

Related

Mysql delete a row php

I have a MySQL table and i would like to delete a row in my table and after deleting the row the result must show all data left in my table .
<?php
include 'Connection.php';
// Create connection
$con= mysqli_connect($host,$user,$pass,$db);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT * FROM productos";
$result = $con->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc())
{
$tem = $row;
$json = json_encode(array("productos"=>$tem));
}
} else {
echo "No Results Found.";
}
echo $json;
$con->close();
?>
This is my select code... now i don't know how create the delete function..
Any help please.
You are overwritting, make array of rows and then echo json_encode at last
$items = array();
if ($result->num_rows >0) {
while($row = $result->fetch_assoc())
{
$items[] = $row;
}
} else {
echo "No Results Found.";
}
echo json_encode(array("productos"=>$items));
$con->close();

How can I make a submenu

I have the next database:
I have the below function:
<?php
$connection = mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("database1", $connection) or die(mysql_error());
function loop_array($array = array(), $parent_id = 0)
{
if (!empty($array[$parent_id])) {
echo '<ul>';
foreach ($array[$parent_id] as $items) {
echo '<li>';
echo ''.$items['title'].'';
loop_array($array, $items['id']);
echo '</li>';
}
}
}
function displays_menus_revised()
{
$sql = "SELECT * FROM pages";
$query = mysql_query($sql) or die(mysql_error());
$array = array();
if (mysql_num_rows($query)) {
while ($rows = mysql_fetch_array($query)) {
$array[$rows['parent_id']][] = $rows;
}
loop_array($array);
}
}
?>
My problem is the funtion show the records like in the left picture. I want the function to show the records like in the right picture. Can you tell me where is the problem? What i must change that the function show records like in the right picture? Thanks.
So nice code, with such a small slip. Only the closing 'ul' is missing.
function loop_array($array = array(), $parent_id = 0)
{
if (!empty($array[$parent_id])) {
echo '<ul>';
foreach ($array[$parent_id] as $items) {
echo '<li>';
echo ''.$items['title'].'';
loop_array($array, $items['id']);
echo '</li>';
}
echo '</ul>';
}
}

Replace string at particular position in function In PHP?

I want to Print some dynamically data from my function to passing string in function like this.
My Function
function mysql_funX_Repeter($query,$itemtemplet)
{
$this->mysql_funX_connect();
$result = mysql_query($query) or die("Repeter Query Error.");
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{
$newtempelt = str_replace("Eval", '$row', $itemtemplet);
while ($row = mysql_fetch_array($result))
{
echo $newtempelt;
}
}
}
Passing Value In Function
<ul>
<?php
$rptvalue="<li><a href=''>Eval['name']</a></li>";
$myFun->mysql_funX_Repeter("Select name from tbldemo",$rptvalue);
?>
</ul>
My Output
$row['name']
$row['name']
$row['name']
I want Output
raj
ram
prince
But it's Show me only variable but not show it's database value.
How to solve this...!!!
I would change a little bit your method.
function mysql_funX_Repeter($query,$itemtemplet)
{
$this->mysql_funX_connect();
$result = mysql_query($query) or die("Repeter Query Error.");
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{
while ($row = mysql_fetch_array($result))
{
echo "<li><a href=''>".$row[$itemtemplet]."</a></li>";
}
}
}
And of course, your method invokation:
<ul>
<?php
$rptvalue="name";
$myFun->mysql_funX_Repeter("Select ".$rptvalue." from tbldemo",$rptvalue);
?>
</ul>
And that's how I'd do it to avoid nasty evals. :)
function mysql_funX_Repeter($query,$itemtemplet)
{
$html='';
$this->mysql_funX_connect();
$result = mysql_query($query) or die("Repeter Query Error.");
$num_rows=mysql_num_rows($result);
if($num_rows){
while ($row = mysql_fetch_array($result))
{
$newtempelt = str_replace("Eval", $row['column_name'], $itemtemplet);
$html.="<li><a href=''>".$newtempelt."</a></li>";
}
}
return $html;
}
//calling method mysql_funX_Repeter
<ul>
<?php
$rptvalue="name";
echo $myFun->mysql_funX_Repeter("Select ".$rptvalue." from tbldemo",$rptvalue);
?>
</ul>
Developer My Problem Was Solved.
Thanks for Reply.
My Function
//This Repeter Function Created By Priyank Patel
function mysql_funX_Repeter($query,$itemtemplet)
{
$this->mysql_funX_connect();
$result = mysql_query($query) or die("Repeter Query Error.");
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{
preg_match_all('/{([^}]*)}/', $itemtemplet, $matches);
$select = '';
while($row = mysql_fetch_assoc($result)){
$aux = $itemtemplet;
for($i = 0; $i < count($matches[0]); $i++){
$aux = str_replace($matches[0][$i], $row[$matches[1][$i]],$aux);
}
$select .= $aux."\n";
}
mysql_close();
return $select;
}
}
Calling Style
<ul><?php $templet="<li>{name}</li>";echo $myFun->mysql_funX_Repeter("Select * from tbldemo",$templet);?></ul>

trouble retrieving data from sql database with php

I'm pretty new to php, and I'm teaching myself. I've looked at a few different resources, and the php script I have now doesn't return any critical errors when executed, but its not returning the data from the table.
<?php
$connect = mysqli_connect("localhost","*","*","*");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
$fetch = mysqli_fetch_array($rs);
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
echo $fetch;
mysqli_close($connect);
echo "hello";
?>
you have double entry:
$fetch = mysqli_fetch_array($rs); //<--- remove this as you are calling it again in the while loop
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
Check this
$connect = mysqli_connect("localhost","turlough","samus1","comments");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
if($rs)
{
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
}
else
{
// no results from query
}
mysqli_close($connect);
}

PHP Query + IF ELSE

I Have a PHP file doing a query
function getFiles()
{
$solnid = $_GET[id];
$query = mysql_query("SELECT * FROM library WHERE libr_solutionId = '$solnid' AND libr_deleted IS NULL") or die(mysql_error());
$result = mysql_query($query);
if (mysql_num_rows($result)==0) {
echo "<br>No Related Links";
} else {
while($library = mysql_fetch_assoc($query)) {
echo "<span style=\"margin-bottom: 5px;\"><img src=\"images/icon-download-02.png\" align=\"absmiddle\" style=\"margin-right: 5px;\"><span>".$library[libr_c_title]."</span></span><br>";
echo "<br>";
}
}
}
I have tested the query in PHPMyAdmin and I get a result however when running the function in my page it only displays the 'No Related Links' statement any ideas anyone?
Here's the fixed code. :) The problem was that you used mysql_query on a Resource (the first mysql_query), and that will give 0 rows, or an exception..
function getFiles()
{
$solnid = $_GET['id'];
$result = mysql_query("SELECT * FROM library WHERE libr_solutionId = '$solnid' AND libr_deleted IS NULL") or die(mysql_error());
if (mysql_num_rows($result)==0) {
echo "<br>No Related Links";
} else {
while($library = mysql_fetch_assoc($result)) {
echo "<span style=\"margin-bottom: 5px;\"><img src=\"images/icon-download-02.png\" align=\"absmiddle\" style=\"margin-right: 5px;\"><span>".$library['libr_c_title']."</span></span><br>";
echo "<br>";
}
}
}
change your function to (change to lines)
function getFiles()
{
$solnid = $_GET[id];
$result = mysql_query("SELECT * FROM library WHERE libr_solutionId = '$solnid' AND libr_deleted IS NULL") or die(mysql_error());
if (mysql_num_rows($result)==0) {
echo "<br>No Related Links";
} else {
while($library = mysql_fetch_assoc($query)) {
echo "<span style=\"margin-bottom: 5px;\"><img src=\"images/icon-download-02.png\" align=\"absmiddle\" style=\"margin-right: 5px;\"><span>".$library[libr_c_title]."</span></span><br>";
echo "<br>";
}
}
}

Categories