please help with code error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
preetksgill
Forum Newbie
Posts: 7
Joined: Sat Apr 25, 2015 4:40 pm

please help with code error

Post by preetksgill »

my code is

$description preg_replace('"<a href"','"<a rel=nofollow href"',$description);

it does not work as required and gives a parse error T_String

please guide.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: please help with code error

Post by requinix »

Have you not noticed you need to use the assignment operator?

When that eventually works, you'll discover it's not replacing things they way you want - or at all. Instead of preg_replace() which does so much more than what you're trying to do (and is more complicated making it easier to misuse, as you're doing), use str_replace with about half as many quotes.
preetksgill
Forum Newbie
Posts: 7
Joined: Sat Apr 25, 2015 4:40 pm

Re: please help with code error

Post by preetksgill »

yes i forgot the assignment operator, and i realized it after making my post, it works now thanks

btw how do i use str_replace on the above code ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: please help with code error

Post by Christopher »

preetksgill wrote:btw how do i use str_replace on the above code ?

Code: Select all

$description = str_replace('<a href', '<a rel="nofollow" href', $description);
(#10850)
preetksgill
Forum Newbie
Posts: 7
Joined: Sat Apr 25, 2015 4:40 pm

Re: please help with code error

Post by preetksgill »

okay i have 2 problems.

first problem is that this code gives an error when q is not present in url

index.php
index.php?q=xyz

<?php
//get the q parameter from URL
$q = $_GET['q'];

second problem is that i get a result based on above q value but i am not able to echo it

<?php
//get the q parameter from URL
$q = $_GET['q'];

//sql connect

require_once('../mysqli_connect.php');

$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";

$result = @mysqli_query($conn, $sql);

if ($result)
{
echo "Query executed";
}

echo $result;

i am trying to get the Link i put in database for the given title.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: please help with code error

Post by Celauran »

preetksgill wrote:first problem is that this code gives an error when q is not present in url

index.php
index.php?q=xyz

<?php
//get the q parameter from URL
$q = $_GET['q'];
You need to check if it's set before trying to use it. isset()
preetksgill wrote:second problem is that i get a result based on above q value but i am not able to echo it

<?php
//get the q parameter from URL
$q = $_GET['q'];

//sql connect

require_once('../mysqli_connect.php');

$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";

$result = @mysqli_query($conn, $sql);

if ($result)
{
echo "Query executed";
}

echo $result;

i am trying to get the Link i put in database for the given title.
Pay attention to return types. mysqli::query returns a [http://php.net/manual/en/class.mysqli-result.php]mysqli_result[/url] object from which you must fetch rows.
$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";
This is also ripe for SQL injection. Use prepared statements.
preetksgill
Forum Newbie
Posts: 7
Joined: Sat Apr 25, 2015 4:40 pm

Re: please help with code error

Post by preetksgill »

i am a newbie to code, please guide me with changes required.

i have set isset)_ for the first problem, and enclosed whole of the rest in {}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: please help with code error

Post by Celauran »

Post your code and let's have a look.
preetksgill
Forum Newbie
Posts: 7
Joined: Sat Apr 25, 2015 4:40 pm

Re: please help with code error

Post by preetksgill »

<?php
// get the q parameter from URL
// index.php?q=xyz

if isset($_GET['q'])
{
$q = $_GET['q'];
require_once('../mysqli_connect.php');

$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";

$result = @mysqli_query($conn, $sql);

if ($result)
{
echo "Query executed <br />";
echo $result;

mysqli_close($conn);
}
}

this is my whole code.

i am expecting it to return a url value " http://www.example.com/ " from the database.

i was able to insert it in local xampp database after watching a tutorial for it on youtube.
preetksgill
Forum Newbie
Posts: 7
Joined: Sat Apr 25, 2015 4:40 pm

Re: please help with code error

Post by preetksgill »

Pay attention to return types. mysqli::query returns a [http://php.net/manual/en/class.mysqli-result.php]mysqli_result[/url] object from which you must fetch rows.

damn, this is where i was wrong, it works now

$sql = "SELECT Link FROM rss WHERE Title = ?";

i am using this now
preetksgill
Forum Newbie
Posts: 7
Joined: Sat Apr 25, 2015 4:40 pm

Re: please help with code error

Post by preetksgill »

okay, i am stuck again.

3 problems, and all of them with querystring

if there is no querystring or it is simple index.php
if there is a querystriong but no value index.php?q=
if there is a querystring with a value index.php?q=xyz

for my page to work, it is is essential that i collect q=xyz from the page hyperlink, but at the same time i wish to safeguard against the first 2 situations

how do i do this ?
Post Reply