Page 1 of 1

help with PDO / select * from statement

Posted: Sun Dec 21, 2014 6:09 pm
by billythekid
please take a look at my code and help me figure out why im getting the following error:

something along the lines of "my_sql_fetch_array expects parameter 1 to be source"


///////begin code

Code: Select all

$ordernumber = $_POST['ordernumber'];
$dbname = "bob_orders";

$pdo = new PDO( "mysql:dbname=$dbname;host=$host" , $user , $password);


$sql= "SELECT * FROM invoices WHERE OrderNumber= $ordernumber";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':OrderNumber', $ordernumber, PDO::PARAM_INT);
$stmt->execute();

echo "<table  align=\"center\"><tr><td>Order Number</td><td>First Name</td><td>Order Status</td></tr>";
if($pdo){
while($row = mysql_fetch_array($sql))
{
    echo "<tr><td>";
    echo $row['OrderNumber'];
    echo "</td><td>";
    echo $row['FirstMiddle'];
    echo "</td><td>";
    echo $row['Status'];
    echo "</td></tr>";

}

echo "</table><br><br><br><br>";
}
/////////end code

Re: help with PDO / select * from statement

Posted: Sun Dec 21, 2014 6:23 pm
by requinix
You're mixing PDO with the mysql_* functions. They're two different things. You need to pick one and use just that. (PDO would be best.)

$stmt is a PDOStatement which has its own way of getting rows from the query. For example, ->fetch.

Re: help with PDO / select * from statement

Posted: Sun Dec 21, 2014 7:36 pm
by billythekid
thanks. i got it working now with the fetch:assoc query.

if only i could figure out how to make it display as a table instead of a line of text that says "array->[OrderNumber] 'value' " etc. i would be set

Re: help with PDO / select * from statement

Posted: Sun Dec 21, 2014 9:47 pm
by requinix
But... you are using a table. Got the <table> and everything.

Re: help with PDO / select * from statement

Posted: Sun Dec 21, 2014 10:03 pm
by billythekid
Oh, sorry I should've been more clear.

I rewrote the entire query just to perform the fetch function, deleted the table part. Are you saying I can simply add the table part back and it will display properly even with a fetch query instead of the query I was using before?

I assume I will need a foreach or a while loop

Re: help with PDO / select * from statement

Posted: Mon Dec 22, 2014 12:14 am
by requinix
Maybe not "simply", depends what you did to the code of course.

Try adding it back in. If you have problems, post the code and describe what it's doing wrong.