Making Query's Result #1 As First Added? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Basically I have this function that is meant for my forum where it gets all the replies to a certain topic. The problem is it will echo the latest reply first, and it will display the first reply last.
Does that make sense?
I basically want this to be backwards.
function getReply($id){
$q2 = #mysql_query("SELECT * FROM `reply` where `reply_id`='$id'");
if(!$q2){
echo 'Error: '.mysql_error();
}
echo '<ul class="list-group">';
while($res2 = mysql_fetch_array($q2)) {
echo '<center><br />
<div class="container">
<li class="list-group-item">
'.$res2['reply_content'].'
</li>
<li class="list-group-item">
Posted By <strong>'.getOwner($res2['reply_by']).'</stong>
on <strong>'.$res2['reply_date'].'</strong><br />
</li>
</div>';
}
}

function getReply($id){
$sql = "
SELECT *
FROM `reply`
WHERE `reply_id` = '$id'
ORDER BY `id` ASC
";
$q2 = mysql_query($sql) or die('Error: '.mysql_error());
// ...
}

Related

what is error in following code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
$rowcount=mysqli_num_rows($result);
if($rowcount==1)
{
while($row = mysqli_fetch_array($result))
{
$libid=$row['libid'];
$regno= $row['regno'] ;
$name= $row['stuname'] ;
$branch= $row['branch'] ;
$semester= $row['semester'] ;
$section= $row['section'] ;
$yearofadm= $row['yearofadm'];
}
}
Dont post anything directly in database as its a threat to data security (SQL Injection)
$libid = $_POST['libid'];
$libid = mysqli_real_escape_string($connection, $libid);
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".$libid."'");
Make sure that your mysql field is really libsutdent. Seems like it should be libstudent.
Then Place {} around your Post variable. ie {$_POST[\"libid\"]}.
Conversely you can place another step in your code like:
$libid = $_POST["libid"];
I think you can do without the quotes around libid, but I always think it reads better to add them.
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
Should be
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".mysql_real_escape_string($_POST['libid'])."'");

PHP: How to check if no rows are returned [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
With the following PHP code, how do i check if there's no row retuned so i can echo some message?
PHP:
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
while($row = mysql_fetch_array($Result)){
echo '<span class="classPid" style="display:none">'.$row['pid'].'</span>';
}
?>
Thanks
There are a number of ways to do this. There is a specific function mysql_num_rows() which will return the number of rows returned.
mysql_num_rows($Result);
This will return 0 if there are no rows affected or returned.
You could also create some conditions using mysql_fetch_array. mysql_fetch_array will return FALSE if there are no rows.
On a separate note it would be a good idea to update your connection
and functions to mysqli as mysql is no depreciated.
See docs on mysql_num_rows().
http://www.php.net/mysql_num_rows
Something like this
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
$i=0;
while($row = mysql_fetch_array($Result)){
echo "<span class='classPid' style='display:none'>".$row['pid']."</span>";
$i++;
}
if($i==0){
echo "No rows found";
}
?>

Is this the correct way to call a sql database? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hey I was wondering if my ip_address is 0 and I want to check all tables that have a 0 ip_address and then execute a function of that. So something like this code:
mysql_connect(db_server, db_user, db_pass);
$ipzerouseroffline = mysql_db_query(db_name,"SELECT ip_address = '0' FROM user");
if ( $ipzerouseroffline == 0 ) {
//THEN CODE GOES HERE
}
Is that the correct way to write? If the ip_address is zero then the code passes.
I think this is what you want:
$con = mysqli_connect(db_server, db_user, db_pass, db_name);
$result = mysqli_query($con,"SELECT * FROM user WHERE ip_address = '0'");
while($row = mysqli_fetch_array($result)){
//CODE
}
mysqli_close($con);

Can this code work or not [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can the code below work or not to retrieve data from database and display.. I know that the sql4 can't work in a mysql database can the php code below make it work or not.
<?php
include 'connect.php';
$sql2 = "SELECT * from pm Order by mid";
while ($row3 = mysql_fetch_array($sql2)) {
$sql4 = mysql_query("SELECT * FROM reply ORDER BY mid =".$row3["$mid"]." ");
$row4 = mysql_fetch_array($sql4);
echo"<trid='".$mid."'><td><img src='a/$name' width='150' height='100' /></td> <td>".$row['mid']."</td></tr><tr><td>".$row['reply']."</td></tr>";
}
?>
esp. this mysql or mysql and php combined. Basically is the code below improper. I think it is.
<?php
$sql4 = mysql_query("SELECT * FROM reply ORDER BY mid =".$row3["$mid"]." ");
?>
Put the following at the top of your PHP file and try it out yourself:
<?php
error_reporting(-1);
ini_set("display_errors", true);
// Your code goes here
?>
(I think the code is self-explaining)
Try...
$sql2 = "SELECT * from pm Order by mid";
$result = mysql_query($sql2); <- MISSING THIS LINE
while ($row3 = mysql_fetch_array($result))
{
// OTHER CODE HERE
}

Reading data from mysql table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database table, I have a lot of questions ,each having a category . Say, there are 100 questions and some number of categories which I don't know . I want to know the number and the names of the categories.
Please tell the way to do it in php.
You can try something like this
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT category_name FROM your_table GROUP BY category_name");
$count = mysqli_num_rows($result); // no of categories
while ($row=mysqli_fetch_row($result)) {
echo $row[0]; // printing category name
}
How about...
<?php
$mysqli = mysqli_connect( /* info */) or die ("MySQL error");
$result = mysqli_query("SELECT COUNT(DISTINCT `category`)");
echo $result;
?>
That is assuming categories are all in a single column.

Categories