PHP database query failed - php

<?php
require_once 'coninc.php';
$query_run = mysql_query("SELECT * FROM station") or die('Query failed');
?>
I am getting the output as query failed everytime I am trying to refreshing the page. The coninc.php file has no errors. The connection with the database is getting established. The name of the table is correct. Is there anything wrong with the syntax?

Try this ,remove single '' from table name
<?php
require 'coninc.php';
$query_run = mysql_query("SELECT * FROM station") or die('Query failed');
?>

remove ' at table name
<?php
require 'coninc.php';
$query_run = mysql_query("SELECT * FROM station") or die('Query failed');
?>

Related

How to select the top row from database using php

<?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)";

Query is not running when i try to run it using mysql_query() command

I am trying to test if this query runs or not.But i am output with blank screen i.e no output. I am using xampp server.
<?php
$mysql_host='localhost';
$mysql_user='root';
$mysql_pass='';
$mysql_db='a_database';
$con_error='connection error';
$query="SELECT `name` FROM `users` WHERE `id`='1' "; //selecting name from databas where id is 1
$query_run=mysql_query($query); //this is my query
if($query_run)
{
echo 'success';
}
?>
Please help me with this. $query_run neither returns false here nor true. I am not able to understand where the problem is.
First of all try to avoid mysql_* functions from php > 5.4, use mysqli_* function like this.
connect to Databse before running a query like this
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$query="SELECT `name` FROM `users` WHERE `id`='1' "; //selecting name from databas where id is 1
$query_run=mysqli_query($con,$query);
For php < 5.5 use this
$con=mysql_connect("localhost","my_user","my_password","my_db");
$query="SELECT `name` FROM `users` WHERE `id`='1' "; //selecting name from databas where id is 1
$query_run=mysql_query($con,$query);
First of all stop using mysql it is deprecated. Use mysqli now.
In your script you missed the connection to database. Add before your query:
$link = mysqli_connect($mysql_host,mysql_user,$mysql_pass,$mysql_db) or die("Error " . mysqli_error($link));
For more details see this link.
try this:::
<?php
$link = mysqli_connect("localhost","root","root","a_database") or die("Error " . mysqli_error($link));
$query = "SELECT name FROM users" or die("Error in the consult.." . mysqli_error($link));
$result = $link->query($query);
while($row = mysqli_fecth_array($result)) {
echo $row["name"] . "<br>";
}
?>
as mentioned there, use mysqli_*

PHP MySQL DB query error

I am trying to get the top 10 items from a table:
<?php
include DBConnect.php;
$dbname = 'Telejoke';
mysql_select_db($dbname);
$query = "SELECT * FROM jokes LIMIT 10";
$data = mysql_query($query) or die('Error, insert query failed');
mysql_close($conn);
$info = mysql_fetch_array( $data );
?>
The PHP script keeps executing the die part saying that my query insert failed.
UPDATE:
The error is No connection could be made because the target machine actively refused it.
UPDATE 2:
I think the user I connect to DB is not authorized to use the SELECT command. This would cause the preceding error?
In db connection.php you must use a user name with password who is allowed to do operations on db.

mysql where statement working in phpmyadmin but not on site

I have this query:
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM='61'") or die(mysql_error());
In phpmyadmin it returns (as expected) 2 rows but on page it returns 0.
If i use
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM<>'61'") or die(mysql_error());
or
$res=mysql_query("SELECT * FROM `t_modules`") or die(mysql_error());
it runs on the page correctly it's just the WHERE and = combination that doesn't work
I also checked that the type for nPageM is int(11)
UPDATE
I can run comparisons on other columns in the table but not on nPageM
$res=mysql_query("SELECT * FROM `t_modules` WHERE id_md='5'") or die(mysql_error());
Is working. But i still don't have a clue about why it's not working on the nPageM column
have you ensured that you have included a connection to the databse in your php script before running this code?
<?php
$con = mysql_connect('sqluser', 'sqlpassword', 'sqlserver');
$db = mysql_select_db('dbame', $con);
//now, make sure it's connecting
if (!$con) {
die('mysql connection error' . mysql_error());
}
if (!$db) {
die('mysql Database error' . mysql_error());
}
?>
Instead try putting brackets around it:
$res=mysql_query("SELECT * FROM `t_modules` WHERE (nPageM<>'61') ") or die(mysql_error());

insert into mysql problem

i have a field in table opt named confirm of type tinyint. i want to insert value(1) by this statement but it is not working can any one help??
$connect= mysql_connect("localhost","root") or die ("Sorry, Can not connect to database");
mysql_select_db("login") or die (mysql_error());
$user=$_POST['staff'];
echo $user;
$query="SELECT * from users where username='$user' ";
$result=mysql_query($query,$connect) or die(mysql_error());
$row=mysql_fetch_array($result);
$uid=$row['userid'];
echo $uid;
$query="SELECT * from opt where userid='$uid' ";
$result=mysql_query($query,$connect) or die(mysql_error());
$row=mysql_fetch_array($result);
if($row['confirm']==0)
{
$query = "INSERT INTO opt (confirm) values(1)";
echo 'The user selected options has confirmed';
}
?>
You are not executing the query.
add an extra
$result=mysql_query($query,$connect) or die(mysql_error());
after the line
$query = "INSERT INTO opt (confirm) values(1)";
Apart from not executing the "InSERT STATEMENT",
You should probably be using an
"UPDATE OPT SET CONFIRM = '1' WHERE USERID = $user;"
as the row already exists ('cause you managed to select it!).
$query is a variable and there's no reason that it would cause a record to magically get inserted into the opt table.
You need to insert the following line after $query = "...":
mysql_query($query);
Also, I hopethat's not the code you're running in production.
You need to have the following somewhere:
$user = mysql_real_escape_string($user);
Why is not working? what error is throwing?
Check the other fields of the table...

Categories