PHP and SQL -- Select from database - php

I have a problem with this code. It has syntax error and I don't know what is it.
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'id1381007_accounts';
$conn = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
if (!$conn) {
die('Could not connect: ' . mysql_error());
$sql = 'SELECT id FROM users WHERE email=\"donat12#icloud.com\"';
echo $sql;
?>

There are some issue with the code. First you forgot to close the if condition over here
if (!$conn) {
And then you forgot to execute the sql query
the complete code would be like
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'id1381007_accounts';
$conn = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT id FROM users WHERE email=\"donat12#icloud.com\"';
if ($result = $conn->query($sql)) {
while ( $row = $result->fetch_assoc()) {
$data[] = $row;
}
echo "<pre>";
print_r($data);
echo "</pre>";
}
$conn->close();
?>

There are two errors
You are missing } closing bracket after die
Mysql query is wrong.
So the code should be
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT id FROM users WHERE email="donat12#icloud.com"';
echo $sql;

Related

PHP and mysql connection not working

I have written following code to connect mysql in php but I am not getting output.
<?php
$servername = "localhost";
$username = "root";
$password = "pravin";
$mysql_conn = new mysqli($servername, $username, $password);
if ($mysql_conn->connect_error) {
die("Connection failed: ". $mysql_conn->connect_error);
}
echo "Connected successfully";
$name = $_POST["microorganism"];
echo $name;
$db_selected = mysql_select_db('yieldofvanillin', $mysql_conn);
if (!$db_selected){
die ('Can\'t use : ' . mysql_error());
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
$result = $mysql_query($query);
while ($line = myql_fetch_array($result, MYSQL_ASSOC)) {
echo $line["Substrate"];
echo $line["products"];
echo $line["Microorganism"];
echo $line["yield"];
echo $line["Reference"];
}
mysql_close($mysql_conn);
?>
The database name is "yieldofvanillin" and it has five column. I an getting output Connected successfully. After that no output. Please let me know the bug in code.
i have remove errors. which i mention in comments. Code Reference PHP Manual. you should read this manual (strongly recommended)
<?php
$mysqli = new mysqli("localhost", "root", "pravin", "yieldofvanillin");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row["Substrate"];
echo $row["products"];
echo $row["Microorganism"];
echo $row["yield"];
echo $row["Reference"];
}
/* free result set */
$result->free();
}
you're mixing mysqli and mysql libraries.
the code should be:
<?php
$servername = "localhost";
$username = "root";
$password = "pravin";
$mysql_conn = new mysqli($servername, $username, $password);
if (mysqli_connect_errno()) {
die("Connection failed: ". mysqli_connect_error());
}
echo "Connected successfully";
$name = $_POST["microorganism"];
echo $name;
$db_selected = mysqli_select_db($mysql_conn,'yieldofvanillin');
if (!$db_selected){
die ('Can\'t use : ' . mysqli_error($mysql_conn));
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
$result = mysqli_query( $mysql_conn,$query);
while ($line = mysqli_fetch_assoc($result)) {
echo $line["Substrate"];
echo $line["products"];
echo $line["Microorganism"];
echo $line["yield"];
echo $line["Reference"];
}
mysqli_close($mysql_conn);
?>
Remove error in your code.Read carefully php manual.
<?php
$servername = "localhost";
$username = "root";
$password = "pravin";
$db = "yieldofvanillin";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $db);
/* connection string*/
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo $row["Substrate"];
}
$result->free();
}
$mysqli->close();
?>
Your output not showing because mysql_fetch_array is not correct.Because you are mixing mysql_ and mysqli_ functions and you called myql_fetch_array that doesn't exist in mysqli. MySQL and MySQLi are two different PHP extensions and they cannot be mixed. Because the former is deprecated in mysqli

How to save a variables value out of the loop in PHP?

<?php
//connecting to the database
define('DB_HOST', 'localhost');
define('DB_NAME', 'visitor_list');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " .
mysql_error()); //inserting Record to the database
$dat1 = date("d");
$moth1 = date("m");
$year1 = date("Y");
$sql2 = 'SELECT * FROM list1 ORDER BY card_no DESC LIMIT 1;';
$retval = mysql_query($con,$sql2 );
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
/*echo "{$row['card_no']}";*/
$in= $row['card_no'];
$in++;
}
$ap_ty = $_POST['ap_ty'];
$per = $_POST['per'];
$no_vis = $_POST['no_vis'];
$nm_vis = $_POST['nm_vis'];
$co_nm = $_POST['co_nm'];
$ad = $_POST['ad'];
$po = $_POST['po'];
$ty_vis = $_POST['ty_vis'];
$met_nm = $_POST['met_nm'];
$dep = $_POST['dep'];
$des = $_POST['des'];
$por_met = $_POST['por_met'];
$vad = $_POST['vad'];
if(!isset($_POST['submit'])){
$query = "INSERT INTO list1 (card_no,d1,m1,y1,app_ty,no_per,area_vis,nm,com_nm,add1,pho,ty1_vis,met_with,depart,desi,pur_meet,vad_up) VALUES ('$in','$dat1','$moth1','$year1','$ap_ty','$per','$no_vis','$nm_vis','$co_nm','$ad','$po','$ty_vis','$met_nm','$dep','$des','$por_met','$vad')";}
$result = mysql_query($query);
if($result)
{
echo '<script type="text/javascript">
alert("Your Deatils has been Submitted");
</script> ';
header('location:index.php');
}
else
{ die('Error: '.mysql_error($con)); }
mysql_close($con);
?>
I want to save the $in variables value in the query but not able to save it. At time of insertion it saves the value 0 not the actual value.
<input type="text" class="form-control" name="vis_id" value=" <?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM list1 ORDER BY card_no DESC LIMIT 1;';
mysql_select_db('visitor_list');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
/*echo "{$row['card_no']}";*/
$in= $row['card_no'];
$in+
$test = $in;
}
mysql_close($conn);
?>" disabled>
this is the actual code
in this input box the value is incrementing
help me save the value of this input box into another file which is database file.

Query failed in php

$query = "SELECT * FROM Student WHERE student_id = $child AND school_id = $college";
$result = mysqli_query($conic, $query);
if(!$result){
die(" query failed");
echo 'error here';
} else {
i am trying to retrieve some information from the SQL database but the query seems to fail any ideas?
You must learn more : how to retrieve data from database using select in php
so try out following example to display all the records from Student table
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM student WHERE student_id = '"$child"' AND school_id = '"$college"' ";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
// some code here
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
NOTE
The mysql extension is deprecated and will be removed in the future:
use mysqli or PDO

php mysql data rerival from table

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = "";
$name=$_POST['user'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM `usertable` WHERE `username`='$name'' ;
mysql_select_db('myxiv');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Name :{$row['username']} .<br> ";
}
mysql_close($conn);
?>
Here is my code and i'm getting this error.
Syntax error, unexpected '$name' (T_VARIABLE)
I m trying to retrive data from a table to display in the profile page
$sql = 'SELECT * FROM usertable WHERE username='$name'' ;
Replace Your above query by bellow code and check it.
$sql = "SELECT * FROM usertable WHERE username=' ".$name." ' ";
Try this
Change
$sql = 'SELECT * FROM usertable WHERE username`='$name'' ;
TO
$sql = "SELECT * FROM usertable WHERE username='".$name."';
You make a few mistakes. mysql (which is depracted (take a look at PDO or mysqli)) doesn't need a connection.
You could have a double single quote error but the following should fix some of it:
if($_POST) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$name = mysql_real_escape_string(strip_tags($_POST['user']));
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('myxiv');
$retval = mysql_query("SELECT * FROM `usertable` WHERE `username` = '$name'");
if(! $retval ) {
die('Could not get data: ' . mysql_error());
} else {
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Name :{$row['username']} .<br> ";
}
}
}

syntax error when delete sql data using php

im getting the following syntax error can someone please help!
im guessing it something soooo easy but i have been looking at it for ages and can see what im doing wrong
<?php
if(isset($_POST['delete']))
{
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$CourseId = $_POST['CourseId'];
$sql = "DELETE course ".
" WHERE CourseId = $CourseId" ;
mysql_select_db('d11os_projectdb');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
}
else
{
?>`enter code here`
$sql = "DELETE FROM course ". --<-- Missing key word FROM
" WHERE CourseId = $CourseId"
You are missing the table name from the sql query
$sql = "DELETE course FROM **table_name**".
" WHERE CourseId = $CourseId" ;

Categories