How to combine to php's (sql) - php

I need to combine these two codes,more likely the sql. I'm sitting on it about hours and can't get it.
<?php
$mysqli2 = new mysqli("Edudb", "mct", "QwSkepticx97!");
$mysqli2->select_db("maturita");
$ask2 = $mysqli2->query("SELECT * FROM gallery");
if(mysqli_num_rows($ask2)>0)
{
while ($row2=mysqli_fetch_object($ask2))
{
echo "<a href='show-gall.php?gallery=$row2->gall_id'>$row2->name<br></a</span>";
}
}
?>
and this one
<?php
$conn = mysqli_connect("Edudb", "mct", "QwSkepticx97!", "maturita");
$sql="SELECT g.name, i.img_thumb FROM gallery g JOIN images i ON g.gall_id = i.gall_id GROUP BY g.gall_id ";
$query=mysqli_query($conn, $sql);
if(mysqli_num_rows($query)>0)
{
while ($row=mysqli_fetch_object($query))
{
echo "<div class='view'>";
echo "<a href='show-gall.php?gallery=$row->gall_id'><img src='$row->img_thumb' alt=''></a>";
echo "<br/>";
echo "<a href='show-gall.php?gallery=$row->gall_id'>$row->name</a>";
echo "<br/>";
echo "</div>";
}
}
?>
I'll be thankful for any suggestions

Try this one
include('here put the name of one file of two ');
Then put this statement in the other one.
Now the other file that has include() statement shows everything in the second one.

Related

ELSE condition is not working in the while loop in PHP

<?php
$conn=mysqli_connect("localhost","id6755695_artemi8","sharanod"
,"id6755695_user_info");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$department = $_POST["department"];
$year = $_POST["year"];
$sem = $_POST["semester"];
$reg = $_POST["regulation"];
$sql = "SELECT book_name, author, edition, image FROM dept_search WHERE
department='$department' AND year='$year' AND semester='$sem' AND
regulations='$reg' ";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if($row)
{
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
else
{
echo "sorry book not found";
}
}
mysqli_close($conn);
?>
please help me with this code,i am building a library management system.. The thing is I should be able to display the books if the given values are present i have in the database if not book not found must be displayed but in while loop after if, else does not runs.....
As others have pointed out, your else statement will never run. If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.
What you can do is, check beforehand if the query returned actual results, like so:
$result=mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
}else{
echo "Sorry book not found";
}
You can try with mysqli_num_rows .. sample code as follows :
$rowcount=mysqli_num_rows($conn,$sql);
if($rowcount!=0){
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<br>";
?>
You are looping through all the rows returned from the "mysqli_fetch..." command. Your "if" and "else" is useless -- you will always have rows. If you get no rows, you do not even enter the body of the while loop.
You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.
All you need to do is that you have to change if the condition from if($row) to if($other_condition)
Currently, you are just checking either there is something inside $row, and this condition will never be wrong unless you will assign it null. Because where $row will have something then while loop will be executed, and when while loop will be executed then if condition will be executed.
you have to simply one thing, that is to change if condition like given below...
if($row['value'] == 'something')

Panel List Php not showing all data

I am trying to create a panel list that shows certain rows from my database, however when I'm trying to call it, it only shows 1 data in the row even though i limit it by 5.
Here is my script:
<?php
$link = mysqli_connect("localhost", "root", "", "test");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM updates order by id desc limit 5";
if($result = mysqli_query($link, $sql)){
if($list=mysqli_fetch_array($result)){
echo "<div class=\"container\">";
echo "<table>";
echo "<tr>";
echo "</tr>";
echo "<div class=\"col-md-4\">";
echo "<div class=\"panel panel-primary\">";
echo "<div class=\"panel-heading\">Updates";
echo "</div>";
echo "<ul class=\"list-group\">";
echo "<li class=\"list-group-item\">";
echo "<b><h4>{$list["updates"]}</b></h4>";
echo "</li>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
echo "</table>";
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
That's because you are fetching only once, here:
if ($list=mysqli_fetch_array($result) )
Instead wrap this up inside a loop like this:
$row_count = mysqli_num_rows($result);
if ($row_count <= 0) {
echo "No records matching your query were found.";
}
else {
// you have results
// panel starts here
while ($list = mysqli_fetch_array($result)) {
// do some magic with your results
}
// panel ends here
mysqli_free_result($result);
}
I think you got the idea, now you can do the rest. If not let us know again. Happy coding!
You use sql to get five data, but you use mysqli_fetch_array() function only took out a line.
error line 8;

php echo if query is empty

I'm querying a database but when the result is empty i want to output a table row displaying "nothing to display" but the if seems to always return true.
Here's my code...
$priorityincidentsQ = mysql_query("SELECT * FROM applications WHERE pi >= ('2') ");
while($priorityincidentsR = mysql_fetch_object($priorityincidentsQ))
{
if (empty($priorityincidentsR)) {
echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";
} else {
echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
echo "<td class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
}
}
Use mysqli_num_rows() to check is there any result:
$conn = mysqli_connect($host, $user, $password, $database);
$priorityincidentsQ = mysqli_query($conn, "SELECT * FROM applications WHERE pi >= ('2') ");
if (mysqli_num_rows($priorityincidentsQ) > 0){
while ($priorityincidentsR = mysqli_fetch_object($priorityincidentsQ)) {
echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
echo "<td class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
}
}else{
echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";
}
And yes, better use mysqli_* functions instead of mysql_*.
Still never figured out why this wouldn't work out for me this way, I tried the query directly in SQL workbench and everything looks how it should, I ended up solving the problem like this.
<!-- priority incidents-->
<?php
$priorityincidentsQ = mysql_query("SELECT * FROM applications WHERE pi >= ('1') ");
while($priorityincidentsR = mysql_fetch_object($priorityincidentsQ))
{
echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
echo "<td class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
}
?>
<!-- if no incidents-->
<?php
$incidentNumberofRowsQ = mysql_query("SELECT COUNT(*)numberofrows FROM applications WHERE pi >= ('1') ");
while($incidentNumberofRowsR = mysql_fetch_object($incidentNumberofRowsQ))
{
if ($incidentNumberofRowsR->numberofrows == '0')
{
echo "<tr><td class=\"closedcallscell centered\"><b>Currently no priority incidents</b></td>";
}
}
?>
may seem a rather silly way of going about it but atleast it works. Thanks all for the help. :)

Pasing data/values

I have this search php code in which i made the formnumber into a hyperlink. All I wanna do is once i click a formnumber it will retrieve all ta data of tha click formnumber into the database and echos it.
$dbname = "vianney300";
$SRCHDATA = $_POST["srch"];
if($connection===FALSE)
echo "<p> Connection Failed. ". mysql_error()."</p>";
else{
if(mysql_select_db( $dbname)===FALSE)
echo "<p>Could not select database.</p>";
}
$query = "SELECT * FROM profile WHERE LastName='$SRCHDATA'";
$result = #mysql_query($query);
if ($result===FALSE){
echo "<p>Unable to execute query.</p>";
}
else {
while (($row = mysql_fetch_assoc($result))!==FALSE){
echo "Form no: ";
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
echo "Last name: ";
echo "{$row['LastName']}</br>";
echo "First name: ";
echo "{$row['FirstName']}</br></br>";
If you want to send a form number to individual page then you have to do like this inside your while function :
echo "<a href='individual.php?form_no={$row['FormNo']}'>{$row['FormNo']}</a></br>";
And on individual.php you will retrieve form number using $_GET['form_no']
individual.php
<?php
echo $_GET['form_no'];
// Using this form number you can fetch the data from database
?>
You have made a small mistake on this line
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
it should be
echo "<a href='individual.php?formnumber={$row['FormNo']}'>{$row['FormNo']}</a></br>";
ok.then what should i put to echo in the individual.php
Thats another question really, but
code individual.php
<?php
if ( isset($_GET['formnumber']) ) {
echo 'Recieved parameter "formnumber" in the GET array = ' . $_GET['formnumber'];
} else {
echo 'No parameter passed';
}
?>

How to ckeck and display an image if there is one

I would like to ask for some help because I am stuck at that problem. I have a MySQL DB and I am trying to create some kind of a blog system. The problem which occure to me is this:
I would like to check is there is an image tag if not not to display it. The system works perfect without images but it would be nice if I new where I messed it up. Here is my code up to now:
function outputStory($article, $only_snippet = FALSE){
global $conn;
if($article){
$sql = "SELECT ar.*, usr.name FROM cms_articles ar LEFT OUTER JOIN cms_users usr ON ar.author_id = usr.user_id WHERE ar.article_id = " . $article;
$result = mysql_query($sql, $conn);
if($row = mysql_fetch_array($result)){
echo "<h2>" . htmlspecialchars($row['title']) . "</h2>\n";
echo"<h5><div class='byLine'>От:" . htmlspecialchars($row['name']) . "</div>";
echo "<div class='pubdate'>";
if($row['is_published'] == 1){
echo date("F j, Y",strtotime($row['date_published']));
} else {
echo "No articles are published yet!";
}
echo "</div></h5>\n";
if ($only_snippet){
echo "<p>\n";
echo nl2br(trimBody($row['body']));
// I think I messed this statement alot but don't know how to make it work properly :S
if(hasPostImage() == true){
$getPostImage = "SELECT * FROM cms_images";
$getImgResult = mysql_query($getPostImage,$conn);
$rowImg = mysql_fetch_array($getImgResult);
echo"<img src='".$rowImg['img_src']."' alt='".$rowImg['img_desc']."'>";
} else {
echo '<img style="display:none">';
}
echo "</p>\n";
echo "<h4>More...</h4><br>\n";
} else {
echo "<p>\n";
echo nl2br($row['body']);
echo "</p>\n";
}
}
}
}
I hope the question is not silly or something else and thank you for the help in advance.
There are many ways for doing this, my first approach is to first check you are getting file in $_FILES is its okay then there is functions available in PHP to check file size means this will give you files size in bytes and height and width as well. File Size()
also getimagesize() if this criteria matches your criteria then proceed further

Categories