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());
Related
<?php
//CONNECT TO DATABASE
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="admin";
#mysql_connect("$db_host","$db_username","$db_pass","$db_name")
or die ("not connect");
#mysql_select_db("$db_name") or die ("no database");
echo "succesful connection";
//THEN I CHECK THE VALUES FROM MY FORM
if($_SERVER ['REQUEST_METHOD']=='POST'){
$username=$_POST['username'];
$password=$_POST['password'];
$username=htmlspecialchars($username);
$password=htmlspecialchars($password);
//SEARCH INTO MY DATABASE TABLE
$SQL="SELECT * FROM members WHERE`` username=$username AND password=$password ";
$result=mysql_query($SQL);
//BASED ON MY RESULTS I GIVE TO SESSION VARIABLE A VALUE 1 OR "" AND REDIRECT TO INDEX.PHP
if($result){
$num_rows=mysql_num_rows($result);
if($num_rows>0){
session_start();
$_SESSION['check']="1";
header ("Location:index.php");
}
else{
session_start();
$_SESSION['check']="";
header ("Location:index.php");
}
}
}
?>
#mysql_connect and #mysql_select_db: Please don't do that,
Use mysqli instead of the deprecated mysql extension, see Why shouldn't I use mysql_* functions in PHP?
There is a reason why functions maybe throws errors, you should handle it, instead of using # so they don't show up.
To your problem:
Look at your sql statement:
$SQL="SELECT * FROM members WHERE`` username=$username AND password=$password ";
That doesn't work, you pass $password as plain text for the password, not the value of this var, try:
$SQL='SELECT * FROM members WHERE username="' . $username . '" AND password="' . $password . '";
I think you have issue in your sql query. So try this
$SQL="SELECT * FROM members WHERE `username`='".$username."' AND `password`='".$password."' ";
Issue :
1) You are using direct $username without single quote so if username is string it will not work
2) check that special character you are using after WHERE
I am new to PHP and I have taken up an online tutorial, till now I had been working fine but now my database is not returning the query, though when I go to PHPmyadmin there I can get the query working fine.
Following is the code
<?php
ob_start();
//Delete Item question to admin and delete product
include"../storescripts/connect_to_mysql.php";
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete the item with ID '.$_GET['deleteid'].'?Yes|No';
exit();
}
if(isset($_GET['yesdelete'])){
// Delete the actual product and delete picture also
//delete from database
//$id_to_delete = $_GET['yesdelete'];
//echo $id_to_delete;
$sql =mysqli_query( "DELETE * FROM `products` WHERE `id`=2 LIMIT1 ");
//mysql_query("DELETE * FROM `products` WHERE `id`='$id_to_delete'LIMIT1") or (mysql_error());
//mysqli_query("DELETE * FROM products WHERE id=`$id_to_delete`LIMIT1");// or (mysql_error());
//Unlink file from server
$pictodelete=("../inventory_images/$id_to_delete");
//echo $pictodelete;
if(file_exists($pictodelete)){
unlink($pictodelete);
}
header("location:inventory_list.php");
exit();
}
?>
I would really appreciate the help, my server reads PHP Extension :mysqli .
i dont know what is inside connect_to_mysql.php but at first there is a procedure to connect to a database which i am assuming that you have done correctly, it consist of code which looks something like that at default settings
<?php
$servername = "localhost";
$username = "root";
$password = "";
$databasename="abc";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$databasename);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
the second things i see in your code
$sql =mysqli_query( "DELETE * FROM `products` WHERE `id`=2 LIMIT1 ");
it contains syntax errors,it should be
$sql =mysqli_query( $conn,"DELETE FROM `products` WHERE `id`=2 LIMIT 1 ");
A space after Limit.
You have not specified the connection in mysqli_query() function.
eg:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
In your case
$sql =mysqli_query( $connectionname,"DELETE * FROM `products` WHERE `id`=2 LIMIT 1 ");
Error at query : $sql =mysqli_query( "DELETE * FROM products WHERE id=2 LIMIT1 ");
replace DELETE * FROM products with DELETE FROM products.DELETE delete row from table.
Procedure like mysqli_query takes at least two argument
Link identifier returned form mysqli_connect
Query string
And you haven't specified link as first arguments you should use returned link in to mysqi_query.
$con = mysqli_connect('localhost','root','password','db');
$sql =mysqli_query( $con,"DELETE FROM `products` WHERE `id`=2 LIMIT1 ");
This link helps you link mysqli_query
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_*
I'm trying to SELECT elements from a database. This database uses the UTF-8 encoding.
For example, in one column named title, I have a row with this text: Crăciunul. It appears fine in phpMyAdmin as Crăciunul.
Now, I'm trying to connect to database in PHP and SELECT this database element. The problem is that it appears as Cr?ciunul on my website, which has the UTF-8 encoding setted, too.
This problem appears only if I get elements form database. I have made a string in PHP = Crăciunul, and it is shown fine on the website.
Thanks a lot!
EDIT 1:
$con = mysql_connect("HOST","USER","PASS");
mysql_select_db("DB", $con);
if (mysql_errno($con) !== 0)
{
die("Failed to connect to MySQL: " . mysql_error());
}
if (!mysql_select_db("DB"))
{
die('Could not select database: ' . mysql_error());
}
$query = mysql_query("SELECT * FROM table LIMIT 1");
while($row = mysql_fetch_array($query)) {
echo $row['title'];
}
Set the character encoding of the DB connection.
$con = mysql_connect("HOST","USER","PASS");
mysql_set_charset('utf8', $con);
Also, please don't use this deprecated mysql API for new development.
try this ...
place this code before executing query ...
mysqli_set_charset ($dbconnection, "utf8");
try this
mysql_query('SET NAMES UTF8');
and
mysqli_set_charset($conn,"UTF8");
<?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');
?>