<?php
//Step1
$db = mysqli_connect('localhost','root','','form')
or die('Error connecting to MySQL server.');
//Step2
$query = "SELECT * FROM info";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result)) {
$nam=$row['name'];
$fnam=$row['father'];
$date=$row['date'];
$aadh=$row['aadhaar'];
}
1.In this code it fetches all the values that are stored in the database . But I am in need of the code for fetching the top most row from the database.
2.The query with top attribute is also not working. It gives the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 * FROM info LIMIT 0, 30' at line 1
This is not a PHP issue, it's rather a matter of your MySQL query.
This will return everything because of the * usage.
$query = "SELECT * FROM info";
You can try this instead:
$query = "SELECT *
FROM info
WHERE unique_id = (SELECT MAX(unique_id) FROM info)";
Related
I'm Updating with mysql over ODBC Filemaker Table.
When a field contains o'reilly or example'two I get this error message:
Warning: odbc_exec(): SQL error: [FileMaker][FileMaker] FQL0001/(1:80):
There is an error in the syntax of the query., SQL state 42000 in SQLExecDirect in C:\fm_1.php on line 49
and using addslashes() does not work.
thank you!
this is my code:
<?php
$conn = odbc_connect("DSN=Server;Database=TEST;UID=odbc;PWD=1234", "odbc", "1234");
if ($conn)
echo "\nConnection established.";
else
die("\nConnection could not be established.");
$result = odbc_exec($conn, "SELECT ID_MH, MH_Name FROM myTable WHERE MH_Name LIKE '%EXAMPLE'");
while ($row = odbc_fetch_array($result)) {
$ID_MH = $row["ID_MH"];
$MH_Name = $row["MH_Name"];
// do something
$MH_Name = addslashes($MH_Name);
$update = "UPDATE myTable SET MH_Name='$MH_Name' WHERE ID_MH=" . $ID_MH;
$data_update = odbc_exec($conn, $update);
}
odbc_close($conn);
?>
Try to escape instead of using addslashes:
"UPDATE myTable SET MH_Name=\"$MH_Name\" WHERE ID_MH=" . $ID_MH;
here is the solution:
$query = 'UPDATE myTable SET MH_Name=? WHERE ID_MH=?';
$stmt = odbc_prepare ($conn, $query);
$success = odbc_execute($stmt, array($MH_Name, $ID_MH));
source:https://www.skeletonkey.com/FileMaker_11_ODBC_Drivers/
thanks!
So I've been struggling with my database to get it to give me the name of columns contained within a table.
Here's my PHP :
$sql = "SELECT * FROM hacklvrf_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'generators'" ;
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$generators = $row['generators'];
foreach ($row as $lol) {
echo ($lol);
}
For some reason this isn't answering with anything (PHP doesn't pop an error but my variables seem to be empty) and I don't really understand what I'm missing.
echo (gettype ($row));
Shows a 'NULL'
I know this question has been asked before and I actually got my SQL query from other places but I since I can't work it out... here I am !
Thanks in advance guys !
Change
$sql = "SELECT * FROM hacklvrf_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'generators'";
To
$sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='database-name' AND `TABLE_NAME`='table-name'";
Used your sql query after changing database name and table name, I got error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '.COLUMNS WHERE TABLE_NAME
Updated Code (Just put your database name and table name in query)
<?php
$sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='database-name' AND `TABLE_NAME`='table-name'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo $row['COLUMN_NAME']."<br>";
}
?>
For more info, click MySQL query to get column names?
I'm querying a server and iterating through multiple databases using PHP, but for some reason this $sql2 query (which I have read works in countless threads) is returning a syntax error:
$res = mysqli_query($conn,"SHOW DATABASES");
if (!$res){
// Deal with error
}
while ($d = mysqli_fetch_array($res)){
$db = $d['Database'];
$sql1 = "USE $db";
$query1 = mysqli_query($conn, $sql1);
if (!$query1){
// Deal with error
}
$sql2 = "IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLE
WHERE TABLE_SCHEMA = '$db'
AND TABLE_NAME = 'appusers'))
BEGIN
SELECT * FROM `appusers`
END";
$query2 = mysqli_query($conn, $sql2);
if (!$query2){
// Deal with error
}
}
This is the error I receive:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE WHERE TABLE_S' at line 1
My MySQL Server version is 5.6.27 and my PHP interpreter is 5.6
You can't use an IF statement as a query, only in a stored procedure. You'll need to perform two separate queries.
$sql = "SELECT COUNT(*) AS count
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '$db'
AND TABLE_NAME = 'appusers'";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn);
$row = mysqli_fetch_assoc($result);
if ($row['count'] != 0) {
$sql2 = "SELECT * FROM appusers";
$query2 = mysqli_query($conn, $sql2);
...
} else {
// deal with error
}
There is no if in SQL (although mysql has a function called if)
Apparently, you want to run a query if the table exists. The more common way would be to try running the query and check whether the error you get says that the table doesn't exist.
E.g. if you run the query in the mysql command line application, you might get this:
mysql> SELECT * FROM appusers;
ERROR 1146 (42S02): Table 'test.appusers' doesn't exist
You see two codes:
1146: internal mysql code (obtain this via mysqli_errno)
42S02: the SQL standard error state (obtain this via mysqli_sqlstate)
In your case, checking the SQL state might be better because it covers more cases. E.g., all of these mysql error codes map to SQL state 42S02:
1051 - Unknown table '%s'
1109 - Unknown table '%s' in %s
1146 - Table '%s.%s' doesn't exist
When connecting to a database with PHP I have been using an ODBC connection with the following query:
"SELECT * FROM TAB.LE WHERE TAB.LE.Id = 1";
Where the tablename is TAB.LE.
The code used to make the query isn't required here - but it works fine returning the correct result(s). When I use an OCI connection the same query fails:
$conn = oci_connect("username", "password", "database");
if($conn){
$query = "SELECT * FROM TAB.LE WHERE TAB.LE.Id = 1";
$stid = oci_parse($conn, $query);
$res = oci_execute($query);
if($res){
echo "success";
}
else{
echo "failed";
}
}
I constantly see failed on the screen. I am stumped as to why. The odd thing is: the tablename TAB.LE works for the ODBC connection; however, when viewed in MS Access it shows as TAB_LE. I have attempted to use this different notation in the OCI connection but to no avail.
oci_execute() Executes a statement previously returned from oci_parse().
$stid = oci_parse($conn, $query);
So change
$res = oci_execute($query);
To
$res = oci_execute($stid);
I have php script like this
$query = "select * where userid = 'agusza' ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
echo $result;
}
when I execute, the result like this
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where userid = 'agusza'' at line 1
But when I run that sql in sqlserver, it running well
Anybody has solution ?
$query = "select * from table_name where userid = 'agusza' ";
See the corrections I have made. You haven't used the right syntax for SELECT query
You didn't select a table using FROM. Without that, it does not know which table you are selecting data from.
You should also stop using mysql as it is deprecated. Use mysqli or PDO as they are safer.
You are also echoing the wrong variable in your while loop, try this:
while ($row = mysql_fetch_array($result) {
echo $row['column_name'];
}
$query = "select * from table where userid = 'agusza'";
Right now, you're not telling which table SQL should look in.
You should format your query like so:
select * from `TableName` where userid='agusza'
In your query below you doesnt state the database table where you should get that data using FROM
$query = "select * where userid = 'agusza' "; // instead of this
$query = "select * FROM declaredtable where userid = 'agusza' "; used this