Jump to content

chrisguk

Members
  • Posts

    24
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

chrisguk's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Its ok woohoo I figured it alone if ($stmt->rowCount() > 0) { while ($row = $stmt->fetch()) { $title = $row[1]; $description = $row[2]; $keywords = $row[3]; } } else { $title = 'Set Default Title'; $description = 'Set Default Description'; $keywords = 'Set Default Keywords'; }
  2. Ok I seem to be getting somewhere now. I used this: while($row = $stmt->fetch()) { print_r($row); } and it printed this: Array ( [id] => 50 [0] => 50 => home [1] => home [title] => Home [2] => Home [descr] => ssssgdgdfgbooooooooooo [3] => ssssgdgdfgbooooooooooo [keywords] => sdfsfsdf [4] => sdfsfsdf ) So I am guessing the query is working. @Jessica I understand that you probably have thousands of posts regarding similar subjects. But please understand I am new to this stuff. I do absolutely appreciate the help though because every piece of advice you give me will help me learn better. Can I ask some advice for the final step please ?
  3. I added that code and inserted the query above. I didnt have errors turned on full but I have now. I ran the above and no errors are produced. based on my previous code what would be the best way to include my desired output, as I am guessing $row is now undefined: if ($stmt->num_rows > 0) { while ($row = $stmt->fetch_object()) { $title = $row->title; $description = $row->descr; $keywords = $row->keywords; } } else { $title = 'Set Default Title'; $description = 'Set Default Description'; $keywords = 'Set Default Keywords'; }
  4. Jess, thank you for your reply. The output is the default values declared in the bottom half of the code. Obviously I am expecting it to produce and output based on the page name that has been selected. For example: I select the home page and the title, keywords and description are pulled from the DB and populated using variables. This is really just the begining as I ultimately want to start populating repetitive text values across the site too in the future. I am extremely new to PDO and prepare statements as for some unknown reason I have been doing things the old way for a long time. When you mention bindValue() between 13-14, would this following example be the correct usage: $stmt->bindValue($page, PDO::PARAM_STR); I have tried it like that and still the code is not retrieving the data. Are there any debug steps I could take to try and understand the function better? Thank you for your patience with me so far Chris
  5. I really hope someone can help me because its driving me mad trying to understand what I need to do here. I have read the PDO and prepare statements manual, but just cant work out whats wrong with my code below: $dsn = "mysql:host=localhost;dbname=maindb"; $user = "root"; $password = "mypass"; try { $pdo = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } // Database $page = isset($_GET['page']) ? $_GET['page'] : 'home'; #$page = $pdo->real_escape_string($page); #$sql = $mysqli->query("SELECT * FROM url WHERE page = '" . $page . "' LIMIT 1"); $stmt = $pdo->prepare("SELECT * FROM url WHERE page = ? LIMIT 1"); $stmt->execute(array($page)); if ($pdo->connect_errno) { printf("Connect failed: %s\n", $pdo->connect_error); exit(); } if ($stmt->num_rows > 0) { while ($row = $stmt->fetch_object()) { $title = $row->title; $description = $row->descr; $keywords = $row->keywords; } } else { $title = 'Set Default Title'; $description = 'Set Default Description'; $keywords = 'Set Default Keywords'; } I would be really glad for any assistance
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.