$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "igscript";
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT * FROM lastsearches";
$result = mysqli_query($con, $query);
if(mysqli_connect_errno()) {
die("DB Error: " . mysqli_connect_error() . " ( " .mysqli_connect_errno() . ")");
}
while($row = mysqli_fetch_assoc($result)) {
$query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1;";
echo '<center><p>'.$row["name"].'</p> </center><hr>';
if(!mysqli_query($con, $query)) {
die('Error : '.mysqli_error($con));
}
}
$result = mysqli_query($con, $query);
Whenever i use either LIMIT 1, or LIMIT 10; at $query, it has no effect at all. Still displays the same amount of rows. I tried also TOP 10 or TOP(10) as I seen on internet, and i'm getting
Error : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '10 name FROM lastsearches Order By data DESC LIMIT 1' at line 1
$query = "SELECT TOP 10 name FROM lastsearches Order By data DESC";
-> this was the query;
Also the first query worked properly in phpmyadmin, section SQL.
The query from which you are actually displaying results is this one:
$query = "SELECT * FROM lastsearches";
$result = mysqli_query($con, $query);
If you want to limit the results, you need to edit that query instead i.e.
$query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1";
$result = mysqli_query($con, $query);
I'm not sure what you are trying to achieve with the query in your while loop, but it is not doing anything inside that loop so you can probably remove it.
TOP 10 is SQL Server syntax, not MySQL. MySQL uses LIMIT 10 with a very similar effect.
Since I don't see TOP 10 in your code, could it be some interface package that is tied to SQL Server?
Related
I wrote a php file on Raspberry pi, using "$_Get" to receive query query strings including date('Y-m-d').
But I keep getting syntax error and I can't find out which part is wrong....
Here's my php code below:
{
$connect = mysqli_connect("220.133.21.50", "root", "120232", "weather");
$query = 'SELECT * FROM dosensor ORDER BY num ASC';
$result = mysqli_query($connect, $query);
$s="";
while($row = mysqli_fetch_assoc($result))
{$result = mysqli_query($connect, "Select * FROM dosensor ORDER BY num ASC WHERE `recordtime`
<='".$_GET["date('Y-m-d')"]."';");
if ($s=="") $s=$s."['".$row["recordtime"]."',".$row["DO"]."]";
else $s=$s.",['".$row["recordtime"]."',".$row["DO"]."]";
}
}
And here's the query string sending from my Android Studio:
webview.loadUrl("http://"+intent.getStringExtra("ip")+":/linechart.php?
uid="+intent.getStringExtra("uid")+"&pw="+intent.getStringExtra("password")+"&date('Y-m-d')
AND recordtime >= date('Y-m-d',strtotime('-10 day'))");
Could someone tell my which part is causing syntax error?
the error should be on this line
$result = mysqli_query($connect, "Select * FROM dosensor ORDER BY num ASC WHERE `recordtime`
<='".$_GET["date('Y-m-d')"]."';");
I have a simple MySQL query
select * from tutor where verified = 0 and alert_by < '2015-08-05' LIMIT 0,1
Now, running this directly through phpMyAdmin provides the desired results, however, when this query is being executed through a set of PHP statements, it doesn't return anything. Below is my code in PHP
$this_date = date("Y-m-d");
$query = "select * from tutor where verified = 0 and alert_by < '$this_date' LIMIT 0,1";
$contact = mysqli_query($conn, $query);
$row = $contact->fetch_array(MYSQLI_ASSOC);
However, the $row is empty, I can't seem to figure this out. I know this seems trivial, but its a little annoying.
Note: Removing "and alert_by < '$this_date'" from the query, works fine.
Check
Connection
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Use mysqli_query like this
$contact = mysqli_query($con,"select * from tutor where verified = 0 and alert_by < '$this_date' LIMIT 0,1");
Use fetch array like this (Example #2 Procedural style)
$row = mysqli_fetch_array($contact, MYSQLI_ASSOC)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "comp4";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT OrderID, id, items FROM orders WHERE id= $user";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$results = $row["OrderID"]. " " . $row["id"]. $row["items"]. "<br>" ;
}
$loop = implode( " ", $items );
echo $loop;
}
?>
So, I have this code and I'm trying to display OrderID, id and items from the user thats logged in $user=whoever's logged in that's in a different part of my code, however I keep getting an error
Notice: Trying to get property of non-object in C:\xampp\htdocs\myorders.php on line 35
After looking around, I'm still not quite sure how to fix this. Any help is appreciated
I'm guessing your error lies in this line of code:
$sql = "SELECT OrderID, id, items FROM orders WHERE id= $user";
It could be that ID is a reserved keyword of SQL, or that $user is undefined and/or not a numeric value.
Try the following:
$sql = "SELECT `OrderID`, `id`, `items` FROM `orders` WHERE id = $user";
The reason for the error is your query fails. Then you later do the following:
if ($result->num_rows > 0) {
And since the query failed, $result is not an object, and you try to access num_rows of this non-object.
Of course I am only guessing because we need more information :)
I have a php code that will select the last row in mysql using database but this error comes out:
syntax error, unexpected '$result' (T_VARIABLE)
My php code:
$con = mysqli_connect("localhost","root","","productno") or die("Error " . mysqli_error($con));
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT Alibaba FROM records ORDER BY Date DESC LIMIT 1");
if (mysqli_num_rows($result) > 0)
{
$s_Alibaba = mysqli_fetch_row($result);
$sql_Alibaba = $s_Alibaba[0]; //Compare with the last record
}
echo $sql_Alibaba;
Any idea how to fix it? thanks
The problem is, date is a reserved keyword in mysql. Escape it with ` characters around:
$result = mysqli_query($con, "SELECT Alibaba FROM records ORDER BY `Date` DESC LIMIT 1");
See here: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html
I want to execute a query that i saved in my database like this:
ID | NAME | QUERY
1 | show_names | "SELECT names.first, names.last FROM names;"
2 | show_5_cities | "SELECT cities.city FROM city WHERE id = 4;"
Is this possible ?
I am kinda noob in php so plz explain if it is possible.
If I understand you correctly, you have your queries saved in the database in a table and you want to execute those.
Break the problem down: you have two tasks to do:
Query the database for the query you want to run.
Execute that query.
It's a bit meta, but meh :)
WARNING: the mysql_ functions in PHP are deprecated and can be dangerous in the wrong hands.
<?php
if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
die('Could not connect to mysql');
}
if (!mysql_select_db('mysql_dbname', $link)) {
die('Could not select database');
}
$name = "show_5_cities"; // or get the name from somewhere, e.g. $_GET.
$name = mysql_real_escape_string($name); // sanitize, this is important!
$sql = "SELECT `query` FROM `queries` WHERE `name` = '$name'"; // I should be using parameters here...
$result = mysql_query($sql, $link);
if (!$result) {
die("DB Error, could not query the database\n" . mysql_error(););
}
$query2 = mysql_fetch_array($result);
// Improving the code here is an exercise for the reader.
$result = mysql_query($query2[0]);
?>
if you did create a stored procedure/function you can simply use:
mysql_query("Call procedure_name(#params)")
Thats will work. reference here: http://php.net/manual/en/mysqli.quickstart.stored-procedures.php
Querying the table to get the query, then executing that query and looping through the results and outputting the fields
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$RequiredQuery = intval($_REQUEST['RequiredQuery']);
$sql = "SELECT `QUERY` FROM QueryTable WHERE ID = $RequiredQuery";
$result = mysqli_query($link, $sql);
if ($row = mysqli_fetch_assoc($result))
{
$sql = "SELECT `QUERY` FROM QueryTable WHERE ID = $RequiredQuery";
$result = mysqli_query($link, $row['QUERY']);
while ($row2 = mysqli_fetch_assoc($result))
{
foreach($row2 AS $aField=>$aValue)
{
echo "$aField \t $aValue \r\n";
}
}
}
?>
just open the Table and get the individual query in a variable like
$data = mysql_query('SELECT * FROM <the Table that contains your Queries>');
while(($row = mysql_fetch_row($data)) != NULL)
{
$query = $row['Query'];
mysql_query($query); // The Query from the Table will be Executed Individually in a loop
}
if you want to execute a single query from the table, you have to select the query using WHERE Clause.