What is wrong with this mysql query? help please [closed] - php

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

Related

How to edit the value updated in a database from the form using php and mysql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to edit the data which is already in the databse.
Suppose I have updated my database, but after an year the user who has updated that database want to edit something like his address which is changed now through the form, then how can we do that. I am posting
<?php
// Create Local variable
$taken = "false";
$database = "railway";
$password = "";
$username = "root";
// Main if statement
//if($userreg && $passreg){
// Connect to database
$con = mysqli_connect('localhost', $username, $password,$database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
---------
WHAT TO WRITE IN BETWEEN
----------------
mysqli_close($con);
Suppose my table name is Railway and the attributes are time, name, station_to and station_from. I want to change the name.
Please write both the form as well as php and mysql query.
It would be something like this:
$sql = "UPDATE Railway SET name='Joe' WHERE id=5";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

Data is not inserted in mysql database [closed]

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

query for fetching only one integer value using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
hey i just want a query which will be printing only the result of count query . put in the else part
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
}
?>
You would use the function mysql_fetch_row() to return a row from your database query as an array. You can then access the result for the count(*) as the first element in the returned array.
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
$row = mysql_fetch_row($rq);
echo $row[0];
}
?>

Reading data from mysql table [closed]

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
In my database table, I have a lot of questions ,each having a category . Say, there are 100 questions and some number of categories which I don't know . I want to know the number and the names of the categories.
Please tell the way to do it in php.
You can try something like this
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT category_name FROM your_table GROUP BY category_name");
$count = mysqli_num_rows($result); // no of categories
while ($row=mysqli_fetch_row($result)) {
echo $row[0]; // printing category name
}
How about...
<?php
$mysqli = mysqli_connect( /* info */) or die ("MySQL error");
$result = mysqli_query("SELECT COUNT(DISTINCT `category`)");
echo $result;
?>
That is assuming categories are all in a single column.

SELECT always returns something even when the db is empty [closed]

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.';
}

Categories