php mysql search error Couldn't execute query - php

I have written a php script to fetch data but somehow it has stopped working and giving Couldn't execute query, I am unable to find the problem.
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
$id = $_GET['id'];
$sql = "SELECT * FROM datatable WHERE id = $id";
$result = mysql_query($sql) or die("Couldn't execute");

You have used 2 times database name here. Just use this
$db_username = 'uname';
$db_password = 'password';
$db_name = 'db_name';
$db_host = 'localhost';
$db_select =mysql_connect('localhost', 'root','');
$db_=mysql_select_db($db_name,$db_select);
$sql = "SELECT * FROM datatable WHERE id = $id";
$result = mysql_query($sql) or die("Couldn't execute");

Related

mysql num rows help issue

Im totally confised by this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\le\login.php on line 10
Can any one help me? I'm new in php
Code :
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost", "root", "") or die("Could'nt Connect");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
mysql_select_db("shit") or die("cant find db");
$numrows = mysql_num_rows($query);
echo $numrows;
} else
die("please enter username and a password");
?>
I suggest you using mysqli to interact with db.
mysql_query is deprecated. See documentation here: http://php.net/manual/en/function.mysql-query.php
To connect with DB in mysqli you can do:
$DBhost = "localhost";
$DBuser = "your-user";
$DBpass = "your-password";
$DBName = "your-db-name";
// Create connection
$mysqli = new mysqli($DBhost, $DBuser, $DBpass, $DBName);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}

how to use multiple database in opencart?

I have two project, first one in Yii and another in Open cart, I need use both DB in a project, in Yii site I have complete, now I have a issue in open cart so please explain how to use multiple database in open cart?
You can use multiple database something like this:
// Define database
$host = 'localhost';
$db1 = 'username1';
$pwd = 'pass';
$db2 = 'user2';
$pwd2 = 'pass';
$con1 = mysql_connect($host, $db1, $pwd) or die ('Error connecting to mysql');
$con2 = mysql_connect($host, $db2, $pwd2) or die ('2nd Db Error connecting to mysql');
$db_name1 = '`mydatabase`';
mysql_select_db($db_name, $con1);
$db_name2 = '`mydatabase2`';
mysql_select_db($db_name2, $con2);
This is your database actions:
$result = mysql_query("Select * from ......", $con1) or die(mysql_error());
$result = mysql_query("Select * from ......", $con2) or die(mysql_error());

Import data from mdb file to mysql database can not upload data

I'm using PHP code to upload or insert data from MDB file to MySQL database.I want my table value get inserted into MySQL database. But data does not inserted into MySQL database.This code shows me no error.Here is my code. please help I have tried every solution on net.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'payroll_system';
//mysql
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$sql = "SELECT * FROM attendance";
$result = mysql_query($sql);
//mdb
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\xampp\\htdocs\\payroll\\eTimeTrackLite1.mdb");
$rs = $conn2->Execute("SELECT * FROM AttendanceLogs");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$column1=$row["AttendanceLogId"];$column2=$row["AttendanceDate"];$column3=$row["EmployeeId"];$column4=$row["InTime"];
$column6=$row["OutTime"];$column8=$row["Duration"];
echo "hello";
echo $column1;echo $column2;echo $column3;echo $column4;
$rs->MoveFirst();
while (!$rs->EOF)
{
$attendance_id = $rs->Fields("AttendanceLogId");
$attendance_date = $rs->Fields("AttendanceDate");
$emp_id = $rs->Fields("EmployeeId");
$in_time = $rs->Fields("InTime");
$out_time = $rs->Fields("OutTime");
$duration = $rs->Fields("Duration");
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '"); ?>
<?php
$rs->MoveNext();
}
}
?>
</table> <?php
mysql_free_result($result);
$rs->Close();
$conn2->Close();
$rs = null;
$conn2 = null;
?>
you need to INSERT each records data instead of UPDATE. Re-write the following line:
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '");

Backing up a MySQL database using PHP

I was trying to make a backup of my MySQL db called "backup" using a PHP script below, but for some reason, it doesnt work. Any ideas what is wrong? I wanted to create a file called test.sql in the same folder that would contain the data (because the db is quite big I only selected values of Temp>35, but I could change that later). Right now when I run it, I get the echo, but no file created.
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$tableName = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * WHERE Temp>35 INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
echo "Backed up";
?>
Just Try.
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName WHERE Temp>35";
$result = mysql_query($query) or die(mysql_error());
Try this and let me know:
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$databasename = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $databasename WHERE Temp>35";
$result = mysql_query($query);
echo "Backed up";
?>
<?php
$source_db='source_db';
$target_db='target_db';
$server='127.0.0.1';
$user='root';
$password='';
mysql_connect($server,$user,$password);
mysql_select_db($source_db);
// Get names of all tables in source database
$result=mysql_query("show tables");
while($row=mysql_fetch_array($result)){
$name=$row[0];
$this_result=mysql_query("show create table $name");
$this_row=mysql_fetch_array($this_result);
$tables[]=array('name'=>$name,'query'=>$this_row[1]);
}
// Connect target database to create and populate tables
mysql_select_db($target_db);
$total=count($tables);
for($i=0;$i < $total;$i++){
$name=$tables[$i]['name'];
$q=$tables[$i]['query'];
mysql_query($q);
mysql_query("insert into $name select * from $source_db.$name");
}
?>

Jqgrid can Edit but not Add

I have my grid running and I can EDIT data but I cannot ADD new rows of data. When I submit the add form nothing happens and I get nothing back on my PHPMyAdmin page to show that the grid is hit the database with the INSERT statement.
Here is my Edit code.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "*****";
$dbname = "******";
// connect to the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());
mysqli_select_db($conn,$dbname) or die("Error conecting to db.");
//$id = "'".$_POST['id']."'";
$unit = "'".$_POST['unit_id']."'";
$div = "'".$_POST['div_id']."'";
$titleorg = "'".$_POST['title_org']."'";
$shortdesc = "'".$_POST['short_desc']."'";
$longdesc = "'".$_POST['long_desc']."'";
$unitdesc = "'".$_POST['unit_desc']."'";
$enabled = "'".$_POST['avail_ind']."'";
$active = "'Y'";
switch($_POST['oper'])
{
case "add":
$query = "INSERT INTO depunits (unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind,active_ind) values ($div,$unit,$titleorg,$shortdesc,$longdesc,$unitdesc,$enabled,$active)";
mysqli_query($conn,$query);
break;
case "edit":
$query = "UPDATE depunits SET unit_id=$unit,div_id=$div,title_org=$titleorg,short_desc=$shortdesc,long_desc=$longdesc,unit_desc=$unitdesc,avail_ind=$enabled where unit_id=$unit";
mysqli_query($conn,$query);
break;
}
?>

Categories