/    Sign up×
Community /Pin to ProfileBookmark

Folks,

What is wrong with my php mysql query ?

[code]
$total_pages = $conn->query(“SELECT * FROM browsing_histories WHERE username = ?”)->num_rows;
[/code]

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘?’ at line 1 in C:xampphtdocstestsearch.php:31 Stack trace: #0 C:xampphtdocstestsearch.php(31): mysqli->query(‘SELECT * FROM b…’) #1 {main} thrown in C:xampphtdocstestsearch.php on line 31

If I remove the “WHERE username = ?” part and make it like the following then I get no error.

[code]
$total_pages = $conn->query(“SELECT * FROM browsing_histories WHERE username = ?”)->num_rows;
[/code]

But making it like this means getting it to pull all rows from the table. I do not want that. I want to pull matching rows in the “username” column.
How to fix this ? How should it have been ?

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMar 15.2019 — How about breaking down your code into separate statements to make it easier to work with. And try using a prepare before querying since you appear to be using a prepare-type query.
Copy linkTweet thisAlerts:
@NogDogMar 15.2019 — Yep, a "?" is not valid SQL -- but if you use the prepare method and then bind a parameter to it and execute() it, you may be okay.
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — @ginerjm#1601800

Thanks man!

If you must know, I found this script on a tutorial. Notice it is in oop style. I only know procedural.

Hence, I won't be able to do what you suggest in oop. Therefore, kindly show me snippet how to do it in oop.

I just started pdo tonight. Meaning, started oop just 3hrs ago.
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — @NogDog#1601802

How-about you ? Care to show me how to do that in oop style ? Like I told Ginerjm, I just started pdo and oop 3hrs ago. One of you 2 showing me how to do what you 2 suggest will be a boost in my oop style learning.

Thanks!
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — Folks,

How come the manual is doing prepare-type-query too ?

http://php.net/manual/en/mysqli.prepare.php

However, by looking at the manual, I do see what Ginerjm means about using statements.
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — Found the tutorial that made me get into this mess when I tried changing it to do username searches:

https://codeshack.io/how-to-create-pagination-php-mysql/

This tutorial just grabs all rows from tbl and creates pagination pages.

I need a script that allows users to do a username search and then displays the results in pagination mode. If you find such a tutorial online then do drop me a line. I been googling for days and not finding any that uses prepared statements using mysqli.

Just found this pdo one but like I said I just got into pdo tonight and prefer to end the pagination building learning using mysqli_ procedural style first.

https://phppot.com/php/php-search-and-pagination-using-pdo/
Copy linkTweet thisAlerts:
@NogDogMar 15.2019 — Tutorials can be useful for a quick, general introduction -- but you're better off using the php.net manual to really figure out how to do things. Simply mimicking what somewhat else did isn't much use if you don't understand why they did it, and potentially recognize why they might not be making the best recommendation for what you need to do.

And no, I'm not going to spend hours here typing out tutorials. I have a full-time coding job, hobbies, youtube videos to watch, sleep to catch up on, etc., etc., and so forth.
Copy linkTweet thisAlerts:
@ginerjmMar 15.2019 — RTFM and LEARN from it. No - I'm not going to write the code for you. You could EASILY convert the problem code you posted to procedural and move forward if you only had the ambition to do it.....
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — @ginerjm#1601823

No. I failed for months to convert it in other forums. Opened threads after threads but no luck.
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — @NogDog#1601820

You watch youtube vids but don't earn anything. This script I am building. Do you know what it does ? Ofcourse you don't.

Others can remotely watch what you're browsing (with your permission) and you earn money when each time they click the same link you clicked while following you.

And so, if you're watching youtube now on your smart fone then others from repmote parts of the world can follow you to the same vid and you'd earn "trailing clicks" which earn you money. Let's say you got 100k twitter followers, 100k youtube subscribers, 100k facebook fans, etc. Let's say all your social network followers total 1m (1 million). Now, everyone of them would be able to follow you online from website to website and webpage to webpage LIVE or later some other time and day. The trailing clicks will earn you money. Trailing clicks from English speaking countries would earn you $5/1k clicks. Other countries lesser rates.

So, from our example, when 1m people from English speaking countries follow you to your first youtube vid that you're watching RIGHT NOW all cosy in bed then you earn $5k within the min. When these same 1m follow you to a 2nd vid you watch in the next min then you earn another $5k. Where the money comes from is my trade secret as for now so no-one can copy my idea and build this baby before me. Yes, there would be copy-cats but I'd get the satisfaction that I started something that others are imitating.

That is what I have been building for 2yrs now by learning php real slowly. Only thing keeping me back is that dratted file upload feature which I am struggling on. Now, I'm thinking I'll ditch it for some other feature.

Well, now you guys know why I bug guys like you to learn php from seniors. Want to build this thing all by myself without hiring a programmer. Will make me feel good when I think an amateur like me built a tool that earns people money. Gets homeless re-homed. Gets the unemployed signing off from the dole. Gets house wives earning something on the side while they take care of their children at home. Gets bankrupt people not commiting suicide or getting heart attacks. Saves women from becoming prostitutes (those who are considering to be such due to lack of decent money or due to being broke). I'm out to be the saviour. Maybe, I should call my web script: Messiah.

Want to put the public, including yourselves, into earning money while you surf online. Unlimited earnings.

Now, it's upto you guys if you want to help me speeden-up my learning. I'm not asking you guys to build my dream for me. Just asking to show code samples so I can learn from them. I always change the codes later once I learn it and get the drift. If you get what I mean. Otherwise, it won't be my code but your's.

Cheers!
Copy linkTweet thisAlerts:
@ginerjmMar 16.2019 — "speeden-up"???? Are you FK'ing me????

When will you learn that this forum (and probably most of them) are not put on the Earth to hand out code snippets that people don't want to learn for themselves? Forums help steer (a word - look it up) people who have managed to write some code onto the correct path because their learning is a bit-off-course. You, on the other hand, are lazy. You don't want to take the time to learn by doing, which IS THE ONLY WAY!!!.

Please stop asking and start doing. And learn proper English while you are at it. I see that you use it fairly well so learning may really be a problem for you with examples such as your last post....
Copy linkTweet thisAlerts:
@site-developerauthorMar 16.2019 — @ginerjm#1601841

I did not understand your last outburst paragraph:

"Please stop asking and start doing. And learn proper English while you are at it. I see that you use it fairly well so learning may really be a problem for you with examples such as your last post...."

You sounded like the fat dwarf Bombur from the Hobbit. I won't understand you unless you take a deep breath and calm down and then write in a cool head. So, rephrase your last outburst. Being more clear this time. Don't give another "huff & puff and I'll blow you away" outcry this way again. I don't want to roll my eyes and say "Here we go again!".
Copy linkTweet thisAlerts:
@site-developerauthorMar 16.2019 — Jimmy (Ginerjm),

I tell you what will keep me busy. Learning froma tutorial how to build a pagination page. I've been searching for days but google nor msn can show me results of a tutorial that teaches pagination with search feature using mysqli proceudral style programming. I found tutorials that teach you how to build pagination with mysqli but the pagination displays all records from the mysql tbl. I don't want that. I want it to show matching rows (search feature). So, if I do a search for "usa" as country then the pagination should only pull those rows that match "usa" from the country column. Search feature.

Such tutorials do not exist on the internet. And if you can find one then show the link to prove me wrong. I tried searching for many days now with a variety of different keywords and phrases on google and msn but no luck.

Let's see how you fair.
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...