I want use one column value in mysqli query but I am not able to find way of use it. My query is like this
$result = mysqli_query($mysqli,"SELECT * FROM tbl_settings");
while($row = $result->fetch_assoc()){
if($row['automatic'] ==on){
$number= "$result['automatic']";
echo $number;
$sql = mysqli_query($mysqli,"SELECT _quid FROM tbl_quotes where qu_status=0 ORDER BY _quid ASC LIMIT 5");
while($result = mysqli_fetch_array($sql)){
$qu_time = getDatetimeNow();
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=1,qu_favorite=0, qu_time='$qu_time' where _quid=".$result['_quid']." ORDER BY _quid ASC");
}
I want get value of column automatic_number and want use it as LIMIT like this
$sql = mysqli_query($mysqli,"SELECT _quid FROM tbl_quotes where qu_status=0 ORDER BY _quid ASC LIMIT $number");
I am trying from half hour but not able to find way of it. Let me know if any expert can help me. Thanks
mistake
if($row['automatic'] ==on)
correct:
if($row['automatic'] =='on')
if you want to assign value of $result['automatic'] then
$number=$result['automatic'];
this may be he reason you are not getting limit
in here $sql = mysqli_query($mysqli,"SELECT _quid FROM tbl_quotes where qu_status=0 ORDER BY _quid ASC LIMIT 6");
query:
$sqlquery = "SELECT _quid FROM tbl_quotes where qu_status=0 ORDER BY _quid ASC LIMIT " .$number;
execution:
$sql = mysqli_query($mysqli, $sqlquery);
if you want to use value the atomatic_number in the limit where the atomatic_number is the table of the tbl_settings then just change
$number=$result['automatic'];
to
$number=$result['automatic_number'];
then
$sql = mysqli_query($mysqli,"SELECT _quid FROM tbl_quotes where qu_status=0 ORDER BY _quid ASC LIMIT $number");
I hope this is what you are looking for.
Related
I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";
i have a question which is my limit statement didn't work i want the content select from database and limit the show content in 40 only but it didn't work
here is my SQL statement with php code
$chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];
$sql="SELECT * FROM chatroom_chat WHERE chatroom_id ='$chatroomID'";
$result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result1)) {
$chat = $row['chat_id'];
$sql3 ="SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 0,40
) sub
ORDER BY id ASC ";
$getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));
/*here have statement to get username*/
while($row3 = mysqli_fetch_array($getChatData)) {
echo "<div>all content</div>";
}
}
does my code have any syntax error? i no sure why it didn't work
SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 40
) sub
ORDER BY id ASC
I want to use php to determine which table my information is coming from but don't know how to place the PHP correctly into the statement.
Say I have the tables:
page1_articles
page2_articles
page3_articles
And I give the page an ID of $page = "page1" or $page = "page2" or $page = "page3"
How can I use this in my SELECT statement:
$get_articles_sql = "SELECT * FROM $page_articles ORDER BY views DESC LIMIT 5";
This should work for you:
$get_articles_sql = "SELECT * FROM " . $page . "_articles ORDER BY views DESC LIMIT 5";
With this you concat $page which could be page1 or page2 or page3 with the string _articles
Edit after comment:
Escape your variable if you didn't do that already! with this:
mysql_real_escape_string($page);
this will work
$get_articles_sql = "SELECT * FROM ".$page_articles." ORDER BY views DESC LIMIT 5";
this code will work better
$get_articles_sql = "SELECT * FROM ".$page_articles." ORDER BY views DESC LIMIT 5";
this code is be best
$get_articles_sql = "SELECT * FROM {$page_articles} ORDER BY views DESC LIMIT 5";
I'm trying to convert a script from basic sql to pdo. I'm not so good at it, but i have come this far that my pdo returns all the rows in the database (limit 12) i can make 2 statements one without the limit and one with the limit. but in the sql that is originally there, it uses QL_CALC_FOUND_ROWS what this does I think, is that i returns the total rows, and the limit rows, so that the query is faster.
How can i do it with PDO ?
CODE:
<?
// NORMAL SQL
$result = mysql_query('SELECT SQL_CALC_FOUND_ROWS * FROM photos ORDER BY id ASC limit 12');
$row_object = mysql_query('SELECT Found_Rows() AS rowcount');
$row_object = mysql_fetch_object($row_object);
$actual_row_count = $row_object->rowcount;
// PDO
$result = $pdo->prepare('SELECT * FROM photos ORDER BY id ASC limit 12');
$result->execute();
$TOTALrows = $result->rowCount();
?>
EDIT:
Tried some things but this won't work:
$result = $pdo->prepare('SELECT SQL_CALC_FOUND_ROWS * FROM photos ORDER BY id ASC limit 12');
$result->execute();
$resultALL = $pdo->prepare('SELECT Found_Rows() AS rowcount');
$resultALL->execute();
$resultALL->fetch(PDO::FETCH_OBJ);
$actual_row_count = $resultALL->rowcount;
EDIT2: Still no success..
$result = $pdo->query('SELECT SQL_CALC_FOUND_ROWS * FROM photos ORDER BY id ASC limit 12');
$resultALL = $pdo->query('SELECT Found_Rows() AS rowcount');
$resultALL->fetch(PDO::FETCH_OBJ);
$actual_row_count = $resultALL->rowcount;
print_r($actual_row_count);
echo $actual_row_count;
Doesn't echo anything.
Credits also go to Explosion Pills here...
When you're not feeding your query with external input, you don't really have to prepare your statement:
$result = $pdo->query("SELECT SQL_CALC_FOUND_ROWS * FROM photos ORDER BY id ASC LIMIT 12");
This should work!
I wonder... is this possible to select a random row in my DB where only the record that contains field->Status = 0 ? because i only need a row that contains 0 in the field name=Status. if the status contains = 1 , then the row will not belong in the randomization.
my code in the query is
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1");<br>
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1 Where Status=0");
But it does not work.. i appreciate your replies.. thank you very much!
Try below code:
$result = mysql_query("SELECT * FROM `".$haha."` Where Status=0 ORDER BY RAND() limit 0,1");
Your where clause should be before then the order by clause.
And want to know what is the value of $haha.
You use the wrong syntax. The good one:
$result = mysql_query( " SELECT * FROM `$haha` Where Status=0 ORDER BY RAND() limit 1");