Jump to content

Recommended Posts

Hey all,

 

First I know basically 0 about programming, so forgive the obvious mistakes.  I have a simple (at least I thought script) that worked in php 5, but no go in php 7. I tried upgrading it to php 7, but can't get it to work.  Can someone please help me.

<?php 
function sentence()  {
	global $post;
mysqli_connect("localhost", "user", "pass", "database");

mysqli_query('set names "utf8"'); 

$query="SELECT name FROM seo ORDER BY RAND() LIMIT 10;";

$result=mysqli_query($query);

$num=mysqli_num_rows($result);

//Begin find and replace
while($row = mysqli_fetch_array($result))
{
	
	//this is the string to look into
	$myString = $row['name'];
	
	//this is the thing to look for
	$patterns = array();
	$patterns[0] = '/1111/';
	
	//this is what you replace it with
	$result1 = $post->post_title; 
	
	//this is the echo
	echo preg_replace_callback($patterns, $result1, $myString);

}
mysqli_close();
}

?>

Thank you in advance

Link to comment
https://forums.phpfreaks.com/topic/303414-difficulty-with-very-old-script/
Share on other sites

Forget about making a function, just learn how to use PDO. Start with a basic select.

 

* In practice, you will not be using a variable for the table name. This is a really basic example.

<?php

$hostdb   = 'localhost';
$dbname   = 'your_DB';
$username = 'root';
$password = '';
$table    = 'YOURTABLE';

    $pdo = new PDO("mysql:host=$hostdb;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql  = "SELECT * FROM $table";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    $result = $stmt->fetchAll();

            echo '<pre>';
            print_r($result);
            echo '</pre>';  
?>
Edited by benanamen

Holy wow, that's way more complicated than I thought it would be.  I will pull a Captain America here an say I understand most of those references.   As an aside I stuck that into my program and it seemed to work.  Generated a bunch of stuff.  I think the entire table actually :)

I was able to modify your code a bit and it seemed to work...

<?php

$hostdb   = 'localhost';
$dbname   = 'your_DB';
$username = 'root';
$password = '';
$table    = 'YOURTABLE';

    $pdo = new PDO("mysql:host=$hostdb;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//modified this line below//
    $sql  = "SELECT name FROM $table ORDER BY RAND() LIMIT 2;";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    $result = $stmt->fetchAll();

            echo '<pre>';
            print_r($result);
            echo '</pre>';  
?>
Edited by fyurien

Holy wow, that's way more complicated than I thought it would be.  I will pull a Captain America here an say I understand most of those references.   As an aside I stuck that into my program and it seemed to work.  Generated a bunch of stuff.  I think the entire table actually :)

 

Really, it isn't that complicated when you break it down.

 

 

Define a bunch of variables to whatever you want.

$hostdb = 'localhost';
$dbname = 'your_DB';
$username = 'root';
$password = '';
$table = 'YOURTABLE';

Okay, at first these can be a little intimidating.  First one just sets up a connection to your database using the host name (likely localhost), and your database name and credentials.  The second line tells it how to deal with errors, and you "can" do without, but probably should leave it in.

$pdo = new PDO("mysql:host=$hostdb;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Define a query, prepare it in MySQL PDO, and execute it.  Note that I slightly changed your query by adding a WHERE query.  The strange part is the silly :id at the end.  This just tells PDO to replace it with the value in the array with the same index.  Instead of :id, you can also use ?, and use a non-associated array.  You can also use some crazy binding and whatnot, but don't worry about that for a while.  Queries with one or two things to replace, use ?, otherwise, use the first method.  Not only are your queries faster (but who cares), they are also much more secure as they are automatically escaped.  

$sql = "SELECT * FROM $table WHERE id=:id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id']=>123);

Get all your results.  You could have used $stmt->fetch(), and it would have pulled only one (such as what you would have wanted to use if you used my example where I had the WHERE clause on the PK. 

$result = $stmt->fetchAll();

And print them.

echo '<pre>';
print_r($result);
echo '</pre>';  

While there are many benefits of PDO, one of the greatest ones is it is so dang easy.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.