i am trying to insert a row in table using query->
INSERT INTO organizers(username, hotel, city, state, address, type, price1, price2, contact) VALUES ('nitin','moxvox','jodhpur','rajasthan','shastri circle','1','500','0','1000');
this query works fine. but when i try to give multi word values like username as 'nitin gehlot' instead of 'nitin': the query fails and returns the error part of response.
i am not much familiar with php: please help me.
php code->
$connect= mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
$sqlq=$_GET['query'];
//test
if(mysqli_connect_errno()) {
die("data base connection failed: ".
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
$query = "$sqlq";
$result = mysqli_query($connect,$query);
if(!$result)
{
die("query is faild" . mysql_error);
}
if(strpos($query,'INSERT')!==false)
echo mysqli_insert_id($connect);
else{
echo ' {"result" : 1, "message" : [';$i=1;
while($row = mysqli_fetch_object($result))
{
if($i==1){$i=0;}
else echo',';
$j = json_encode($row);
echo $j;
}
echo ']}';
}
Thanks in advance.
Related
I will be running every midnight a cron job (php script) to copy all the records from one table to another and then delete them from the first one table.
Here is what I have for now:
<?php
include('conf/conn.php');
$result = mysqli_query($conn, "SHOW TABLES FROM mydb LIKE '%salon%'");
while($table = mysqli_fetch_array($result)) {
$tableToCopy = $table[0];
mysqli_begin_transaction($conn, MYSQLI_TRANS_START_READ_ONLY);
mysqli_query($conn, "INSERT INTO history SELECT * FROM $tableToCopy");
// Here is where I'm stuck
// $sql = "INSERT INTO history SELECT * FROM $tableToCopy; ";
// $sql.= "DELETE FROM $tableToCopy; ";
// if (!$mysqli->multi_query($sql)) {
// echo "Both executions failed: (" . $mysqli->errno . ") " . $mysqli->error;
// }
mysqli_commit($conn);
mysqli_close($conn);
if (!mysqli_commit($conn)) {
echo 'Failed.';
echo '<br>';
} else {
echo 'Successful';
echo '<br>';
}
}
?>
I can't find how to make more than one query inside a transaction. Can someone please guide me? Thanks in advance.
Php results that excluded null database entries have now stopped working.
I produced the following test code and it delivers nulls in the result. I am baffled and haven't a clue how to solve. Please help a 75 year old and my first time on an internet question site
<body>
<?PHP
include "dbcfg.php";
$link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword, $dbname) or
die("Error connecting to mysqli server: " . \mysqli_error());
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$attractquery = "select * from eatingout where town = 'Newcastle'";
$result = \mysqli_query($link, $attractquery) or die
("Query to get data from table failed: " . \mysqli_error());
while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected
/*if($db_row['image'] != null){*/
/*if (!empty($db_row['image']));{*/
if(isset($db_row['image']));{
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
?>
</body>
Results from the above include numerous Nulls followed by EOID numbers from any of the three options. What have I done?
You can add NULL test in query:
$attractquery = "select * from eatingout where town = 'Newcastle' AND image is NOT NULL";
Might be a string conversion problem?
while ($db_row = mysqli_fetch_array($result)) {
if(isset($db_row['image']) && $db_row['image'] != NULL && strtolower($db_row['image']) != 'null' ){
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
Please check that your columns containt a NULL value and not a string that says "NULL". You should enable for column image to have NULL value by ticking the checkbox for NULL
<body>
<?PHP
include "dbcfg.php";
$link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword, $dbname) or
die("Error connecting to mysqli server: " . mysqli_error());
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$attractquery = "select * from eatingout where town = 'Newcastle' and ifnull(image,'')=''";
$result = mysqli_query($link, $attractquery) or die
("Query to get data from table failed: " . mysqli_error());
while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected
/*if($db_row['image'] != null){*/
/*if (!empty($db_row['image']));{*/
if(!is_null($db_row['image'])){
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
}
?>
</body>
Kindly remove '\' before mysqli_query and mysqli_error.
You should use !is_null($db_row['image']) instead of isset bcoz $db_row['image'] will always be set.
Secondly you can check the null value in query it self, as mentioned in other answer.
third there is a colan after if clause ie
if(isset($db_row['image']));{
echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ;
}
I am getting 'Trying to get property of non-object error' for the 3 lines in the following code. What can be done to resolve this issue? My full code is:
$con=mysqli_connect("localhost","root","","mydatabase");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$data = json_decode(file_get_contents("php://input"));
$name = mysqli_real_escape_string($con, $data->name); //ERROR FOR THIS LINE
$address = mysqli_real_escape_string($con, $data->address); //ERROR FOR THIS LINE
$sql = "INSERT INTO friend_data(name,address) values ('$name','$address')"; //ERROR FOR THIS LINE
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Record Added";
mysqli_close($con);
Also I am getting 'Undefined variable: id' error for the following code:
$con=mysqli_connect("localhost","root","","mydatabase");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['id']; //ERROR FOR THIS LINE
$sql = "delete from friend_data where id= '$id'";
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Record Removed";
mysqli_close($con);
Can you show me your JSON data. And for second option try this
$id = $_GET['id'];
$con=mysqli_connect("localhost","root","","mydatabase");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "delete from friend_data where id= '$id'";
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Record Removed";
mysqli_close($con);
You need to check that the variables are there before you do anything with them. For the second question you can't guarantee that id was passed in on the URL so do:
if (isset($_GET['id])) {
//do something
} else {
//show an error
}
for the first question again you cant guarantee that what came from "php://input" was a JSON string or that json_decode worked. Break the code into different stages and test that each worked before continuing rather than concatenating it all into a single line.
if (($content = file_get_contents("php://input")) !== FALSE) {
$data = json_decode($content);
if (($data != null) && (is_object($data))) {
//do your stuff
} else {
//error
}
} else {
`//error
}
I want to use the fetch results from different queries as an input in a mysql procedure. To do this, I wrote the following code:
$link = mysqli_connect("localhost","root","","localhost") or die("Error " . mysqli_error($link));
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query_level1 = "select id,name...from table1; ";
$query_level1 = "select id,address...from table2;";
if ($res_level1=mysqli_query($link,$query_level1))
{
while ($row_l1 = mysqli_fetch_row($res_level1))
{
foreach($row_l1 as $key)
{
$parent_l1 = explode(';',$key);
if ( 1 <count($parent_l1))
{
for ($i = 0;$i < count($parent_l1);$i++)
{
$res_level2=mysqli_query($link,$query_level2);
-----here fatch the result of $res_level2 into $child array with another for loop
after this step call a procedure:
if (!$link->query("call update_from_level1(" . $child[$k] . "," . $parent_l1[$i] ")" ))
{
echo "update_from_level1 : (" . $link->errno . ") ";
}
}
}
}
}
}
If I fetch the result of $query_level1, it works properly; but when I fetch the result of $query_level2, it didn't work. Could you help me to figure out the problem?
Mitstake
$query_level1 = "select id,name...from table1; ";
$query_level1 = "select id,address...from table2;";
I'm trying to execute a simple mySQL query in a php page, and I keep getting this error :
"Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in..."
even though the query returns a result in mysql workbench.
This is my code:
<?php
$con=mysqli_connect("localhost","root","","eshkol");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="SET Names utf8";
$sql = mysql_query("SELECT * FROM student WHERE idStudent=2");
$r = mysql_fetch_array($sql);
echo $r["idStudent"];
if (!mysqli_query($con,$sql1))
{
die('Error hebrew: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "success";
mysqli_close($con);
?>
What am I doing wrong here?
You're mixing mysql_* and mysqli_* functions.
$sql = mysql_query($con, "SELECT * FROM student WHERE idStudent=2");
$r = mysql_fetch_array($sql);
should be
$sql = mysqli_query($con, "SELECT * FROM student WHERE idStudent=2");
$r = mysqli_fetch_array($sql);
What's interesting is you're using them just below that code:
if (!mysqli_query($con,$sql1))
{
die('Error hebrew: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
You probably want to combine the two to clean up your code:
$sql = mysql_query($con, "SELECT * FROM student WHERE idStudent=2");
if (!$sql) {
die('Error: ' . mysqli_error($con));
}
$r = mysql_fetch_array($sql);