Note that there are some explanatory texts on larger screens.

plurals
  1. POSwitching over from mysql to PDO
    primarykey
    data
    text
    <p>As the title states, I'm switching from old MySQL_statements to PDO. I am having a problem however when trying to see if <code>'tableitem'</code> exists already, then if it does I want to grab <code>'tableitem2'</code>, and if the <code>'tableitem'</code> doesn't exist, I want to set <code>'tableitem2'</code> to 1. Here is my old MySQL code and below I will state what I have tried. </p> <pre><code>$user_ip = $_SERVER['REMOTE_ADDR']; $query = "SELECT * FROM TABLENAME WHERE user_ip='$user_ip' "; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_row($result); if (mysql_num_rows($result) ) { $_SESSION['stage'] = $row[1]; } else { $_SESSION['stage'] = 1; $strSQL2 = "INSERT INTO TABLENAME (user_ip, stage) VALUES ('$user_ip','$stage')"; $result = mysql_query($query) or die(mysql_error()); } </code></pre> <p>The above worked, but now I wish to switch to PDO statements. Here if what I have tried with PDO statements:</p> <pre><code>$user_ip = $_SERVER['REMOTE_ADDR']; $sql = "SELECT COUNT(`stage`) FROM `TABLENAME` WHERE `user_ip` = ?"; $result = $pdo-&gt;prepare($sql); $result-&gt;execute(array($user_ip)); $number_of_rows = $result-&gt;fetchColumn(); /* Check the number of rows that match the SELECT statement */ if ($result-&gt;fetchColumn() &gt; 0) { $_SESSION['stage'] = $row[1]; } else { $_SESSION['stage'] = 1; $result = $pdo-&gt;prepare("INSERT INTO `BPGUV` (`user_ip`, `stage`) VALUES (:user_ip, :stage)"; $result-&gt;execute( array( ':user_ip' =&gt; $user_ip, ':stage' =&gt; 1 ) ); } </code></pre> <p>Any help is much appreciated, I have connected to the database using PDO and am able to use MySQL commands using PDO, just can't seem to transfer the above code into PDO statements properly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload