Hi Im duplicating a row with multiple queries and would like to get the mysql_insert_id() from the result. The duplicating works fine but I seem unable to get the last id from it.
Here is my code:
$mysqli = new mysqli("localhost", "xxxx", "xxxx", "xxxx");
if ($mysqli->connect_errno) {
echo($error_page_header);
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
echo($error_page_footer);
exit;
}
$sql = "CREATE TEMPORARY TABLE tmp ENGINE=MEMORY SELECT * FROM test_table WHERE id=1; ";
$sql.= "UPDATE tmp SET id=NULL; ";
$sql.= "INSERT INTO test_table SELECT * FROM tmp; ";
$sql.= "DROP TABLE tmp; ";
if (!$mysqli->multi_query($sql)) {
echo($error_page_header);
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
echo($error_page_footer);
exit;
} do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
echo( 'id: ' . mysql_insert_id() ); // prints 0
Don't be confused .. ext/mysql and mysqli are not compatible and can't be used with each other. Use $mysqli->insert_id (it's per-connection)
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.
Let me explain the story, so I am starting a website users can join and track stuff from a website, I want to create a postback link that inserts 'payout' into the table with the locker code, but when I try and test it, it gives me this error
Execute failed: (2031) No data supplied for parameters in prepared statement
So here is the code that I used...
<?php
define("MYSQL_HOST", "localhost");
define("MYSQL_PORT", "3306");
define("MYSQL_DB", "dbuser");
define("MYSQL_TABLE", "userpayout");
define("MYSQL_USER", "user");
define("MYSQL_PASS", "dbpassweord");
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .
$mysqli->connect_error;
}
$aff_sub1 = $_GET['aff_sub'];
$aff_sub2 = $_GET['aff_sub'];
$aff_sub3 = $_GET['aff_sub'];
$aff_sub4 = $_GET['aff_sub'];
$aff_sub5 = $_GET['aff_sub'];
$aff_sub6 = $_GET['aff_sub'];
$payout = $_GET['payout'];
if (!($stmt = $mysqli->prepare("UPDATE ".MYSQL_DB.".".MYSQL_TABLE." SET
payout=payout+(?) WHERE aff_sub1=(?)")))
{
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$stmt->bind_param('ds', $aff_sub1, $aff_sub2, $aff_sub3, $aff_sub4, $aff_sub5, $aff_sub5, $payout);
if (!$stmt->execute())
{
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else
{
printf("%d Row updated, added $".$payout." to locker ".$aff_sub1." .\n",
mysqli_stmt_affected_rows($stmt));
}
?>
So I am trying to track multiple aff_subs which are the lockers that they get the payout from. I want to insert it into the row with the same affsubs.
Check the variables $aff_sub1, $aff_sub2, payout etc. before calling bind_param to make sure they are set.
Also make sure the postback link is invoked via GET method, since you are retrieving the variables from $_GET.
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 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 know the basics of HTML, and nothing about PHP. I've found a ton of tutorials, but I couldn't get a single one to work. I have a mySQL database working and apache running. How can I display an entire table on my page? I do not know the column names ahead of time.
Edit: Added and adjusted the code. My index page shows this: connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?>
This is my entire index.html source:
<?php
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
Once I changed my file to .php, the error message was more clear. It reminded me that I didn't have webuser at 192.168.1.2. I created the user again and gave the correct permissions. The function appears to work, but I'll have more time in a few hours to look at it.
The real problem here is that you see connect_errno) { echo "Failed to... on your page, instead of a line beginning with Failed to... only.
Because your browser thinks that the code from <?php to $mysqli-> is an HTML tag (because of the opening and closing <>, I think your file is a .htm(l) file rather than a .php file.
Just change the file extension and if PHP is installed correctly it should work.
<?php
$mysqli = new mysqli("localhost", "root", "", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from tbl_comment"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
From what I understood. You want to display values but you don't know column names.
If that's so, try this.
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
foreach($row as $ind => $val)
{
echo "Column name: $ind, Column Value: $val<br />";
}
echo "<hr />";
}
?>