Tako Andghuladze
Tako Andghuladze

Reputation: 1

A select that is not working

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

Upvotes: 0

Views: 102

Answers (4)

aimme
aimme

Reputation: 6763

$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!!

Upvotes: 0

echo_Me
echo_Me

Reputation: 37233

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));

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

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

Try this:

list($person) = mysql_fetch_row(mysql_query("select personID from ....."));

Upvotes: 1

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

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'];

Upvotes: 0

Related Questions