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";
Related
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.
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 have the following code which selects information from one random row.
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 1 ");
while($rows = mysql_fetch_array($query))
{
$question = $rows['question'];
$hint = $rows['hint'];
$level = $rows['level'];
$keyword = $rows['keyword'];
$showme = $rows['showme'];
$picture_path = $rows['picture_path'];
}
This works well for me but I now I need to be able to select two more DIFFERENT pictures from the picture_path column and assign them to variables. Again, all three rows need to be different.
Any tips for a newbie on how to do this?
Just change your query as follows:
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
You are ordering by ORD() so it will give you different records.
No, new modification, just change the limit to 3 (whatever your need).
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
As your are already getting random values with order by clause it will always return different values so you just need to edit your limit value and you are done!
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
i'm trying to Order by Desending and want to limit 30 in Query
PHP CODE
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 30;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
$query_pag_data = "SELECT * from titles LIMIT $start, $per_page ORDER BY id DESC";
ERROR : MySql ErrorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id DESC' at line 1
PS: i'm using pagination... so limiting 30 result like this
correct syntax is:
SELECT * from titles
ORDER BY id DESC
LIMIT $start, $per_page
LIMIT at the end of the query.
change positions or LIMIT and ORDER BY, like:
$query_pag_data = "SELECT * from titles ORDER BY id DESC LIMIT $start, $per_page";
You have to put the limit at the end of the query for proper syntax:
$query_pag_data = "SELECT * from titles ORDER BY id DESC LIMIT $start, $per_page";
You need to put ORDER BY statement first before LIMIT
Correct syntax is as follows:
SELECT * FROM *table_name* WHERE *condition* ORBER BY *field_name* LIMIT *limit*;
LIMIT statement should always come at the end of the query.
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");