Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I made a new table , everything worked.
CREATE TABLE IF NOT EXISTS logdata (
email varchar(30),
password varchar(20),
username varchar(15),)
Inserted the id auto increment code
,and some data :
INSERT INTO logdata(email,password,username,id) VALUES('test#test.org','testtest1','test',' ')
Everything worked here. When I try to output the data i dont get any results (except "ERROR"). I have no idea why.
<?php
error_reporting(E_ALL);
// here is where I set the connection , everything is working here
if(mysqli_connect_errno()){
echo "Could not connect to the database <br /><br />";
echo mysqli_connect_error();
exit();
}
$dostuff="SELECT * FROM logdata";
$query = mysqli_query($db_conn, $dostuff);
if($query == TRUE) {
echo "Succes!";
}
else{
echo "ERROR ";
echo mysqli_error($db_conn);
}
?>
In order to query something in your database, you have to provide a query to it. Your query variable is an empty string!!
$dostuff="";
It should have some SQL statements, like e.g:
$dostuff="SELECT * FROM logdata";
Or whatever.
UPDATE
I believe that using === to test the result will fail because the mysqli_query returns a mysql_result object, according to the docs:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
So if its succeful it won't be === TURE for your SELECT statement and it will have no error. Your query is fine, just try this:
if ($query = mysqli_query($db_conn, $dostuff)) {
echo "Success!";
}
else {
echo "ERROR ";
echo mysqli_error($db_conn);
}
It should works.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need get Data form mySql and echo it . but show to me error ! please help me . I am amator . please check my code. (get Data form mySql and echo it - PHP)
my error in $result1=mysqli_query($link,$query1);
my PHP file :
<?php
$post_data=#$_POST['myjson'];
$post_data=json_decode($post_data,true);
$command=$post_data['command'];
$server="localhost";
$user="user";
$pass="pass";
$db="db";
$link=mysqli_connect($server,$user,$pass,$db);
mysqli_set_charset($link,"utf8");
if ($command=="get_contact") {
$id=$post_data['id'];
$query="select * from ad where id=$id";
$result=mysqli_query($link,$query);
$row=mysqli_fetch_assoc($result);
$num=mysqli_num_rows($result);
if ($num == 1) {
$query1="select * from user where id=$row['user_id']";
$result1=mysqli_query($link,$query1);
$row1=mysqli_fetch_assoc($result1);
$num1=mysqli_num_rows($result1);
if ($num1 == 1) {
$specifications=array("mobile"=>$row1["mobile"], "email"=>$row1["email"]);
echo "<b>".json_encode($specifications)."</b>";
} else {
echo "<b>Not Found</b>";
}
} else {
echo "<b>Not Found</b>";
}
exit();
}
?>
If you expect just one result of each query, you can get the same results with just one query instead of the two you have:
$query = "select user.* from user, ad where ad.id=$id and user.id = ad.user_id";
Also, you should use prepared statements to avoid sql injection instead of writing vars inside the sql queries.
Besides that, give more info in the error messages because now you don't know which error is returning.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am doing a system with php code,But delete function with SQL is not working.I don't know why it happens.
Below is my code:
function deleteEmployee($params)
{
$tid = $_SESSION['tmid'];
$data = array();
//print_R($_POST);die;
$sql = "delete from `cusinfo` WHERE TICKET_ID='".$params["id"]."' AND AGENT_CODE_STAFF_ID IN (SELECT id FROM `users` where tm_groupid = '$tid')";
echo $result = mysqli_query($this->conn, $sql) or die("error to delete employee data");
}
The problem probably is in the line echo $result = mysqli_query($this->conn, $sql) or die("error to delete employee data");
As I said in one comment, replacing the die string with mysqli_error($this->conn) should display an error.
However after some testing I found that assigning a variable in a echo might give strange results, i test echo $test = "hello" or die("test"); and found that neither hello nor test was displayed on the screen, but 1 was displayed, which probably was the boolean true.
A better way to see if the query was executed could be:
//other code that stayed the same
$statement = mysqli_prepare($this->conn, "delete from `cusinfo` WHERE TICKET_ID=? AND AGENT_CODE_STAFF_ID IN (SELECT id FROM `users` where tm_groupid = ?)");
$statement = mysqli_stmt_bind_param($this->conn, $params['id'], $tid); //
$sql = msyqli_stmt_execute($statement); // returns either true or false
if ($sql === true) {
echo "Successfull"; // executing successfull code
}
else {
var_dump(mysqli_stmt_error_list($statement)); // handling error
die;
}
This will handle some sql errors in a way that is expected(they are 'dumped and died').
Using prepared statements the correct way will mean that most sql injections are able to be stopped, and with a DELETE query, you want to make sure that sql injections are stopped.
Note: I am no expert on sql injections
Note 2: I would have used PDO for prepared statements though, it seems to me to be much more logical to work with
echo $result = mysqli_query($this->conn, $sql) or die("error to delete employee data");
In above line you are execution query and echo it. But if it is not executed you are echo your own message. This will prevent you from actual error message. And if the row that you are going to delete from TICKET_ID not exsist you cannot see it, you only see your message "error to delete employee data".
To solve this:
echo mysqli_error($this->conn);
This will give you connection error.
Or:
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
Many many function have to handle these errors. stackoverflow question, php manual and this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
<?php
if (isset($_POST['preview'])){
echo $user = $_SESSION['ue'];
echo $title=$_POST['title'];
echo $dis=$_POST['dis'];
echo $a=$_POST['a'];
echo $b=$_POST['b'];
echo $c=$_POST['c'];
echo $d=$_POST['d'];
echo $timespan=$_POST['timespan'];
$sql="INSERT INTO survey (user, title, description, opta, optb,optc,optd) VALUES ('$user','$title', '$dis', '$a' , '$b', '$c', '$d','timespan')";
if (mysqli_query($con,$sql))
{
echo "Success";
}
else
{
echo "Error: " . mysql_error();
}
mysqli_close($con);
}
?>
Here is my code, all post variables are showed on web page, while data is not inserted in database table. also it does not show any error or exception.
INSERT INTO survey (...) - there is 7 columns
VALUES (...) - and you are sending 8 variables
You didn't escape your $_POST variables
Your connection to database is missing
mysql_error() will not show the errors thrown by the MySQLi functions
There are too many values added: 8 instead of 7
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to select a table and I don't know what I am doing wrong:
$result = mysql_query('SELECT * FROM sfat WHERE done="0" LIMIT 0,10');
$row = mysql_fetch_array($result)
$url = $row["web"];
use
while ($row = mysql_fetch_array($result) ) {
$url = $row["web"];
echo $url;
}
in stead of
$row = mysql_fetch_array($result)
$url = $row["web"];
Because your query indicates you are expecting up to 10 rows. But your code will only show the first one.
The following example will be used to create database
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
{
echo "Database my_db created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
my problem is that always the result is bigger than 0 and all the time he's telling like there is something inside the database, but in fact the database is empty, what could be the reason? I must have done something wrong here.
Thanks in advance.
<?php
header("Content-type: text/html; charset=utf8");
$connection = mysql_connect("localhost","username","password"); // Your Username and Password
if(!$connection)
{
die("database connection failed:" . mysql_error());
}
$db = mysql_select_db("database_name",$connection); // The database name
if(!db)
{
die("database connection failed:" . mysql_error());
}
mysql_query("SET NAMES 'utf8'",$connection);
?>
<?php
$varGet1 = $_POST['deviceToken'];
$varGet2 = $_POST['uniqueIdentifier'];
$result = mysql_query("SELECT * FROM `push_notification_users` WHERE uniqueIdentifier = '".$varGet2."'");
if ($result > 0)
{
echo 'UDID found on Database. Not added.';
}
else
{
$result = mysql_query("INSERT INTO `push_notification_users` (`deviceToken`,`uniqueIdentifier`) VALUES ('".$varGet1."','".$varGet2."')");
if($result === FALSE)
{
die(mysql_error());
}
else
{
$message = "UDID didnt found on Database. Added successfully";
echo $message;
}
}
mysql_close($connection);
?>
This is correct behavior. mysql_query returns RESOURCE or false on error for select query, and you cannot do if ($result > 0). So if the query is correct, $result casted to int will be always larger than 0. You should use if (mysql_num_rows($result) > 0)
Try this:
$result = mysql_num_rows(mysql_query("SELECT * FROM push_notification_users WHERE uniqueIdentifier = '".$varGet2."'"));
if ($result >= 1)
{
echo 'UDID found on Database. Not added.';
}