user2495313
user2495313

Reputation: 91

Cannot retrieve the last row from mysql

I seem to be having trouble getting any response from this script whatsoever. No error messages, nothing...As you can see, what I am trying to do is insert into table "yum", echo the last entry, then use that last entry to insert in a separate table. What am I doing wrong and why aren't there any error messages?

<?php
$b= $_GET["sto"];
$pb= $_GET["usto"];
$sql = "INSERT INTO yum(vara, varb) 
     VALUES('" . $b . "', '" . $pb . "')";

$hostname_Database = "blocked";
$database_Database = "blocked";
$username_Database = "blocked";
$password_Database = "blocked";

$mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database); 
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}



$result = $mysqli->query($sql);
if ($result) {

echo "ha";


$sql = $mysqli->query("SELECT vara FROM yum ORDER BY vara DESC LIMIT 1");

if($sql === FALSE) {
echo "ahahahahah";
    die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_array($sql))
{
    echo $row['vara'];
}



//$row = $result->fetch_assoc();

$sql = "INSERT INTO disco(varb, vara) 
     VALUES( '" . $item . "',  {$row['vara']})";
$result = $mysqli->query($sql);
if ($result) {


etc...


}
}
?>

Upvotes: 0

Views: 112

Answers (1)

Hamza
Hamza

Reputation: 1583

You are using

while($row = mysql_fetch_array($sql))
{
echo $row['vara'];
}

Here when $row becomes null for last entry so use foreach but as you are getting only one row so instead of all the while loop just use

$row = mysql_fetch_array($sql);

Also either use just mysql_* or mysqli_*

Here is the full code

 $mysqli = mysqli_connect($hostname_Database, $username_Database, $password_Database, $database_Database); 
 if (mysqli_connect_errno($mysqli)) {
      printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
     }



 $result = $mysqli_query($con,$sql);
 if ($result) {

   echo "ha";


   $sql = $mysqli_query("SELECT vara FROM yum ORDER BY vara DESC LIMIT 1");

Upvotes: 1

Related Questions