This question already has answers here:
Read one column from one row from a MySQL database
(8 answers)
Closed 8 years ago.
I am having an issue declaring a PHP variable as a MySQL result.
Code:
$num = mysql_query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
What it does it retrieve the latest entry in the database, which is an int, and I want it to store into the PHP variable $num. Is this possible?
Is this possible?
Yes, that is possible.
You should, however, use mysqli or PDO instead of the old mysql_ functions.
See: Why shouldn't I use mysql_* functions in PHP?
An example of how it could look:
$result = $mysqli->query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
$num = $result->fetch_object()->num;
Related
This question already has answers here:
What is the difference between varchar and nvarchar?
(21 answers)
Closed 5 years ago.
how to select 主页报修上广告
$sql = "SELECT text,pic FROM newstable where type='在线报修上广告' order by id desc limit 0,1";
but you may find the return value is null
But i try to use this
$sql = "SELECT text,pic FROM newstable where type=N'在线报修上广告' order by id desc limit 0,1";
there is N before '在线报修上广告' ------->>>>>but it works....what's the meaning of this 'N'!!
What is the meaning of the prefix N in T-SQL statements?
It#s declaring your varchar as nvarchar which is using a unicode encoding page. Otherwise it will be converted to the default encoding page of your database. Basically it's prefered to use only varchar when intended (like email addresses)
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I have the following:
<?php
include 'connect.php';
//get average from all reviews
$allreviews = "SELECT round(avg(Stars_overall),0) AS average FROM (SELECT Stars_overall FROM Reviews WHERE Meal_ID = 1 ORDER BY Order_ID DESC LIMIT 5) AS Average";
$getresult = mysqli_query($conn, $allreviews);
$row3 = mysql_fetch_assoc($getresult);
echo "aaaaa" .$row3['average']. "bbbbb";
mysqli_close($conn);
?>
Unfortunately it is not working, any clue why? It is for a review system, i'm trying to display an average of star ratings stored in database. The first sql gets all the ratings for a specific meal and the second sql calculates the average. I then want to be able to display the average
You are using mysqli, therefore you must use mysqli_fetch_assoc and not mysql_fetch_assoc.
This question already has answers here:
How to show the last queries executed on MySQL?
(10 answers)
Closed 8 years ago.
Is there a way to see the previous query entered in a MySQL server through PHP? I do not want the results of the query, I want the actual plaintext query entered.
No. By default PHP disconnects and closes any mysql connections when the script ends, so there will be nothing left to view when the NEXT connection comes in. And since you have to send the query over each time anyways, by definition you should have that query available to you already. If you need the query text kept, then you should put it into a variable first
e.g. instead of
$result = mysql_query('SELECT ...');
you should be doing
$sql = 'SELECT ...';
$result = mysql_query($sql);
This question already has answers here:
Anyway to Limit MySQL Query Execution time?
(4 answers)
Closed 9 years ago.
How can I limit the runtime of a SQL I call with a php file to 2 seconds
here an example I am using
$output = '';
$sql = "SELECT epf_application.view_url FROM epf_application INNER JOIN app_uri ON epf_application.application_id=app_uri.application_id WHERE app_uri.uri = '$URL' LIMIT 1";
foreach ($xpdo->query($sql) as $row) {
$output .= $row['view_url'];
}
I dont want to limit the whole script runtime only the request on the database
You can't! As the query is blocking and you'll only know how long it took when it's done.
So you need to optimize your Queries if they are too slow. You do that by analyzing and testing and by using INDEXES in the right places and playing with EXPLAIN.
This question already has answers here:
MySQL offset infinite rows
(10 answers)
Closed 9 years ago.
I have the following SQL code;
SELECT * FROM myTable WHERE id = 1 LIMIT 4, 10;
I would like the sql to run from result 4 but have no end limit, so I would like the script to get all the results from 4 onwards. Is this possible?
Any help appreciated.
You can use the OFFSET option in your query. Like this
SELECT *
FROM myTable
WHERE id = 1
OFFSET 4
SELECT * FROM tbl LIMIT 4, 18446744073709551615;
resource mysql docs.