0

I have a select that doesn't work.

$person = mysql_query ("Select personID from persons order by personID desc Limit 0,1");        

$query_string = 'INSERT INTO topics (topic, 
                                      description,
                                      abstract,
                                      personID) 
                            VALUES (?, ?, ?, ?)';

$query = $db->prepare($query_string);

$query->execute(array($_POST['topic'], 
                      $_POST['description'], 
                      $_POST['abstract'],
                      $person));

I dont understand where is the problem

  • What problem ? Tell us why you think there is a problem. Tell us what happens, and what you expected to happen. – nos Nov 01 '13 at 08:45
  • Hi, [you should not use the mysql_ functions any more](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). There are [better choices](http://php.net/manual/en/mysqlinfo.api.choosing.php) like [mysqli](http://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](http://www.php.net/manual/en/ref.pdo-mysql.php) functions. – Gustav Bertram Nov 01 '13 at 09:00
  • 1
    *"Please help me to understand whats wrong in **my** code"* - Gawd I love these types of questions. If it's **"your"** code, then you should already know where you went wrong. When I build code, or even a **"backyard shed"** for that matter, and the foundation, 2x4's etc weren't properly built/put together, then yeah... if you don't know what you're doing or know how to use a hammer/tools, of course something's bound to go "wrong". – Funk Forty Niner Nov 01 '13 at 13:51

4 Answers4

1

$person is a mysql result, not any kind of value.

Try this:

list($person) = mysql_fetch_row(mysql_query("select personID from ....."));
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Here is the problem...

$person = mysql_query ("Select personID from persons order by personID desc Limit 0,1");

Do this...

$result = mysql_query ("Select personID from persons order by personID desc Limit 0,1");
$row = mysql_fetch_array($result);
$person = $row['personID'];
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
0
$dbh = new PDO('mysql:host='.$server.';dbname='.$db, $user, $pass);
$st=$dbh->prepare('Select personID from persons order by personID desc Limit 0,1');
$st->execute();
$result=$st->fetchAll();
//FOR TEST PURPOSE TO MAKE IT EASY.
echo "<pre>";
print_r($result);
echo "</pre>";
//END TEST
echo $result[0]['personID'];

Try this PDO code to select and use data.PDO is a prefererred way. and also instead of mysql use mysqli. we are unclear about your concern. it would be better if you copy paste the error message or make us clear by editing you post, saying what actually you want and what you are unable to do. hope my help works!!

aimme
  • 6,385
  • 7
  • 48
  • 65
0

you are mixing to fetch mysql inside mysqli try this.

   $person = $db->prepare("Select personID from persons order by personID desc Limit 0,1");   
   $person->execute();     
   $person->store_result();
   $person->bind_result( $personID )  ; // to bind the result as variable to use it later
   $person->fetch();

   $query_string = 'INSERT INTO topics (topic, 
                                  description,
                                  abstract,
                                  personID) 
                        VALUES (?, ?, ?, ?)';

   $query = $db->prepare($query_string);

    $query->execute(array($_POST['topic'], 
                  $_POST['description'], 
                  $_POST['abstract'],
                  $personID));
echo_Me
  • 37,078
  • 5
  • 58
  • 78